Skip to content

Commit

Permalink
add MatchAll: enable composing PositionalArgs (spf13#896)
Browse files Browse the repository at this point in the history
  • Loading branch information
nelz9999 authored and umarcor committed Sep 25, 2020
1 parent e5e4643 commit 20e85a4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ of `Command`. The following validators are built in:
- `MaximumNArgs(int)` - report an error if more than N positional args are provided.
- `ExactArgs(int)` - report an error if there are not exactly N positional args.
- `RangeArgs(min, max)` - report an error if the number of args is not between `min` and `max`.
- `MatchAll(pargs ...PositionalArgs)` - enables combining existing checks with arbitrary other checks (e.g. you want to check the ExactArgs length along with other qualities).

If `Args` is undefined or `nil`, it defaults to `ArbitraryArgs`.

Expand Down
12 changes: 12 additions & 0 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ func RangeArgs(min int, max int) PositionalArgs {
}
}

// MatchAll allows combining several PositionalArgs to work in concert.
func MatchAll(pargs ...PositionalArgs) PositionalArgs {
return func(cmd *Command, args []string) error {
for _, parg := range pargs {
if err := parg(cmd, args); err != nil {
return err
}
}
return nil
}
}

// ExactValidArgs returns an error if there are not exactly N positional args OR
// there are any positional args that are not in the `ValidArgs` field of `Command`
//
Expand Down
49 changes: 49 additions & 0 deletions args_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cobra

import (
"fmt"
"strings"
"testing"
)
Expand Down Expand Up @@ -184,3 +185,51 @@ func TestChildTakesArgs(t *testing.T) {
t.Fatalf("Unexpected error: %v", err)
}
}

func TestMatchAll(t *testing.T) {
// Somewhat contrived example check that ensures there are exactly 3
// arguments, and each argument is exactly 2 bytes long.
pargs := MatchAll(
ExactArgs(3),
func(cmd *Command, args []string) error {
for _, arg := range args {
if len([]byte(arg)) != 2 {
return fmt.Errorf("expected to be exactly 2 bytes long")
}
}
return nil
},
)

testCases := map[string]struct {
args []string
fail bool
}{
"happy path": {
[]string{"aa", "bb", "cc"},
false,
},
"incorrect number of args": {
[]string{"aa", "bb", "cc", "dd"},
true,
},
"incorrect number of bytes in one arg": {
[]string{"aa", "bb", "abc"},
true,
},
}

rootCmd := &Command{Use: "root", Args: pargs, Run: emptyRun}

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
_, err := executeCommand(rootCmd, tc.args...)
if err != nil && !tc.fail {
t.Errorf("unexpected: %v\n", err)
}
if err == nil && tc.fail {
t.Errorf("expected error")
}
})
}
}

0 comments on commit 20e85a4

Please sign in to comment.