2015-08-21 08:58:14 +00:00
|
|
|
package cli
|
|
|
|
|
2016-05-25 16:05:14 +00:00
|
|
|
type CommandCategories interface {
|
|
|
|
// AddCommand adds a command to a category, creating a new category if necessary.
|
|
|
|
AddCommand(category string, command *Command)
|
|
|
|
// Categories returns a copy of the category slice
|
|
|
|
Categories() []CommandCategory
|
2016-05-22 19:20:52 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 16:05:14 +00:00
|
|
|
type commandCategories []*commandCategory
|
|
|
|
|
|
|
|
func newCommandCategories() CommandCategories {
|
|
|
|
ret := commandCategories([]*commandCategory{})
|
|
|
|
return &ret
|
2015-08-21 08:58:14 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 16:05:14 +00:00
|
|
|
func (c *commandCategories) Less(i, j int) bool {
|
|
|
|
return (*c)[i].Name() < (*c)[j].Name()
|
2015-08-21 08:58:14 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 16:05:14 +00:00
|
|
|
func (c *commandCategories) Len() int {
|
|
|
|
return len(*c)
|
2015-08-21 08:58:14 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 16:05:14 +00:00
|
|
|
func (c *commandCategories) Swap(i, j int) {
|
|
|
|
(*c)[i], (*c)[j] = (*c)[j], (*c)[i]
|
2015-08-21 08:58:14 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 16:05:14 +00:00
|
|
|
func (c *commandCategories) AddCommand(category string, command *Command) {
|
|
|
|
for _, commandCategory := range []*commandCategory(*c) {
|
|
|
|
if commandCategory.name == category {
|
2016-05-24 00:15:35 +00:00
|
|
|
commandCategory.commands = append(commandCategory.commands, command)
|
|
|
|
return
|
2015-08-21 08:58:14 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-25 16:05:14 +00:00
|
|
|
newVal := commandCategories(append(*c,
|
|
|
|
&commandCategory{name: category, commands: []*Command{command}}))
|
|
|
|
(*c) = newVal
|
2016-05-24 00:15:35 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 16:05:14 +00:00
|
|
|
func (c *commandCategories) Categories() []CommandCategory {
|
2016-05-27 18:38:51 +00:00
|
|
|
ret := make([]CommandCategory, len(*c))
|
|
|
|
for i, cat := range *c {
|
|
|
|
ret[i] = cat
|
2016-05-25 16:05:14 +00:00
|
|
|
}
|
2016-05-24 00:15:35 +00:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// CommandCategory is a category containing commands.
|
2016-05-25 16:05:14 +00:00
|
|
|
type CommandCategory interface {
|
|
|
|
// Name returns the category name string
|
|
|
|
Name() string
|
|
|
|
// VisibleCommands returns a slice of the Commands with Hidden=false
|
|
|
|
VisibleCommands() []*Command
|
|
|
|
}
|
2016-05-24 00:15:35 +00:00
|
|
|
|
2016-05-25 16:05:14 +00:00
|
|
|
type commandCategory struct {
|
|
|
|
name string
|
2016-05-24 00:15:35 +00:00
|
|
|
commands []*Command
|
2015-08-21 08:58:14 +00:00
|
|
|
}
|
2016-05-03 10:54:05 +00:00
|
|
|
|
2016-05-25 16:05:14 +00:00
|
|
|
func newCommandCategory(name string) *commandCategory {
|
|
|
|
return &commandCategory{
|
|
|
|
name: name,
|
|
|
|
commands: []*Command{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandCategory) Name() string {
|
|
|
|
return c.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandCategory) VisibleCommands() []*Command {
|
|
|
|
if c.commands == nil {
|
|
|
|
c.commands = []*Command{}
|
|
|
|
}
|
|
|
|
|
2016-05-22 19:20:52 +00:00
|
|
|
ret := []*Command{}
|
2016-05-24 00:15:35 +00:00
|
|
|
for _, command := range c.commands {
|
2016-05-03 10:54:05 +00:00
|
|
|
if !command.Hidden {
|
|
|
|
ret = append(ret, command)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|