Skip to content

Commit

Permalink
Fix fuzzy command matching when registering built-in help/version com…
Browse files Browse the repository at this point in the history
…mands
  • Loading branch information
fabpot committed Feb 22, 2023
1 parent f6cbcb6 commit 4739e93
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (a *Application) Run(arguments []string) (err error) {
args := context.Args()
if args.Present() {
name := args.first()
context.Command = a.Command(name)
context.Command = a.BestCommand(name)
}

if a.Before != nil {
Expand Down Expand Up @@ -154,12 +154,25 @@ func (a *Application) Run(arguments []string) (err error) {

// Command returns the named command on App. Returns nil if the command does not exist
func (a *Application) Command(name string) *Command {
var matches []*Command
for _, c := range a.Commands {
if c.HasName(name, true) {
c.UserName = name
return c
}
}
return nil
}

// BestCommand returns the named command on App or there is exactly one command the fuzzy matches.
// Returns nil if the command does not exist
func (a *Application) BestCommand(name string) *Command {
if c := a.Command(name); c != nil {
return c
}

// fuzzy match?
var matches []*Command
for _, c := range a.Commands {
if c.HasName(name, false) {
matches = append(matches, c)
}
Expand Down

0 comments on commit 4739e93

Please sign in to comment.