@@ -7,6 +7,25 @@ import (
77
88type 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.
5555func 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.
10290func 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