Add command name prefix matching

A command can now be invoked with a prefix of its own name, assuming that
prefix is unambiguous (ie it isn't also a prefix of any sibling command's
name).
This commit is contained in:
tummychow 2014-03-26 04:53:00 -04:00
parent e174a40cf5
commit f4c075f8f8

View file

@ -239,11 +239,19 @@ func (c *Command) Find(arrs []string) (*Command, []string, error) {
innerfind = func(c *Command, args []string) (*Command, []string) {
if len(args) > 0 && c.HasSubCommands() {
matches := make([]*Command, 0)
for _, cmd := range c.commands {
if cmd.Name() == args[0] {
if cmd.Name() == args[0] { // exact name match
return innerfind(cmd, args[1:])
} else if strings.HasPrefix(cmd.Name(), args[0]) { // prefix match
matches = append(matches, cmd)
}
}
// only accept a single prefix match - multiple matches would be ambiguous
if len(matches) == 1 {
return innerfind(matches[0], args[1:])
}
}
return c, args