commands/cli,http: Properly preserve argument value count when checking argument validity
Matt Bell committed
Nov 12, 2014 at 23:38 UTC
28306a49c42792fbdd67b35be984ac187c42852d
2 files changed
+20
-24
commands/cli/parse.go
+9
-12
@@ -109,8 +109,6 @@ func parseOptions(input []string) (map[string]interface{}, []string, error) {
109
}
110
111
func parseArgs(stringArgs []string, cmd *cmds.Command) ([]interface{}, error) {
112
- args := make([]interface{}, 0)
113
-
112
// count required argument definitions
113
lenRequired := 0
114
for _, argDef := range cmd.Arguments {
@@ -119,6 +117,8 @@ func parseArgs(stringArgs []string, cmd *cmds.Command) ([]interface{}, error) {
117
}
118
}
119
120
+ args := make([]interface{}, len(stringArgs))
121
+
122
valueIndex := 0 // the index of the current stringArgs value
123
for _, argDef := range cmd.Arguments {
124
// skip optional argument definitions if there aren't sufficient remaining values
@@ -134,39 +134,36 @@ func parseArgs(stringArgs []string, cmd *cmds.Command) ([]interface{}, error) {
134
135
if argDef.Variadic {
136
for _, arg := range stringArgs[valueIndex:] {
137
- var err error
138
- args, err = appendArg(args, argDef, arg)
137
+ value, err := argValue(argDef, arg)
138
if err != nil {
139
return nil, err
140
}
141
+ args[valueIndex] = value
142
valueIndex++
143
}
144
} else {
145
var err error
146
- args, err = appendArg(args, argDef, stringArgs[valueIndex])
146
+ value, err := argValue(argDef, stringArgs[valueIndex])
147
if err != nil {
148
return nil, err
149
}
150
+ args[valueIndex] = value
151
valueIndex++
152
}
153
}
154
154
- if len(stringArgs)-valueIndex > 0 {
155
- args = append(args, make([]interface{}, len(stringArgs)-valueIndex))
156
- }
157
-
155
return args, nil
156
}
157
161
-func appendArg(args []interface{}, argDef cmds.Argument, value string) ([]interface{}, error) {
158
+func argValue(argDef cmds.Argument, value string) (interface{}, error) {
159
if argDef.Type == cmds.ArgString {
163
- return append(args, value), nil
160
+ return value, nil
161
162
} else {
163
in, err := os.Open(value) // FIXME(btc) must close file. fix before merge
164
if err != nil {
165
return nil, err
166
}
170
- return append(args, in), nil
167
+ return in, nil
168
}
169
}
commands/http/parse.go
+11
-12
@@ -39,8 +39,6 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) {
39
opts, stringArgs2 := parseOptions(r)
40
stringArgs = append(stringArgs, stringArgs2...)
41
42
- args := make([]interface{}, 0)
43
-
42
// count required argument definitions
43
numRequired := 0
44
for _, argDef := range cmd.Arguments {
@@ -52,13 +50,16 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) {
50
// count the number of provided argument values
51
valCount := len(stringArgs)
52
// TODO: add total number of parts in request body (instead of just 1 if body is present)
55
- if r.Body != nil {
53
+ if r.Body != nil && r.ContentLength != 0 {
54
valCount += 1
55
}
56
57
+ args := make([]interface{}, valCount)
58
+
59
+ valIndex := 0
60
for _, argDef := range cmd.Arguments {
61
// skip optional argument definitions if there aren't sufficient remaining values
61
- if valCount <= numRequired && !argDef.Required {
62
+ if valCount-valIndex <= numRequired && !argDef.Required {
63
continue
64
} else if argDef.Required {
65
numRequired--
@@ -67,14 +68,15 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) {
68
if argDef.Type == cmds.ArgString {
69
if argDef.Variadic {
70
for _, s := range stringArgs {
70
- args = append(args, s)
71
+ args[valIndex] = s
72
+ valIndex++
73
}
74
valCount -= len(stringArgs)
75
76
} else if len(stringArgs) > 0 {
75
- args = append(args, stringArgs[0])
77
+ args[valIndex] = stringArgs[0]
78
stringArgs = stringArgs[1:]
77
- valCount--
79
+ valIndex++
80
81
} else {
82
break
@@ -82,14 +84,11 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) {
84
85
} else {
86
// TODO: create multipart streams for file args
85
- args = append(args, r.Body)
87
+ args[valIndex] = r.Body
88
+ valIndex++
89
}
90
}
91
89
- if valCount-1 > 0 {
90
- args = append(args, make([]interface{}, valCount-1))
91
- }
92
-
92
optDefs, err := root.GetOptions(path)
93
if err != nil {
94
return nil, err