Simplify calling of the suggestion function.

This commit is contained in:
Žan V. Dragan 2025-01-18 00:14:46 +01:00
parent e4d96b75c1
commit 84b14d4e50
2 changed files with 8 additions and 8 deletions

View file

@ -33,7 +33,7 @@ func legacyArgs(cmd *Command, args []string) error {
// root command with subcommands, do subcommand checking.
if !cmd.HasParent() && len(args) > 0 {
return fmt.Errorf("unknown command %q for %q%s", args[0], cmd.CommandPath(), cmd.SuggestFunc()(args[0]))
return fmt.Errorf("unknown command %q for %q%s", args[0], cmd.CommandPath(), cmd.SuggestFunc(args[0]))
}
return nil
}
@ -58,7 +58,7 @@ func OnlyValidArgs(cmd *Command, args []string) error {
}
for _, v := range args {
if !stringInSlice(v, validArgs) {
return fmt.Errorf("invalid argument %q for %q%s", v, cmd.CommandPath(), cmd.SuggestFunc()(args[0]))
return fmt.Errorf("invalid argument %q for %q%s", v, cmd.CommandPath(), cmd.SuggestFunc(args[0]))
}
}
}

View file

@ -483,16 +483,16 @@ func (c *Command) Help() error {
return nil
}
// SuggestFunc returns either the function set by SetSuggestFunc for this command
// or a parent, or it returns a function with default suggestion behavior.
func (c *Command) SuggestFunc() func(string) string {
// SuggestFunc returns suggestions for the provided typedName using either
// the function set by SetSuggestFunc for this command, parent's or a default one.
func (c *Command) SuggestFunc(typedName string) string {
if c.suggestFunc != nil && !c.DisableSuggestions {
return c.suggestFunc
return c.suggestFunc(typedName)
}
if c.HasParent() {
return c.Parent().SuggestFunc()
return c.Parent().SuggestFunc(typedName)
}
return c.findSuggestions
return c.findSuggestions(typedName)
}
// UsageString returns usage string.