|
|
@ -2,7 +2,6 @@ package moon
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
@ -55,22 +54,69 @@ func parseArgs(args []string, dest interface{}) (map[string]interface{}, error)
|
|
|
|
|
|
|
|
|
|
|
|
req, ok := longs[key]
|
|
|
|
req, ok := longs[key]
|
|
|
|
if !ok {
|
|
|
|
if !ok {
|
|
|
|
// ignore unknown options silently?
|
|
|
|
return nil, fmt.Errorf("unrecognized long opt: %s", key)
|
|
|
|
log.Printf("no such long opt: %s", key)
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if req.t.Kind() == reflect.Bool {
|
|
|
|
if req.t.Kind() == reflect.Bool {
|
|
|
|
out[key] = true
|
|
|
|
out[req.name] = true
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// this is horrible
|
|
|
|
d, err := ReadString(fmt.Sprintf("%s: %s", key, val)) // :(
|
|
|
|
d, err := ReadString(fmt.Sprintf("%s: %s", key, val)) // :(
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to parse cli argument %s: %s", key, err)
|
|
|
|
return nil, fmt.Errorf("unable to parse cli argument %s: %s", key, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out[key] = d.items[key]
|
|
|
|
out[req.name] = d.items[key]
|
|
|
|
} else if strings.HasPrefix(arg, "-") {
|
|
|
|
} else if strings.HasPrefix(arg, "-") {
|
|
|
|
panic("i'm not doing short args yet")
|
|
|
|
arg = strings.TrimPrefix(arg, "-")
|
|
|
|
|
|
|
|
if strings.ContainsRune(arg, '=') {
|
|
|
|
|
|
|
|
runes := []rune(arg)
|
|
|
|
|
|
|
|
if len(runes) == 1 { // -=
|
|
|
|
|
|
|
|
// no clue what to do here
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("unable to parse cli arguments: weird -=?")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if runes[1] != '=' {
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("you may only use one short flag with an equals sign")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
req, ok := shorts[string(runes[0])]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("unrecognized short opt: %c", runes[0])
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
d, err := ReadString(fmt.Sprintf("key: %s", runes[2:]))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("unable to parse cli argument %c: %s", runes[0], err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
out[req.name] = d.items["key"]
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
runes := []rune(arg)
|
|
|
|
|
|
|
|
for j := 0; j < len(runes); j++ {
|
|
|
|
|
|
|
|
r := runes[j]
|
|
|
|
|
|
|
|
req, ok := shorts[string(r)]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("unrecognized short opt: %c", r)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.t.Kind() == reflect.Bool {
|
|
|
|
|
|
|
|
out[req.name] = true
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if j != len(runes)-1 {
|
|
|
|
|
|
|
|
// what a totally fucking preposterous error message
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("illegal short opt: %c: a "+
|
|
|
|
|
|
|
|
"non-boolean short flag may only appear as the"+
|
|
|
|
|
|
|
|
" terminal option in a run of short opts", r)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
i++
|
|
|
|
|
|
|
|
if i >= len(args) {
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("arg %s is missing a value", req.name)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
val := args[i]
|
|
|
|
|
|
|
|
d, err := ReadString(fmt.Sprintf("key: %s", val))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("error parsing cli arg %s: %s", req.name, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
out[req.name] = d.items["key"]
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|