From 4739e933a9f740a23d3cebfeceb0eb3659a1c5eb Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 22 Feb 2023 09:48:53 +0100 Subject: [PATCH] Fix fuzzy command matching when registering built-in help/version commands --- application.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/application.go b/application.go index 8200b6e..5481672 100644 --- a/application.go +++ b/application.go @@ -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 { @@ -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) }