mirror of
https://github.com/spf13/cobra
synced 2025-01-30 15:36:47 +00:00
Simplify calling of the suggestion function.
This commit is contained in:
parent
e4d96b75c1
commit
84b14d4e50
2 changed files with 8 additions and 8 deletions
4
args.go
4
args.go
|
@ -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]))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
12
command.go
12
command.go
|
@ -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.
|
||||
|
|
Loading…
Reference in a new issue