Generate flag type lookup funcs

This commit is contained in:
Dan Buch 2016-07-10 12:50:57 -04:00
parent 3a8ad862a6
commit 77f1130e65
No known key found for this signature in database
GPG Key ID: FAEF12936DD3E3EC
4 changed files with 209 additions and 167 deletions

View File

@ -3,9 +3,7 @@ package cli
import ( import (
"errors" "errors"
"flag" "flag"
"strconv"
"strings" "strings"
"time"
) )
// Context is a type that is passed through to // Context is a type that is passed through to
@ -173,156 +171,6 @@ func lookupGlobalFlagSet(name string, ctx *Context) *flag.FlagSet {
return nil return nil
} }
func lookupInt(name string, set *flag.FlagSet) int {
f := set.Lookup(name)
if f != nil {
val, err := strconv.ParseInt(f.Value.String(), 0, 64)
if err != nil {
return 0
}
return int(val)
}
return 0
}
func lookupInt64(name string, set *flag.FlagSet) int64 {
f := set.Lookup(name)
if f != nil {
val, err := strconv.ParseInt(f.Value.String(), 0, 64)
if err != nil {
return 0
}
return val
}
return 0
}
func lookupUint(name string, set *flag.FlagSet) uint {
f := set.Lookup(name)
if f != nil {
val, err := strconv.ParseUint(f.Value.String(), 0, 64)
if err != nil {
return 0
}
return uint(val)
}
return 0
}
func lookupUint64(name string, set *flag.FlagSet) uint64 {
f := set.Lookup(name)
if f != nil {
val, err := strconv.ParseUint(f.Value.String(), 0, 64)
if err != nil {
return 0
}
return val
}
return 0
}
func lookupDuration(name string, set *flag.FlagSet) time.Duration {
f := set.Lookup(name)
if f != nil {
val, err := time.ParseDuration(f.Value.String())
if err == nil {
return val
}
}
return 0
}
func lookupFloat64(name string, set *flag.FlagSet) float64 {
f := set.Lookup(name)
if f != nil {
val, err := strconv.ParseFloat(f.Value.String(), 64)
if err != nil {
return 0
}
return val
}
return 0
}
func lookupString(name string, set *flag.FlagSet) string {
f := set.Lookup(name)
if f != nil {
return f.Value.String()
}
return ""
}
func lookupStringSlice(name string, set *flag.FlagSet) []string {
f := set.Lookup(name)
if f != nil {
return (f.Value.(*StringSlice)).Value()
}
return nil
}
func lookupIntSlice(name string, set *flag.FlagSet) []int {
f := set.Lookup(name)
if f != nil {
return (f.Value.(*IntSlice)).Value()
}
return nil
}
func lookupInt64Slice(name string, set *flag.FlagSet) []int64 {
f := set.Lookup(name)
if f != nil {
return (f.Value.(*Int64Slice)).Value()
}
return nil
}
func lookupGeneric(name string, set *flag.FlagSet) interface{} {
f := set.Lookup(name)
if f != nil {
return f.Value
}
return nil
}
func lookupBool(name string, set *flag.FlagSet) bool {
f := set.Lookup(name)
if f != nil {
val, err := strconv.ParseBool(f.Value.String())
if err != nil {
return false
}
return val
}
return false
}
func lookupBoolT(name string, set *flag.FlagSet) bool {
f := set.Lookup(name)
if f != nil {
val, err := strconv.ParseBool(f.Value.String())
if err != nil {
return true
}
return val
}
return false
}
func copyFlag(name string, ff *flag.Flag, set *flag.FlagSet) { func copyFlag(name string, ff *flag.Flag, set *flag.FlagSet) {
switch ff.Value.(type) { switch ff.Value.(type) {
case *StringSlice: case *StringSlice:

View File

@ -3,25 +3,29 @@
"name": "Bool", "name": "Bool",
"type": "bool", "type": "bool",
"value": false, "value": false,
"context_default": "false" "context_default": "false",
"parser": "strconv.ParseBool(f.Value.String())"
}, },
{ {
"name": "BoolT", "name": "BoolT",
"type": "bool", "type": "bool",
"value": false, "value": false,
"doctail": " that is true by default", "doctail": " that is true by default",
"context_default": "false" "context_default": "false",
"parser": "strconv.ParseBool(f.Value.String())"
}, },
{ {
"name": "Duration", "name": "Duration",
"type": "time.Duration", "type": "time.Duration",
"doctail": " (see https://golang.org/pkg/time/#ParseDuration)", "doctail": " (see https://golang.org/pkg/time/#ParseDuration)",
"context_default": "0" "context_default": "0",
"parser": "time.ParseDuration(f.Value.String())"
}, },
{ {
"name": "Float64", "name": "Float64",
"type": "float64", "type": "float64",
"context_default": "0" "context_default": "0",
"parser": "strconv.ParseFloat(f.Value.String(), 64)"
}, },
{ {
"name": "Generic", "name": "Generic",
@ -33,47 +37,57 @@
{ {
"name": "Int64", "name": "Int64",
"type": "int64", "type": "int64",
"context_default": "0" "context_default": "0",
"parser": "strconv.ParseInt(f.Value.String(), 0, 64)"
}, },
{ {
"name": "Int", "name": "Int",
"type": "int", "type": "int",
"context_default": "0" "context_default": "0",
"parser": "strconv.ParseInt(f.Value.String(), 0, 64)",
"parser_cast": "int(parsed)"
}, },
{ {
"name": "IntSlice", "name": "IntSlice",
"type": "*IntSlice", "type": "*IntSlice",
"dest": false, "dest": false,
"context_default": "nil", "context_default": "nil",
"context_type": "[]int" "context_type": "[]int",
"parser": "(f.Value.(*IntSlice)).Value(), error(nil)"
}, },
{ {
"name": "Int64Slice", "name": "Int64Slice",
"type": "*Int64Slice", "type": "*Int64Slice",
"dest": false, "dest": false,
"context_default": "nil", "context_default": "nil",
"context_type": "[]int64" "context_type": "[]int64",
"parser": "(f.Value.(*Int64Slice)).Value(), error(nil)"
}, },
{ {
"name": "String", "name": "String",
"type": "string", "type": "string",
"context_default": "\"\"" "context_default": "\"\"",
"parser": "f.Value.String(), error(nil)"
}, },
{ {
"name": "StringSlice", "name": "StringSlice",
"type": "*StringSlice", "type": "*StringSlice",
"dest": false, "dest": false,
"context_default": "nil", "context_default": "nil",
"context_type": "[]string" "context_type": "[]string",
"parser": "(f.Value.(*StringSlice)).Value(), error(nil)"
}, },
{ {
"name": "Uint64", "name": "Uint64",
"type": "uint64", "type": "uint64",
"context_default": "0" "context_default": "0",
"parser": "strconv.ParseUint(f.Value.String(), 0, 64)"
}, },
{ {
"name": "Uint", "name": "Uint",
"type": "uint", "type": "uint",
"context_default": "0" "context_default": "0",
"parser": "strconv.ParseUint(f.Value.String(), 0, 64)",
"parser_cast": "uint(parsed)"
} }
] ]

View File

@ -1,6 +1,10 @@
package cli package cli
import "time" import (
"flag"
"strconv"
"time"
)
// WARNING: This file is generated! // WARNING: This file is generated!
@ -39,6 +43,18 @@ func (c *Context) GlobalBool(name string) bool {
return false return false
} }
func lookupBool(name string, set *flag.FlagSet) bool {
f := set.Lookup(name)
if f != nil {
parsed, err := strconv.ParseBool(f.Value.String())
if err != nil {
return false
}
return parsed
}
return false
}
// BoolTFlag is a flag with type bool that is true by default // BoolTFlag is a flag with type bool that is true by default
type BoolTFlag struct { type BoolTFlag struct {
Name string Name string
@ -74,6 +90,18 @@ func (c *Context) GlobalBoolT(name string) bool {
return false return false
} }
func lookupBoolT(name string, set *flag.FlagSet) bool {
f := set.Lookup(name)
if f != nil {
parsed, err := strconv.ParseBool(f.Value.String())
if err != nil {
return false
}
return parsed
}
return false
}
// DurationFlag is a flag with type time.Duration (see https://golang.org/pkg/time/#ParseDuration) // DurationFlag is a flag with type time.Duration (see https://golang.org/pkg/time/#ParseDuration)
type DurationFlag struct { type DurationFlag struct {
Name string Name string
@ -110,6 +138,18 @@ func (c *Context) GlobalDuration(name string) time.Duration {
return 0 return 0
} }
func lookupDuration(name string, set *flag.FlagSet) time.Duration {
f := set.Lookup(name)
if f != nil {
parsed, err := time.ParseDuration(f.Value.String())
if err != nil {
return 0
}
return parsed
}
return 0
}
// Float64Flag is a flag with type float64 // Float64Flag is a flag with type float64
type Float64Flag struct { type Float64Flag struct {
Name string Name string
@ -146,6 +186,18 @@ func (c *Context) GlobalFloat64(name string) float64 {
return 0 return 0
} }
func lookupFloat64(name string, set *flag.FlagSet) float64 {
f := set.Lookup(name)
if f != nil {
parsed, err := strconv.ParseFloat(f.Value.String(), 64)
if err != nil {
return 0
}
return parsed
}
return 0
}
// GenericFlag is a flag with type Generic // GenericFlag is a flag with type Generic
type GenericFlag struct { type GenericFlag struct {
Name string Name string
@ -181,6 +233,18 @@ func (c *Context) GlobalGeneric(name string) interface{} {
return nil return nil
} }
func lookupGeneric(name string, set *flag.FlagSet) interface{} {
f := set.Lookup(name)
if f != nil {
parsed, err := f.Value, error(nil)
if err != nil {
return nil
}
return parsed
}
return nil
}
// Int64Flag is a flag with type int64 // Int64Flag is a flag with type int64
type Int64Flag struct { type Int64Flag struct {
Name string Name string
@ -217,6 +281,18 @@ func (c *Context) GlobalInt64(name string) int64 {
return 0 return 0
} }
func lookupInt64(name string, set *flag.FlagSet) int64 {
f := set.Lookup(name)
if f != nil {
parsed, err := strconv.ParseInt(f.Value.String(), 0, 64)
if err != nil {
return 0
}
return parsed
}
return 0
}
// IntFlag is a flag with type int // IntFlag is a flag with type int
type IntFlag struct { type IntFlag struct {
Name string Name string
@ -253,6 +329,18 @@ func (c *Context) GlobalInt(name string) int {
return 0 return 0
} }
func lookupInt(name string, set *flag.FlagSet) int {
f := set.Lookup(name)
if f != nil {
parsed, err := strconv.ParseInt(f.Value.String(), 0, 64)
if err != nil {
return 0
}
return int(parsed)
}
return 0
}
// IntSliceFlag is a flag with type *IntSlice // IntSliceFlag is a flag with type *IntSlice
type IntSliceFlag struct { type IntSliceFlag struct {
Name string Name string
@ -288,6 +376,18 @@ func (c *Context) GlobalIntSlice(name string) []int {
return nil return nil
} }
func lookupIntSlice(name string, set *flag.FlagSet) []int {
f := set.Lookup(name)
if f != nil {
parsed, err := (f.Value.(*IntSlice)).Value(), error(nil)
if err != nil {
return nil
}
return parsed
}
return nil
}
// Int64SliceFlag is a flag with type *Int64Slice // Int64SliceFlag is a flag with type *Int64Slice
type Int64SliceFlag struct { type Int64SliceFlag struct {
Name string Name string
@ -323,6 +423,18 @@ func (c *Context) GlobalInt64Slice(name string) []int64 {
return nil return nil
} }
func lookupInt64Slice(name string, set *flag.FlagSet) []int64 {
f := set.Lookup(name)
if f != nil {
parsed, err := (f.Value.(*Int64Slice)).Value(), error(nil)
if err != nil {
return nil
}
return parsed
}
return nil
}
// StringFlag is a flag with type string // StringFlag is a flag with type string
type StringFlag struct { type StringFlag struct {
Name string Name string
@ -359,6 +471,18 @@ func (c *Context) GlobalString(name string) string {
return "" return ""
} }
func lookupString(name string, set *flag.FlagSet) string {
f := set.Lookup(name)
if f != nil {
parsed, err := f.Value.String(), error(nil)
if err != nil {
return ""
}
return parsed
}
return ""
}
// StringSliceFlag is a flag with type *StringSlice // StringSliceFlag is a flag with type *StringSlice
type StringSliceFlag struct { type StringSliceFlag struct {
Name string Name string
@ -394,6 +518,18 @@ func (c *Context) GlobalStringSlice(name string) []string {
return nil return nil
} }
func lookupStringSlice(name string, set *flag.FlagSet) []string {
f := set.Lookup(name)
if f != nil {
parsed, err := (f.Value.(*StringSlice)).Value(), error(nil)
if err != nil {
return nil
}
return parsed
}
return nil
}
// Uint64Flag is a flag with type uint64 // Uint64Flag is a flag with type uint64
type Uint64Flag struct { type Uint64Flag struct {
Name string Name string
@ -430,6 +566,18 @@ func (c *Context) GlobalUint64(name string) uint64 {
return 0 return 0
} }
func lookupUint64(name string, set *flag.FlagSet) uint64 {
f := set.Lookup(name)
if f != nil {
parsed, err := strconv.ParseUint(f.Value.String(), 0, 64)
if err != nil {
return 0
}
return parsed
}
return 0
}
// UintFlag is a flag with type uint // UintFlag is a flag with type uint
type UintFlag struct { type UintFlag struct {
Name string Name string
@ -465,3 +613,15 @@ func (c *Context) GlobalUint(name string) uint {
} }
return 0 return 0
} }
func lookupUint(name string, set *flag.FlagSet) uint {
f := set.Lookup(name)
if f != nil {
parsed, err := strconv.ParseUint(f.Value.String(), 0, 64)
if err != nil {
return 0
}
return uint(parsed)
}
return 0
}

View File

@ -27,7 +27,9 @@ definition keys is:
"dest": false, "dest": false,
"doctail": " which really only wraps a []float64, oh well!", "doctail": " which really only wraps a []float64, oh well!",
"context_type": "[]float64", "context_type": "[]float64",
"context_default": "nil" "context_default": "nil",
"parser": "parseVeryMuchType(f.Value.String())",
"parser_cast": "[]float64(parsed)"
} }
The meaning of each field is as follows: The meaning of each field is as follows:
@ -45,7 +47,11 @@ The meaning of each field is as follows:
context_default (string) - The literal value used as the default by the context_default (string) - The literal value used as the default by the
`*cli.Context` reader funcs when no value is `*cli.Context` reader funcs when no value is
present present
parser (string) - Literal code used to parse the flag `f`,
expected to have a return signature of
(value, error)
parser_cast (string) - Literal code used to cast the `parsed` value
returned from the `parser` code
""" """
from __future__ import print_function, unicode_literals from __future__ import print_function, unicode_literals
@ -116,6 +122,9 @@ def _write_flag_types(outfile, types):
typedef.setdefault('context_type', typedef['type']) typedef.setdefault('context_type', typedef['type'])
typedef.setdefault('dest', True) typedef.setdefault('dest', True)
typedef.setdefault('value', True) typedef.setdefault('value', True)
typedef.setdefault('parser', 'f.Value, error(nil)')
typedef.setdefault('parser_cast', 'parsed')
_fwrite(outfile, """\ _fwrite(outfile, """\
// {name}Flag is a flag with type {type}{doctail} // {name}Flag is a flag with type {type}{doctail}
@ -165,6 +174,17 @@ def _write_flag_types(outfile, types):
return {context_default} return {context_default}
}} }}
func lookup{name}(name string, set *flag.FlagSet) {context_type} {{
f := set.Lookup(name)
if f != nil {{
parsed, err := {parser}
if err != nil {{
return {context_default}
}}
return {parser_cast}
}}
return {context_default}
}}
""".format(**typedef)) """.format(**typedef))