Skip to content
Myr

Add command name prefix matching

Before adoption: this commit came in with the history the project adopted, so no landing records it.

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).

authored by tummychow · committed by tummychow · parent e174a40 · Browse files · 1 file+9 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+9 added−1 removed

  • other:+9 added−1 removed

1 file+9 added−1 removed

modified:command.go+9 added-1 removed
237 unmodified lines
238 var innerfind func(*Command, []string) (*Command, []string)239240 innerfind = func(c *Command, args []string) (*Command, []string) {241 if len(args) > 0 && c.HasSubCommands() {242 matches := make([]*Command, 0)243 for _, cmd := range c.commands {243 if cmd.Name() == args[0] {244 if cmd.Name() == args[0] { // exact name match245 return innerfind(cmd, args[1:])246 } else if strings.HasPrefix(cmd.Name(), args[0]) { // prefix match247 matches = append(matches, cmd)248 }249 }250251 // only accept a single prefix match - multiple matches would be ambiguous252 if len(matches) == 1 {253 return innerfind(matches[0], args[1:])254 }255 }256257 return c, args258 }
394 unmodified lines