commands: Fail earlier for arg count checking (by doing it in CLI req parser)
Matt Bell committed
Nov 16, 2014 at 22:44 UTC
9dcf21673dff7cf985cbf2888ccd238fb81d6d10
2 files changed
+15
-16
commands/cli/parse.go
+15
-8
@@ -127,7 +127,7 @@ func parseOptions(input []string) (map[string]interface{}, []string, error) {
127
return opts, args, nil
128
}
129
130
-func parseArgs(inputs []string, stdin *os.File, arguments []cmds.Argument, recursive bool) ([]interface{}, []cmds.File, error) {
130
+func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursive bool) ([]interface{}, []cmds.File, error) {
131
// check if stdin is coming from terminal or is being piped in
132
if stdin != nil {
133
stat, err := stdin.Stat()
@@ -144,7 +144,7 @@ func parseArgs(inputs []string, stdin *os.File, arguments []cmds.Argument, recur
144
145
// count required argument definitions
146
numRequired := 0
147
- for _, argDef := range arguments {
147
+ for _, argDef := range argDefs {
148
if argDef.Required {
149
numRequired++
150
}
@@ -156,18 +156,25 @@ func parseArgs(inputs []string, stdin *os.File, arguments []cmds.Argument, recur
156
numInputs += 1
157
}
158
159
+ // if we have more arg values provided than argument definitions,
160
+ // and the last arg definition is not variadic (or there are no definitions), return an error
161
+ notVariadic := len(argDefs) == 0 || !argDefs[len(argDefs)-1].Variadic
162
+ if notVariadic && numInputs > len(argDefs) {
163
+ return nil, nil, fmt.Errorf("Expected %v arguments, got %v", len(argDefs), numInputs)
164
+ }
165
+
166
stringArgs := make([]interface{}, 0, numInputs)
167
fileArgs := make([]cmds.File, 0, numInputs)
168
169
argDefIndex := 0 // the index of the current argument definition
170
for i, input := range inputs {
164
- // get the argument definiton (should be arguments[argDefIndex],
165
- // but if argDefIndex > len(arguments) we use the last argument definition)
171
+ // get the argument definiton (should be argDefs[argDefIndex],
172
+ // but if argDefIndex > len(argDefs) we use the last argument definition)
173
var argDef cmds.Argument
167
- if argDefIndex < len(arguments) {
168
- argDef = arguments[argDefIndex]
169
- } else if len(arguments) > 0 {
170
- argDef = arguments[len(arguments)-1]
174
+ if argDefIndex < len(argDefs) {
175
+ argDef = argDefs[argDefIndex]
176
+ } else if len(argDefs) > 0 {
177
+ argDef = argDefs[len(argDefs)-1]
178
}
179
180
// skip optional argument definitions if there aren't sufficient remaining inputs
commands/command.go
-8
@@ -199,14 +199,6 @@ func (c *Command) CheckArguments(req Request) error {
199
// TODO: check file arguments
200
201
args := req.Arguments()
202
- argDefs := c.Arguments
203
-
204
- // if we have more arg values provided than argument definitions,
205
- // and the last arg definition is not variadic (or there are no definitions), return an error
206
- notVariadic := len(argDefs) == 0 || !argDefs[len(argDefs)-1].Variadic
207
- if notVariadic && len(args) > len(argDefs) {
208
- return fmt.Errorf("Expected %v arguments, got %v", len(argDefs), len(args))
209
- }
202
203
// count required argument definitions
204
numRequired := 0