fix arg parsing

how did this even work before
master
Jordan Orelli 10 years ago
parent 0a81d0efd8
commit 844a6e2eeb

@ -7,13 +7,13 @@ import (
"strings"
)
func parseArgs(args []string, dest interface{}) (map[string]interface{}, error) {
func parseArgs(args []string, dest interface{}) (*Object, error) {
reqs, err := requirements(dest)
if err != nil {
return nil, fmt.Errorf("unable to parse args: bad requirements: %s", err)
}
out := make(map[string]interface{})
out := Object{items: make(map[string]interface{})}
shorts := make(map[string]req, len(reqs))
longs := make(map[string]req, len(reqs))
for _, req := range reqs {
@ -57,7 +57,7 @@ func parseArgs(args []string, dest interface{}) (map[string]interface{}, error)
return nil, fmt.Errorf("unrecognized long opt: %s", key)
}
if req.t.Kind() == reflect.Bool {
out[req.name] = true
out.items[req.name] = true
continue
}
@ -66,7 +66,7 @@ func parseArgs(args []string, dest interface{}) (map[string]interface{}, error)
if err != nil {
return nil, fmt.Errorf("unable to parse cli argument %s: %s", key, err)
}
out[req.name] = d.items[key]
out.items[req.name] = d.items[key]
} else if strings.HasPrefix(arg, "-") {
arg = strings.TrimPrefix(arg, "-")
if strings.ContainsRune(arg, '=') {
@ -86,7 +86,7 @@ func parseArgs(args []string, dest interface{}) (map[string]interface{}, error)
if err != nil {
return nil, fmt.Errorf("unable to parse cli argument %c: %s", runes[0], err)
}
out[req.name] = d.items["key"]
out.items[req.name] = d.items["key"]
} else {
runes := []rune(arg)
for j := 0; j < len(runes); j++ {
@ -96,7 +96,7 @@ func parseArgs(args []string, dest interface{}) (map[string]interface{}, error)
return nil, fmt.Errorf("unrecognized short opt: %c", r)
}
if req.t.Kind() == reflect.Bool {
out[req.name] = true
out.items[req.name] = true
continue
}
if j != len(runes)-1 {
@ -114,14 +114,14 @@ func parseArgs(args []string, dest interface{}) (map[string]interface{}, error)
if err != nil {
return nil, fmt.Errorf("error parsing cli arg %s: %s", req.name, err)
}
out[req.name] = d.items["key"]
out.items[req.name] = d.items["key"]
}
}
} else {
break
}
}
return out, nil
return &out, nil
}
func showHelp(dest interface{}) {

@ -21,23 +21,23 @@ func TestArgs(t *testing.T) {
return
}
if vals["host"] != "example.com" {
t.Errorf("expected host 'example.com', saw host '%s'", vals["host"])
if vals.items["host"] != "example.com" {
t.Errorf("expected host 'example.com', saw host '%s'", vals.items["host"])
}
if vals["port"] != 9000 {
t.Errorf("expected port 9000, saw port %d", vals["port"])
if vals.items["port"] != 9000 {
t.Errorf("expected port 9000, saw port %d", vals.items["port"])
}
if vals["user"] != "fart" {
t.Errorf("expected user 'fart', saw user '%s'", vals["user"])
if vals.items["user"] != "fart" {
t.Errorf("expected user 'fart', saw user '%s'", vals.items["user"])
}
if vals["zip"] != true {
if vals.items["zip"] != true {
t.Errorf("expected zip to be true, is false")
}
if vals["verbose"] != true {
if vals.items["verbose"] != true {
t.Errorf("expected verbose to be true, is false")
}
}

@ -452,15 +452,15 @@ func (o *objectNode) pretty(w io.Writer, prefix string) error {
}
func (o *objectNode) eval(ctx *context) (interface{}, error) {
out := make(map[string]interface{}, len(*o))
out := Object{items: make(map[string]interface{}, len(*o))}
for name, node := range *o {
v, err := node.eval(ctx)
if err != nil {
return nil, err
}
out[name] = v
out.items[name] = v
}
return out, nil
return &out, nil
}
type variableNode struct {

@ -98,7 +98,7 @@ func (o *Object) Get(path string, dest interface{}) error {
var v interface{}
parts := strings.Split(path, "/")
v, err := seekValue(path, parts, o.items)
v, err := seekValue(path, parts, o)
if err != nil {
return err
}
@ -186,12 +186,12 @@ func seekValue(fullpath string, parts []string, root interface{}) (interface{},
return seekValue(fullpath, tail, v)
}
m, ok := root.(map[string]interface{})
m, ok := root.(*Object)
if !ok {
return nil, fmt.Errorf("can only key a map[string]interface{}, root is %v", reflect.TypeOf(root))
return nil, fmt.Errorf("can only key an Object, root is %v", reflect.TypeOf(root))
}
v, ok := m[head]
v, ok := m.items[head]
if !ok {
return nil, NoValue{fullpath, head}
}

@ -106,7 +106,7 @@ func Parse(dest interface{}) *Object {
obj = &Object{items: make(map[string]interface{})}
}
for k, v := range cliArgs {
for k, v := range cliArgs.items {
obj.items[k] = v
}

@ -101,7 +101,7 @@ func requirements(dest interface{}) (map[string]req, error) {
field := dv.Type().Field(i)
req, err := field2req(field)
if err != nil {
return nil, fmt.Errorf("unable to gather requireents for field %s: %s", field.Name, err)
return nil, fmt.Errorf("unable to gather requirements for field %s: %s", field.Name, err)
}
out[field.Name] = *req
}

Loading…
Cancel
Save