From f4c075f8f8fe5b6cf731fa59f1e982a0ac73b4a5 Mon Sep 17 00:00:00 2001 From: tummychow Date: Wed, 26 Mar 2014 04:53:00 -0400 Subject: [PATCH] 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). --- command.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/command.go b/command.go index 73d009f..61f2104 100644 --- a/command.go +++ b/command.go @@ -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