50 lines
706 B
Go
50 lines
706 B
Go
package cmd
|
|
|
|
type Set []*Command
|
|
|
|
func NewSet() Set {
|
|
return Set{}
|
|
}
|
|
|
|
func (s *Set) Add(c *Command) {
|
|
*s = append(*s, c)
|
|
}
|
|
|
|
func (s Set) Get(name string) (*Command, bool) {
|
|
for _, c := range s {
|
|
if c.Name == name {
|
|
return c, true
|
|
}
|
|
|
|
for _, alias := range c.Aliases {
|
|
if alias == name {
|
|
return c, true
|
|
}
|
|
}
|
|
}
|
|
return nil, false
|
|
}
|
|
|
|
func (s Set) MaxWidth() int {
|
|
max := 0
|
|
for _, f := range s {
|
|
if w := len(f.Name); w > max {
|
|
max = w
|
|
}
|
|
}
|
|
return max
|
|
}
|
|
|
|
func (s Set) Names() []string {
|
|
names := make([]string, 0, len(s))
|
|
for _, c := range s {
|
|
names = append(names, c.Name)
|
|
names = append(names, c.Aliases...)
|
|
}
|
|
return names
|
|
}
|
|
|
|
func (s Set) Len() int {
|
|
return len(s)
|
|
}
|