Skip to content

Commit f619abc

Browse files
bpdunnieparis
bpdunni
authored andcommitted
Added ExactValidArgs (#765)
1 parent 40172f1 commit f619abc

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ The following validators are built in:
389389
- `MinimumNArgs(int)` - the command will report an error if there are not at least N positional args.
390390
- `MaximumNArgs(int)` - the command will report an error if there are more than N positional args.
391391
- `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`
392393
- `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.
393394

394395
An example of setting the custom validator:

args.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,18 @@ func ExactArgs(n int) PositionalArgs {
7878
}
7979
}
8080

81+
// ExactValidArgs returns an error if
82+
// there are not exactly N positional args OR
83+
// 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 err
88+
}
89+
return OnlyValidArgs(cmd, args)
90+
}
91+
}
92+
8193
// RangeArgs returns an error if the number of args is not within the expected range.
8294
func RangeArgs(min int, max int) PositionalArgs {
8395
return func(cmd *Command, args []string) error {

args_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,52 @@ func TestExactArgsWithInvalidCount(t *testing.T) {
158158
}
159159
}
160160

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+
161207
func TestRangeArgs(t *testing.T) {
162208
c := &Command{Use: "c", Args: RangeArgs(2, 4), Run: emptyRun}
163209
output, err := executeCommand(c, "a", "b", "c")

0 commit comments

Comments
 (0)