Skip to content

Commit ce4ae68

Browse files
umarcorhoshsadiq
authored andcommitted
Generalize ValidArgs: use it implicitly with any validator
5 of the commits in this PR are about refactoring args_test.go. The sixth one (feat: generalize ValidArgs; use it implicitly with any validator) moves the validation of ValidArgs from args.go to command.go. As a result: - Any validator can be used along with ValidArgs. ValidArgs is checked first, and then the defined validator. - OnlyValidArgs and ExactValidArgs are deprecated. args_test.go is updated accordingly: ``` === RUN TestNoArgs --- PASS: TestNoArgs (0.00s) === RUN TestNoArgsWithArgs --- PASS: TestNoArgsWithArgs (0.00s) === RUN TestNoArgsWithArgsWithValid --- PASS: TestNoArgsWithArgsWithValid (0.00s) === RUN TestArbitraryArgs --- PASS: TestArbitraryArgs (0.00s) === RUN TestArbitraryArgsWithValid --- PASS: TestArbitraryArgsWithValid (0.00s) === RUN TestArbitraryArgsWithValidWithInvalidArgs --- PASS: TestArbitraryArgsWithValidWithInvalidArgs (0.00s) === RUN TestMinimumNArgs --- PASS: TestMinimumNArgs (0.00s) === RUN TestMinimumNArgsWithValid --- PASS: TestMinimumNArgsWithValid (0.00s) === RUN TestMinimumNArgsWithValidWithInvalidArgs --- PASS: TestMinimumNArgsWithValidWithInvalidArgs (0.00s) === RUN TestMinimumNArgsWithLessArgs --- PASS: TestMinimumNArgsWithLessArgs (0.00s) === RUN TestMinimumNArgsWithLessArgsWithValid --- PASS: TestMinimumNArgsWithLessArgsWithValid (0.00s) === RUN TestMinimumNArgsWithLessArgsWithValidWithInvalidArgs --- PASS: TestMinimumNArgsWithLessArgsWithValidWithInvalidArgs (0.00s) === RUN TestMaximumNArgs --- PASS: TestMaximumNArgs (0.00s) === RUN TestMaximumNArgsWithValid --- PASS: TestMaximumNArgsWithValid (0.00s) === RUN TestMaximumNArgsWithValidWithInvalidArgs --- PASS: TestMaximumNArgsWithValidWithInvalidArgs (0.00s) === RUN TestMaximumNArgsWithMoreArgs --- PASS: TestMaximumNArgsWithMoreArgs (0.00s) === RUN TestMaximumNArgsWithMoreArgsWithValid --- PASS: TestMaximumNArgsWithMoreArgsWithValid (0.00s) === RUN TestMaximumNArgsWithMoreArgsWithValidWithInvalidArgs --- PASS: TestMaximumNArgsWithMoreArgsWithValidWithInvalidArgs (0.00s) === RUN TestExactArgs --- PASS: TestExactArgs (0.00s) === RUN TestExactArgsWithValid --- PASS: TestExactArgsWithValid (0.00s) === RUN TestExactArgsWithValidWithInvalidArgs --- PASS: TestExactArgsWithValidWithInvalidArgs (0.00s) === RUN TestExactArgsWithInvalidCount --- PASS: TestExactArgsWithInvalidCount (0.00s) === RUN TestExactArgsWithInvalidCountWithValid --- PASS: TestExactArgsWithInvalidCountWithValid (0.00s) === RUN TestExactArgsWithInvalidCountWithValidWithInvalidArgs --- PASS: TestExactArgsWithInvalidCountWithValidWithInvalidArgs (0.00s) === RUN TestRangeArgs --- PASS: TestRangeArgs (0.00s) === RUN TestRangeArgsWithValid --- PASS: TestRangeArgsWithValid (0.00s) === RUN TestRangeArgsWithValidWithInvalidArgs --- PASS: TestRangeArgsWithValidWithInvalidArgs (0.00s) === RUN TestRangeArgsWithInvalidCount --- PASS: TestRangeArgsWithInvalidCount (0.00s) === RUN TestRangeArgsWithInvalidCountWithValid --- PASS: TestRangeArgsWithInvalidCountWithValid (0.00s) === RUN TestRangeArgsWithInvalidCountWithValidWithInvalidArgs --- PASS: TestRangeArgsWithInvalidCountWithValidWithInvalidArgs (0.00s) === RUN TestRootTakesNoArgs --- PASS: TestRootTakesNoArgs (0.00s) === RUN TestRootTakesArgs --- PASS: TestRootTakesArgs (0.00s) === RUN TestChildTakesNoArgs --- PASS: TestChildTakesNoArgs (0.00s) === RUN TestChildTakesArgs --- PASS: TestChildTakesArgs (0.00s) ``` Merge spf13/cobra#841 Fix #838 and Fix #745
1 parent ebbf83f commit ce4ae68

