本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.1KB

  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. }
上海开阖软件有限公司 沪ICP备12045867号-1