diff --git a/commands/cli/parse.go b/commands/cli/parse.go index 17b5dab16d8027013469d748270b003bb2ba691a..027134dc3d0ae723f13aed86d111b1f7e1f5b45d 100644 --- a/commands/cli/parse.go +++ b/commands/cli/parse.go @@ -46,7 +46,10 @@ func Parse(input []string, stdin *os.File, root *cmds.Command) (cmds.Request, *c } } - req := cmds.NewRequest(path, opts, args, cmd, optDefs) + req, err := cmds.NewRequest(path, opts, args, cmd, optDefs) + if err != nil { + return nil, cmd, path, err + } err = cmd.CheckArguments(req) if err != nil { diff --git a/commands/command_test.go b/commands/command_test.go index 44a39d0c144589cf050bdcb51301e9b002e7c90d..2957ca32eca3492eb0938a372b594af82856cfa8 100644 --- a/commands/command_test.go +++ b/commands/command_test.go @@ -17,21 +17,21 @@ func TestOptionValidation(t *testing.T) { opts, _ := cmd.GetOptions(nil) - req := NewRequest(nil, nil, nil, nil, opts) + req, _ := NewRequest(nil, nil, nil, nil, opts) req.SetOption("beep", true) res := cmd.Call(req) if res.Error() == nil { t.Error("Should have failed (incorrect type)") } - req = NewRequest(nil, nil, nil, nil, opts) + req, _ = NewRequest(nil, nil, nil, nil, opts) req.SetOption("beep", 5) res = cmd.Call(req) if res.Error() != nil { t.Error(res.Error(), "Should have passed") } - req = NewRequest(nil, nil, nil, nil, opts) + req, _ = NewRequest(nil, nil, nil, nil, opts) req.SetOption("beep", 5) req.SetOption("boop", "test") res = cmd.Call(req) @@ -39,7 +39,7 @@ func TestOptionValidation(t *testing.T) { t.Error("Should have passed") } - req = NewRequest(nil, nil, nil, nil, opts) + req, _ = NewRequest(nil, nil, nil, nil, opts) req.SetOption("b", 5) req.SetOption("B", "test") res = cmd.Call(req) @@ -47,28 +47,28 @@ func TestOptionValidation(t *testing.T) { t.Error("Should have passed") } - req = NewRequest(nil, nil, nil, nil, opts) + req, _ = NewRequest(nil, nil, nil, nil, opts) req.SetOption("foo", 5) res = cmd.Call(req) if res.Error() != nil { t.Error("Should have passed") } - req = NewRequest(nil, nil, nil, nil, opts) + req, _ = NewRequest(nil, nil, nil, nil, opts) req.SetOption(EncShort, "json") res = cmd.Call(req) if res.Error() != nil { t.Error("Should have passed") } - req = NewRequest(nil, nil, nil, nil, opts) + req, _ = NewRequest(nil, nil, nil, nil, opts) req.SetOption("b", "100") res = cmd.Call(req) if res.Error() != nil { t.Error("Should have passed") } - req = NewRequest(nil, nil, nil, nil, opts) + req, _ = NewRequest(nil, nil, nil, nil, opts) req.SetOption("b", ":)") res = cmd.Call(req) if res.Error() == nil { diff --git a/commands/http/parse.go b/commands/http/parse.go index a59bb3b784e8b5d520c18f61d702b22bea7405c9..23d2dad878dd9ffbd0c3f9d804ad6805e12670c0 100644 --- a/commands/http/parse.go +++ b/commands/http/parse.go @@ -94,7 +94,10 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) { return nil, err } - req := cmds.NewRequest(path, opts, args, cmd, optDefs) + req, err := cmds.NewRequest(path, opts, args, cmd, optDefs) + if err != nil { + return nil, err + } err = cmd.CheckArguments(req) if err != nil { diff --git a/commands/request.go b/commands/request.go index 5bdfe7adef582410c163e49e5049d0291eca8af3..d987770b9ad419ee64897bc3ded53146a65fad91 100644 --- a/commands/request.go +++ b/commands/request.go @@ -223,8 +223,12 @@ func (r *request) ConvertOptions() error { } val, err := convert(str) if err != nil { - return fmt.Errorf("Could not convert string value '%s' to type '%s'", - v, opt.Type.String()) + value := fmt.Sprintf("value '%v'", v) + if len(str) == 0 { + value = "empty value" + } + return fmt.Errorf("Could not convert %s to type '%s' (for option '-%s')", + value, opt.Type.String(), k) } r.options[k] = val @@ -248,12 +252,13 @@ func (r *request) ConvertOptions() error { } // NewEmptyRequest initializes an empty request -func NewEmptyRequest() Request { +func NewEmptyRequest() (Request, error) { return NewRequest(nil, nil, nil, nil, nil) } // NewRequest returns a request initialized with given arguments -func NewRequest(path []string, opts optMap, args []interface{}, cmd *Command, optDefs map[string]Option) Request { +// An non-nil error will be returned if the provided option values are invalid +func NewRequest(path []string, opts optMap, args []interface{}, cmd *Command, optDefs map[string]Option) (Request, error) { if path == nil { path = make([]string, 0) } @@ -268,7 +273,10 @@ func NewRequest(path []string, opts optMap, args []interface{}, cmd *Command, op } req := &request{path, opts, args, cmd, Context{}, optDefs} - req.ConvertOptions() + err := req.ConvertOptions() + if err != nil { + return nil, err + } - return req + return req, nil } diff --git a/commands/response_test.go b/commands/response_test.go index 6bc7417ea4b248ddf0b48da96dabe81c5af5e807..fa5afccd4fb74116695de09dfdb456f20be69ffb 100644 --- a/commands/response_test.go +++ b/commands/response_test.go @@ -15,7 +15,7 @@ func TestMarshalling(t *testing.T) { cmd := &Command{} opts, _ := cmd.GetOptions(nil) - req := NewRequest(nil, nil, nil, nil, opts) + req, _ := NewRequest(nil, nil, nil, nil, opts) res := NewResponse(req) res.SetOutput(TestOutput{"beep", "boop", 1337})