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