diff options
| author | Alex Flint <[email protected]> | 2018-01-13 14:20:00 -0800 |
|---|---|---|
| committer | Alex Flint <[email protected]> | 2018-01-13 14:20:00 -0800 |
| commit | a0df5f33915247a80a077ec48ed14a8ad5579da4 (patch) | |
| tree | 2b880c8d771cf51dc8fa8fbe3e7e96f707ee5726 /parse.go | |
| parent | 59fccacb2679dde66e0b20ed65bd26178fcbf54e (diff) | |
handle negative values
Diffstat (limited to 'parse.go')
| -rw-r--r-- | parse.go | 18 |
1 files changed, 17 insertions, 1 deletions
@@ -345,7 +345,10 @@ func process(specs []*spec, args []string) error { // if we have something like "--foo" then the value is the next argument if value == "" { - if i+1 == len(args) || isFlag(args[i+1]) { + if i+1 == len(args) { + return fmt.Errorf("missing value for %s", arg) + } + if !nextIsNumeric(spec.dest.Type(), args[i+1]) && isFlag(args[i+1]) { return fmt.Errorf("missing value for %s", arg) } value = args[i+1] @@ -387,6 +390,19 @@ func process(specs []*spec, args []string) error { return nil } +func nextIsNumeric(t reflect.Type, s string) bool { + switch t.Kind() { + case reflect.Ptr: + return nextIsNumeric(t.Elem(), s) + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Float32, reflect.Float64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: + v := reflect.New(t) + err := scalar.ParseValue(v, s) + return err == nil + default: + return false + } +} + // isFlag returns true if a token is a flag such as "-v" or "--user" but not "-" or "--" func isFlag(s string) bool { return strings.HasPrefix(s, "-") && strings.TrimLeft(s, "-") != "" |
