MCPcopy
hub / github.com/spf13/cobra / OnlyValidArgs

Function OnlyValidArgs

args.go:51–66  ·  view source on GitHub ↗

OnlyValidArgs returns an error if there are any positional args that are not in the `ValidArgs` field of `Command`

(cmd *Command, args []string)

Source from the content-addressed store, hash-verified

49// OnlyValidArgs returns an error if there are any positional args that are not in
50// the `ValidArgs` field of `Command`
51func OnlyValidArgs(cmd *Command, args []string) error {
52 if len(cmd.ValidArgs) > 0 {
53 // Remove any description that may be included in ValidArgs.
54 // A description is following a tab character.
55 validArgs := make([]string, 0, len(cmd.ValidArgs))
56 for _, v := range cmd.ValidArgs {
57 validArgs = append(validArgs, strings.SplitN(v, "\t", 2)[0])
58 }
59 for _, v := range args {
60 if !stringInSlice(v, validArgs) {
61 return fmt.Errorf("invalid argument %q for %q%s", v, cmd.CommandPath(), cmd.findSuggestions(args[0]))
62 }
63 }
64 }
65 return nil
66}
67
68// ArbitraryArgs never returns an error.
69func ArbitraryArgs(cmd *Command, args []string) error {

Callers

nothing calls this directly

Calls 3

stringInSliceFunction · 0.85
CommandPathMethod · 0.80
findSuggestionsMethod · 0.80

Tested by

no test coverage detected