Skip to content
Myr

enable composing PositionalArgs (#896)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* enable composing PositionalArgs * ExactValidArgs slated for removal * tests and docs * rename to MatchAll

authored by Nelz · committed by GitHub · parent 9e1d6f1 · Browse files · 3 files+62 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+62 added−0 removed

  • other:+62 added−0 removed

3 files+62 added−0 removed

modified:args_test.go+49 added
1package cobra23import (4 "fmt"5 "strings"6 "testing"7)8
234 unmodified lines
243 if err != nil {244 t.Fatalf("Unexpected error: %v", err)245 }246}247248func TestMatchAll(t *testing.T) {249 // Somewhat contrived example check that ensures there are exactly 3250 // arguments, and each argument is exactly 2 bytes long.251 pargs := MatchAll(252 ExactArgs(3),253 func(cmd *Command, args []string) error {254 for _, arg := range args {255 if len([]byte(arg)) != 2 {256 return fmt.Errorf("expected to be exactly 2 bytes long")257 }258 }259 return nil260 },261 )262263 testCases := map[string]struct {264 args []string265 fail bool266 }{267 "happy path": {268 []string{"aa", "bb", "cc"},269 false,270 },271 "incorrect number of args": {272 []string{"aa", "bb", "cc", "dd"},273 true,274 },275 "incorrect number of bytes in one arg": {276 []string{"aa", "bb", "abc"},277 true,278 },279 }280281 rootCmd := &Command{Use: "root", Args: pargs, Run: emptyRun}282283 for name, tc := range testCases {284 t.Run(name, func(t *testing.T) {285 _, err := executeCommand(rootCmd, tc.args...)286 if err != nil && !tc.fail {287 t.Errorf("unexpected: %v\n", err)288 }289 if err == nil && tc.fail {290 t.Errorf("expected error")291 }292 })293 }294}
modified:args.go+12 added
105 unmodified lines
106 }107 return nil108 }109}110111// MatchAll allows combining several PositionalArgs to work in concert.112func MatchAll(pargs ...PositionalArgs) PositionalArgs {113 return func(cmd *Command, args []string) error {114 for _, parg := range pargs {115 if err := parg(cmd, args); err != nil {116 return err117 }118 }119 return nil120 }121}
modified:user_guide.md+1 added
313 unmodified lines
314- `MaximumNArgs(int)` - the command will report an error if there are more than N positional args.315- `ExactArgs(int)` - the command will report an error if there are not exactly N positional args.316- `ExactValidArgs(int)` - the command will report an 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`317- `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.318- `MatchAll(pargs ...PositionalArgs)` - enables combining existing checks with arbitrary other checks (e.g. you want to check the ExactArgs length along with other qualities).319320An example of setting the custom validator:321322```go
316 unmodified lines