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:
parent
e174a40cf5
commit
f4c075f8f8
10
command.go
10
command.go
@ -239,11 +239,19 @@ func (c *Command) Find(arrs []string) (*Command, []string, error) {
|
|||||||
|
|
||||||
innerfind = func(c *Command, args []string) (*Command, []string) {
|
innerfind = func(c *Command, args []string) (*Command, []string) {
|
||||||
if len(args) > 0 && c.HasSubCommands() {
|
if len(args) > 0 && c.HasSubCommands() {
|
||||||
|
matches := make([]*Command, 0)
|
||||||
for _, cmd := range c.commands {
|
for _, cmd := range c.commands {
|
||||||
if cmd.Name() == args[0] {
|
if cmd.Name() == args[0] { // exact name match
|
||||||
return innerfind(cmd, args[1:])
|
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
|
return c, args
|
||||||
|
Loading…
Reference in New Issue
Block a user