Added ExactValidArgs (#765)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by bpdunni · committed by Eric Paris · parent 40172f1 · Browse files · 3 files·+59 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+59 added−0 removed
- other:+59 added−0 removed
Provenance
- Commit
f619abc- Tree
f557b3c- Author
bpdunni <brian.dunnigan@wavestrike.com>- Committer
Eric Paris <eparis@redhat.com>- Parent
40172f1
3 files changed+59 added−0 removed
modified:args_test.go+46 added
156 unmodified lines157 t.Fatalf("Expected %q, got %q", expected, got)158 }159 }160 161+func TestExactValidArgs(t *testing.T) {162+ c := &Command{Use: "c", Args: ExactValidArgs(3), ValidArgs: []string{"a", "b", "c"}, Run: emptyRun}163+ output, err := executeCommand(c, "a", "b", "c")164+ if output != "" {165+ t.Errorf("Unexpected output: %v", output)166+ }167+ if err != nil {168+ t.Errorf("Unexpected error: %v", err)169+ }170+}171+172+func TestExactValidArgsWithInvalidCount(t *testing.T) {173+ c := &Command{Use: "c", Args: ExactValidArgs(2), Run: emptyRun}174+ _, err := executeCommand(c, "a", "b", "c")175+176+ if err == nil {177+ t.Fatal("Expected an error")178+ }179+180+ got := err.Error()181+ expected := "accepts 2 arg(s), received 3"182+ if got != expected {183+ t.Fatalf("Expected %q, got %q", expected, got)184+ }185+}186+187+func TestExactValidArgsWithInvalidArgs(t *testing.T) {188+ c := &Command{189+ Use: "c",190+ Args: ExactValidArgs(1),191+ ValidArgs: []string{"one", "two"},192+ Run: emptyRun,193+ }194+195+ _, err := executeCommand(c, "three")196+ if err == nil {197+ t.Fatal("Expected an error")198+ }199+200+ got := err.Error()201+ expected := `invalid argument "three" for "c"`202+ if got != expected {203+ t.Errorf("Expected: %q, got: %q", expected, got)204+ }205+}206+207 func TestRangeArgs(t *testing.T) {208 c := &Command{Use: "c", Args: RangeArgs(2, 4), Run: emptyRun}209 output, err := executeCommand(c, "a", "b", "c")210 if output != "" {77 unmodified linesmodified:args.go+12 added
76 unmodified lines77 return nil78 }79 }80 81+// ExactValidArgs returns an error if82+// there are not exactly N positional args OR83+// there are any positional args that are not in the `ValidArgs` field of `Command`84+func ExactValidArgs(n int) PositionalArgs {85+ return func(cmd *Command, args []string) error {86+ if err := ExactArgs(n)(cmd, args); err != nil {87+ return err88+ }89+ return OnlyValidArgs(cmd, args)90+ }91+}92+93 // RangeArgs returns an error if the number of args is not within the expected range.94 func RangeArgs(min int, max int) PositionalArgs {95 return func(cmd *Command, args []string) error {96 if len(args) < min || len(args) > max {5 unmodified linesmodified:README.md+1 added
387 unmodified lines388 - `OnlyValidArgs` - the command will report an error if there are any positional args that are not in the `ValidArgs` field of `Command`.389 - `MinimumNArgs(int)` - the command will report an error if there are not at least N positional args.390 - `MaximumNArgs(int)` - the command will report an error if there are more than N positional args.391 - `ExactArgs(int)` - the command will report an error if there are not exactly N positional args.392+- `ExactValidArgs(int)` = the command will report and error if there are not exactly N positional args OR if there are any positional args that are not in the `ValidArgs` field of `Command`393 - `RangeArgs(min, max)` - the command will report an error if the number of args is not between the minimum and maximum number of expected args.394 395 An example of setting the custom validator:396 335 unmodified lines