Skip to content
Myr

Add cobra unique args validator (#2397)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by takanuva15 · committed by GitHub · parent 746ef07 · Browse files · 3 files+51 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+51 added−0 removed

  • other:+51 added−0 removed

3 files+51 added−0 removed

modified:site/content/user_guide.md+1 added
445 unmodified lines
446 - `ExactArgs(int)` - report an error if there are not exactly N positional args.447 - `RangeArgs(min, max)` - report an error if the number of args is not between `min` and `max`.448- Content of the arguments:449 - `OnlyValidArgs` - report an error if there are any positional args not specified in the `ValidArgs` field of `Command`, which can optionally be set to a list of valid values for positional args.450 - `NoDuplicateArgs` - report an error if any positional argument value is provided more than once.451452If `Args` is undefined or `nil`, it defaults to `ArbitraryArgs`.453454Moreover, `MatchAll(pargs ...PositionalArgs)` enables combining existing checks with arbitrary other checks.
433 unmodified lines
modified:args_test.go+37 added
105 unmodified lines
106 t.Fatalf("Expected %q, got %q", expected, got)107 }108}109110func noDuplicateArgsWithDuplicate(err error, t *testing.T, arg string) {111 if err == nil {112 t.Fatal("Expected an error")113 }114 got := err.Error()115 expected := `duplicate argument "` + arg + `" for "c"`116 if got != expected {117 t.Errorf("Expected: %q, got: %q", expected, got)118 }119}120121// NoArgs122123func TestNoArgs(t *testing.T) {124 c := getCommand(NoArgs, false)
38 unmodified lines
163 _, err := executeCommand(c, "a")164 validOnlyWithInvalidArgs(err, t)165}166167// NoDuplicateArgs168169func TestNoDuplicateArgs(t *testing.T) {170 c := getCommand(NoDuplicateArgs, false)171 output, err := executeCommand(c, "one", "two")172 expectSuccess(output, err, t)173}174175func TestNoDuplicateArgs_WithDuplicateArgs(t *testing.T) {176 c := getCommand(NoDuplicateArgs, false)177 _, err := executeCommand(c, "one", "one")178 noDuplicateArgsWithDuplicate(err, t, "one")179}180181func TestNoDuplicateArgs_WithValid_WithDuplicateArgs(t *testing.T) {182 c := getCommand(NoDuplicateArgs, true)183 _, err := executeCommand(c, "one", "one")184 noDuplicateArgsWithDuplicate(err, t, "one")185}186187func TestNoDuplicateArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {188 c := getCommand(MatchAll(OnlyValidArgs, NoDuplicateArgs), true)189 _, err := executeCommand(c, "a", "a")190 validOnlyWithInvalidArgs(err, t)191}192193// ArbitraryArgs194195func TestArbitraryArgs(t *testing.T) {196 c := getCommand(ArbitraryArgs, false)
382 unmodified lines
modified:args.go+13 added
63 unmodified lines
64 }65 return nil66}6768// NoDuplicateArgs returns an error if there are any duplicate positional args.69func NoDuplicateArgs(cmd *Command, args []string) error {70 seen := make(map[string]struct{}, len(args))71 for _, arg := range args {72 if _, ok := seen[arg]; ok {73 return fmt.Errorf("duplicate argument %q for %q", arg, cmd.CommandPath())74 }75 seen[arg] = struct{}{}76 }7778 return nil79}8081// ArbitraryArgs never returns an error.82func ArbitraryArgs(cmd *Command, args []string) error {83 return nil84}
60 unmodified lines