|
1 // Copyright 2013-2022 The Cobra Authors |
|
2 // |
|
3 // Licensed under the Apache License, Version 2.0 (the "License"); |
|
4 // you may not use this file except in compliance with the License. |
|
5 // You may obtain a copy of the License at |
|
6 // |
|
7 // http://www.apache.org/licenses/LICENSE-2.0 |
|
8 // |
|
9 // Unless required by applicable law or agreed to in writing, software |
|
10 // distributed under the License is distributed on an "AS IS" BASIS, |
|
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
12 // See the License for the specific language governing permissions and |
|
13 // limitations under the License. |
|
14 |
1 package cobra |
15 package cobra |
2 |
16 |
3 import ( |
17 import ( |
4 "fmt" |
18 "fmt" |
5 "strings" |
19 "strings" |
30 return fmt.Errorf("unknown command %q for %q", args[0], cmd.CommandPath()) |
44 return fmt.Errorf("unknown command %q for %q", args[0], cmd.CommandPath()) |
31 } |
45 } |
32 return nil |
46 return nil |
33 } |
47 } |
34 |
48 |
35 // OnlyValidArgs returns an error if any args are not in the list of ValidArgs. |
49 // OnlyValidArgs returns an error if there are any positional args that are not in |
|
50 // the `ValidArgs` field of `Command` |
36 func OnlyValidArgs(cmd *Command, args []string) error { |
51 func OnlyValidArgs(cmd *Command, args []string) error { |
37 if len(cmd.ValidArgs) > 0 { |
52 if len(cmd.ValidArgs) > 0 { |
38 // Remove any description that may be included in ValidArgs. |
53 // Remove any description that may be included in ValidArgs. |
39 // A description is following a tab character. |
54 // A description is following a tab character. |
40 var validArgs []string |
55 var validArgs []string |
41 for _, v := range cmd.ValidArgs { |
56 for _, v := range cmd.ValidArgs { |
42 validArgs = append(validArgs, strings.Split(v, "\t")[0]) |
57 validArgs = append(validArgs, strings.Split(v, "\t")[0]) |
43 } |
58 } |
44 |
|
45 for _, v := range args { |
59 for _, v := range args { |
46 if !stringInSlice(v, validArgs) { |
60 if !stringInSlice(v, validArgs) { |
47 return fmt.Errorf("invalid argument %q for %q%s", v, cmd.CommandPath(), cmd.findSuggestions(args[0])) |
61 return fmt.Errorf("invalid argument %q for %q%s", v, cmd.CommandPath(), cmd.findSuggestions(args[0])) |
48 } |
62 } |
49 } |
63 } |
84 } |
98 } |
85 return nil |
99 return nil |
86 } |
100 } |
87 } |
101 } |
88 |
102 |
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 |
|
101 // RangeArgs returns an error if the number of args is not within the expected range. |
103 // RangeArgs returns an error if the number of args is not within the expected range. |
102 func RangeArgs(min int, max int) PositionalArgs { |
104 func RangeArgs(min int, max int) PositionalArgs { |
103 return func(cmd *Command, args []string) error { |
105 return func(cmd *Command, args []string) error { |
104 if len(args) < min || len(args) > max { |
106 if len(args) < min || len(args) > max { |
105 return fmt.Errorf("accepts between %d and %d arg(s), received %d", min, max, len(args)) |
107 return fmt.Errorf("accepts between %d and %d arg(s), received %d", min, max, len(args)) |