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
Provenance
- Commit
ad460ea- Tree
2ba84f4- Author
takanuva15 <6986426+takanuva15@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
746ef07
3 files changed+51 added−0 removed
modified:site/content/user_guide.md+1 added
445 unmodified lines446 - `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.451 452 If `Args` is undefined or `nil`, it defaults to `ArbitraryArgs`.453 454 Moreover, `MatchAll(pargs ...PositionalArgs)` enables combining existing checks with arbitrary other checks.433 unmodified linesmodified:args_test.go+37 added
105 unmodified lines106 t.Fatalf("Expected %q, got %q", expected, got)107 }108 }109 110+func 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+}120+121 // NoArgs122 123 func TestNoArgs(t *testing.T) {124 c := getCommand(NoArgs, false)38 unmodified lines163 _, err := executeCommand(c, "a")164 validOnlyWithInvalidArgs(err, t)165 }166 167+// NoDuplicateArgs168+169+func TestNoDuplicateArgs(t *testing.T) {170+ c := getCommand(NoDuplicateArgs, false)171+ output, err := executeCommand(c, "one", "two")172+ expectSuccess(output, err, t)173+}174+175+func TestNoDuplicateArgs_WithDuplicateArgs(t *testing.T) {176+ c := getCommand(NoDuplicateArgs, false)177+ _, err := executeCommand(c, "one", "one")178+ noDuplicateArgsWithDuplicate(err, t, "one")179+}180+181+func TestNoDuplicateArgs_WithValid_WithDuplicateArgs(t *testing.T) {182+ c := getCommand(NoDuplicateArgs, true)183+ _, err := executeCommand(c, "one", "one")184+ noDuplicateArgsWithDuplicate(err, t, "one")185+}186+187+func TestNoDuplicateArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {188+ c := getCommand(MatchAll(OnlyValidArgs, NoDuplicateArgs), true)189+ _, err := executeCommand(c, "a", "a")190+ validOnlyWithInvalidArgs(err, t)191+}192+193 // ArbitraryArgs194 195 func TestArbitraryArgs(t *testing.T) {196 c := getCommand(ArbitraryArgs, false)382 unmodified linesmodified:args.go+13 added
63 unmodified lines64 }65 return nil66 }67 68+// NoDuplicateArgs returns an error if there are any duplicate positional args.69+func 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+ }77+78+ return nil79+}80+81 // ArbitraryArgs never returns an error.82 func ArbitraryArgs(cmd *Command, args []string) error {83 return nil84 }60 unmodified lines