File tree

5 files changed

+186
-221
lines changed

5 files changed

+186
-221
lines changed

args.go

+34-31
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,25 @@ import (
77

88
type PositionalArgs func(cmd *Command, args []string) error
99

10+
// validateArgs returns an error if there are any positional args that are not in
11+
// the `ValidArgs` field of `Command`
12+
func validateArgs(cmd *Command, args []string) error {
13+
if len(cmd.ValidArgs) > 0 {
14+
// Remove any description that may be included in ValidArgs.
15+
// A description is following a tab character.
16+
var validArgs []string
17+
for _, v := range cmd.ValidArgs {
18+
validArgs = append(validArgs, strings.Split(v, "\t")[0])
19+
}
20+
for _, v := range args {
21+
if !stringInSlice(v, validArgs) {
22+
return fmt.Errorf("invalid argument %q for %q%s", v, cmd.CommandPath(), cmd.findSuggestions(args[0]))
23+
}
24+
}
25+
}
26+
return nil
27+
}
28+
1029
// Legacy arg validation has the following behaviour:
1130
// - root commands with no subcommands can take arbitrary arguments
1231
// - root commands with subcommands will do subcommand validity checking
@@ -32,25 +51,6 @@ func NoArgs(cmd *Command, args []string) error {
3251
return nil
3352
}
3453

35-
// OnlyValidArgs returns an error if any args are not in the list of ValidArgs.
36-
func OnlyValidArgs(cmd *Command, args []string) error {
37-
if len(cmd.ValidArgs) > 0 {
38-
// Remove any description that may be included in ValidArgs.
39-
// A description is following a tab character.
40-
var validArgs []string
41-
for _, v := range cmd.ValidArgs {
42-
validArgs = append(validArgs, strings.Split(v, "\t")[0])
43-
}
44-
45-
for _, v := range args {
46-
if !stringInSlice(v, validArgs) {
47-
return fmt.Errorf("invalid argument %q for %q%s", v, cmd.CommandPath(), cmd.findSuggestions(args[0]))
48-
}
49-
}
50-
}
51-
return nil
52-
}
53-
5454
// ArbitraryArgs never returns an error.
5555
func ArbitraryArgs(cmd *Command, args []string) error {
5656
return nil
@@ -86,18 +86,6 @@ func ExactArgs(n int) PositionalArgs {
8686
}
8787
}
8888

89-
// ExactValidArgs returns an error if
90-
// there are not exactly N positional args OR
91-
// there are any positional args that are not in the `ValidArgs` field of `Command`
92-
func ExactValidArgs(n int) PositionalArgs {
93-
return func(cmd *Command, args []string) error {
94-
if err := ExactArgs(n)(cmd, args); err != nil {
95-
return err
96-
}
97-
return OnlyValidArgs(cmd, args)
98-
}
99-
}
100-
10189
// RangeArgs returns an error if the number of args is not within the expected range.
10290
func RangeArgs(min int, max int) PositionalArgs {
10391
return func(cmd *Command, args []string) error {
@@ -119,3 +107,18 @@ func MatchAll(pargs ...PositionalArgs) PositionalArgs {
119107
return nil
120108
}
121109
}
110+
111+
// ExactValidArgs returns an error if there are not exactly N positional args OR
112+
// there are any positional args that are not in the `ValidArgs` field of `Command`
113+
//
114+
// Deprecated: now `ExactArgs` honors `ValidArgs`, when defined and not empty
115+
func ExactValidArgs(n int) PositionalArgs {
116+
return ExactArgs(n)
117+
}
118+
119+
// OnlyValidArgs returns an error if any args are not in the list of `ValidArgs`.
120+
//
121+
// Deprecated: now `ArbitraryArgs` honors `ValidArgs`, when defined and not empty
122+
func OnlyValidArgs(cmd *Command, args []string) error {
123+
return ArbitraryArgs(cmd, args)
124+
}

0 commit comments

Comments
 (0)