group.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package cli
  3. import (
  4. "fmt"
  5. "strings"
  6. )
  7. var _ = fmt.Print
  8. type CommandGroup struct {
  9. SubCommands []*Command
  10. Title string
  11. }
  12. func (self *CommandGroup) HasVisibleSubCommands() bool {
  13. for _, c := range self.SubCommands {
  14. if !c.Hidden {
  15. return true
  16. }
  17. }
  18. return false
  19. }
  20. func (self *CommandGroup) Clone(parent *Command) *CommandGroup {
  21. ans := CommandGroup{Title: self.Title, SubCommands: make([]*Command, len(self.SubCommands))}
  22. for i, o := range self.SubCommands {
  23. ans.SubCommands[i] = o.Clone(parent)
  24. }
  25. return &ans
  26. }
  27. func (self *CommandGroup) AddSubCommand(parent *Command, name string) *Command {
  28. ans := NewRootCommand()
  29. ans.Parent = parent
  30. ans.Name = name
  31. self.SubCommands = append(self.SubCommands, ans)
  32. return ans
  33. }
  34. func (self *CommandGroup) FindSubCommand(name string) *Command {
  35. for _, c := range self.SubCommands {
  36. if c.Name == name {
  37. return c
  38. }
  39. }
  40. return nil
  41. }
  42. func (self *CommandGroup) FindSubCommands(prefix string, matches []*Command) []*Command {
  43. for _, c := range self.SubCommands {
  44. if strings.HasPrefix(c.Name, prefix) {
  45. matches = append(matches, c)
  46. }
  47. }
  48. return matches
  49. }
  50. type OptionGroup struct {
  51. Options []*Option
  52. Title string
  53. }
  54. func (self *OptionGroup) Clone(parent *Command) *OptionGroup {
  55. ans := OptionGroup{Title: self.Title, Options: make([]*Option, len(self.Options))}
  56. for i, o := range self.Options {
  57. c := *o
  58. c.init_option()
  59. c.Parent = parent
  60. ans.Options[i] = &c
  61. }
  62. return &ans
  63. }
  64. func (self *OptionGroup) AddOption(parent *Command, spec OptionSpec) (*Option, error) {
  65. ans, err := option_from_spec(spec)
  66. if err == nil {
  67. ans.Parent = parent
  68. self.Options = append(self.Options, ans)
  69. }
  70. return ans, err
  71. }
  72. func (self *OptionGroup) AddOptionFromString(parent *Command, items ...string) (*Option, error) {
  73. ans, err := OptionFromString(items...)
  74. if err == nil {
  75. ans.Parent = parent
  76. self.Options = append(self.Options, ans)
  77. }
  78. return ans, err
  79. }
  80. func (self *OptionGroup) FindOptions(prefix_with_hyphens string) []*Option {
  81. is_short := !strings.HasPrefix(prefix_with_hyphens, "--")
  82. option_name := NormalizeOptionName(prefix_with_hyphens)
  83. ans := make([]*Option, 0, 4)
  84. for _, q := range self.Options {
  85. if q.MatchingAlias(option_name, is_short) != "" {
  86. ans = append(ans, q)
  87. }
  88. }
  89. return ans
  90. }
  91. func (self *OptionGroup) FindOption(name_with_hyphens string) *Option {
  92. is_short := !strings.HasPrefix(name_with_hyphens, "--")
  93. option_name := NormalizeOptionName(name_with_hyphens)
  94. for _, q := range self.Options {
  95. if q.HasAlias(option_name, is_short) {
  96. return q
  97. }
  98. }
  99. return nil
  100. }