commands: Check for option errors when constructing Requests
Matt Bell committed
Nov 16, 2014 at 18:00 UTC
d1c50972f5e3cf44081252241a84ebb9bb8ae84f
3 files changed
+15
-6
commands/cli/parse.go
+4
-1
@@ -46,7 +46,10 @@ func Parse(input []string, stdin *os.File, root *cmds.Command) (cmds.Request, *c
46
}
47
}
48
49
- req := cmds.NewRequest(path, opts, args, cmd, optDefs)
49
+ req, err := cmds.NewRequest(path, opts, args, cmd, optDefs)
50
+ if err != nil {
51
+ return nil, cmd, path, err
52
+ }
53
54
err = cmd.CheckArguments(req)
55
if err != nil {
commands/http/parse.go
+4
-1
@@ -94,7 +94,10 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) {
94
return nil, err
95
}
96
97
- req := cmds.NewRequest(path, opts, args, cmd, optDefs)
97
+ req, err := cmds.NewRequest(path, opts, args, cmd, optDefs)
98
+ if err != nil {
99
+ return nil, err
100
+ }
101
102
err = cmd.CheckArguments(req)
103
if err != nil {
commands/request.go
+7
-4
@@ -248,12 +248,12 @@ func (r *request) ConvertOptions() error {
248
}
249
250
// NewEmptyRequest initializes an empty request
251
-func NewEmptyRequest() Request {
251
+func NewEmptyRequest() (Request, error) {
252
return NewRequest(nil, nil, nil, nil, nil)
253
}
254
255
// NewRequest returns a request initialized with given arguments
256
-func NewRequest(path []string, opts optMap, args []interface{}, cmd *Command, optDefs map[string]Option) Request {
256
+func NewRequest(path []string, opts optMap, args []interface{}, cmd *Command, optDefs map[string]Option) (Request, error) {
257
if path == nil {
258
path = make([]string, 0)
259
}
@@ -268,7 +268,10 @@ func NewRequest(path []string, opts optMap, args []interface{}, cmd *Command, op
268
}
269
270
req := &request{path, opts, args, cmd, Context{}, optDefs}
271
- req.ConvertOptions()
271
+ err := req.ConvertOptions()
272
+ if err != nil {
273
+ return nil, err
274
+ }
275
273
- return req
276
+ return req, nil
277
}