Skip to content

Commit

Permalink
Simplify calling of the suggestion function.
Browse files Browse the repository at this point in the history
  • Loading branch information
zanvd committed Jan 17, 2025
1 parent e4d96b7 commit 84b14d4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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]))
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 84b14d4

Please sign in to comment.