category.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package cli
  2. // CommandCategories is a slice of *CommandCategory.
  3. type CommandCategories []*CommandCategory
  4. // CommandCategory is a category containing commands.
  5. type CommandCategory struct {
  6. Name string
  7. Commands Commands
  8. }
  9. func (c CommandCategories) Less(i, j int) bool {
  10. return c[i].Name < c[j].Name
  11. }
  12. func (c CommandCategories) Len() int {
  13. return len(c)
  14. }
  15. func (c CommandCategories) Swap(i, j int) {
  16. c[i], c[j] = c[j], c[i]
  17. }
  18. // AddCommand adds a command to a category.
  19. func (c CommandCategories) AddCommand(category string, command Command) CommandCategories {
  20. for _, commandCategory := range c {
  21. if commandCategory.Name == category {
  22. commandCategory.Commands = append(commandCategory.Commands, command)
  23. return c
  24. }
  25. }
  26. return append(c, &CommandCategory{Name: category, Commands: []Command{command}})
  27. }
  28. // VisibleCommands returns a slice of the Commands with Hidden=false
  29. func (c *CommandCategory) VisibleCommands() []Command {
  30. ret := []Command{}
  31. for _, command := range c.Commands {
  32. if !command.Hidden {
  33. ret = append(ret, command)
  34. }
  35. }
  36. return ret
  37. }