Skip to content
Myr

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

3 files+59 added−0 removed

modified:args_test.go+46 added
156 unmodified lines
157 t.Fatalf("Expected %q, got %q", expected, got)158 }159}160161func 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}171172func TestExactValidArgsWithInvalidCount(t *testing.T) {173 c := &Command{Use: "c", Args: ExactValidArgs(2), Run: emptyRun}174 _, err := executeCommand(c, "a", "b", "c")175176 if err == nil {177 t.Fatal("Expected an error")178 }179180 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}186187func TestExactValidArgsWithInvalidArgs(t *testing.T) {188 c := &Command{189 Use: "c",190 Args: ExactValidArgs(1),191 ValidArgs: []string{"one", "two"},192 Run: emptyRun,193 }194195 _, err := executeCommand(c, "three")196 if err == nil {197 t.Fatal("Expected an error")198 }199200 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}206207func 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 lines
modified:args.go+12 added
76 unmodified lines
77 return nil78 }79}8081// 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`84func 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}9293// RangeArgs returns an error if the number of args is not within the expected range.94func RangeArgs(min int, max int) PositionalArgs {95 return func(cmd *Command, args []string) error {96 if len(args) < min || len(args) > max {
5 unmodified lines
modified:README.md+1 added
387 unmodified lines
388- `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.394395An example of setting the custom validator:396
335 unmodified lines