@cryptotaxi247 / kubo / commits / eebb43753

commands: Added list of option definitions to Request, so Option can lookup values by all aliases

Matt Bell committed Nov 10, 2014 at 19:23 UTC eebb43753b0eb5ad2585548fbb27a03870cad482
3 files changed +44 -12
commands/cli/parse.go
+6 -1
@@ -45,7 +45,12 @@ func Parse(input []string, roots ...*cmds.Command) (cmds.Request, *cmds.Command,
45 return nil, root, cmd, path, err
46 }
47
48 - req := cmds.NewRequest(path, opts, args, cmd)
48 + optDefs, err := root.GetOptions(path)
49 + if err != nil {
50 + return nil, root, cmd, path, err
51 + }
52 +
53 + req := cmds.NewRequest(path, opts, args, cmd, optDefs)
54
55 err = cmd.CheckArguments(req)
56 if err != nil {
commands/http/parse.go
+6 -1
@@ -90,7 +90,12 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) {
90 args = append(args, make([]interface{}, valCount-1))
91 }
92
93 - req := cmds.NewRequest(path, opts, args, cmd)
93 + optDefs, err := root.GetOptions(path)
94 + if err != nil {
95 + return nil, err
96 + }
97 +
98 + req := cmds.NewRequest(path, opts, args, cmd, optDefs)
99
100 err = cmd.CheckArguments(req)
101 if err != nil {
commands/request.go
+32 -10
@@ -33,11 +33,12 @@ type Request interface {
33 }
34
35 type request struct {
36 - path []string
37 - options optMap
38 - arguments []interface{}
39 - cmd *Command
40 - ctx Context
36 + path []string
37 + options optMap
38 + arguments []interface{}
39 + cmd *Command
40 + ctx Context
41 + optionDefs map[string]Option
42 }
43
44 // Path returns the command path of this request
@@ -47,8 +48,26 @@ func (r *request) Path() []string {
48
49 // Option returns the value of the option for given name.
50 func (r *request) Option(name string) (interface{}, bool) {
50 - val, err := r.options[name]
51 - return val, err
51 + val, found := r.options[name]
52 + if found {
53 + return val, found
54 + }
55 +
56 + // if a value isn't defined for that name, we will try to look it up by its aliases
57 +
58 + // find the option with the specified name
59 + option, found := r.optionDefs[name]
60 + if found {
61 + // try all the possible names, break if we find a value
62 + for _, n := range option.Names {
63 + val, found := r.options[n]
64 + if found {
65 + return val, found
66 + }
67 + }
68 + }
69 +
70 + return nil, false
71 }
72
73 // Options returns a copy of the option map
@@ -152,11 +171,11 @@ func (r *request) ConvertOptions(options map[string]Option) error {
171
172 // NewEmptyRequest initializes an empty request
173 func NewEmptyRequest() Request {
155 - return NewRequest(nil, nil, nil, nil)
174 + return NewRequest(nil, nil, nil, nil, nil)
175 }
176
177 // NewRequest returns a request initialized with given arguments
159 -func NewRequest(path []string, opts optMap, args []interface{}, cmd *Command) Request {
178 +func NewRequest(path []string, opts optMap, args []interface{}, cmd *Command, optDefs map[string]Option) Request {
179 if path == nil {
180 path = make([]string, 0)
181 }
@@ -166,5 +185,8 @@ func NewRequest(path []string, opts optMap, args []interface{}, cmd *Command) Re
185 if args == nil {
186 args = make([]interface{}, 0)
187 }
169 - return &request{path, opts, args, cmd, Context{}}
188 + if optDefs == nil {
189 + optDefs = make(map[string]Option)
190 + }
191 + return &request{path, opts, args, cmd, Context{}, optDefs}
192 }