diff options
| author | Eric Paris <[email protected]> | 2015-05-30 21:07:46 -0400 |
|---|---|---|
| committer | Eric Paris <[email protected]> | 2015-06-01 18:45:34 -0400 |
| commit | 1e0a23de9163cb0706137856f1d060f88e3f277c (patch) | |
| tree | de398763dd538847a8c32d4073ce07d5fe8bb210 /float32.go | |
| parent | 5644820622454e71517561946e3d94b9f9db6842 (diff) | |
Add new FlagSet.Get{Int,String,...} accessor functions
If I declared a bool flag named "hello" I can now call
b, err := f.GetBool("hello")
And b will hold the value of the flag
We can see this is already done in
https://github.com/codegangsta/cli/blob/bcec9b08c7e5564f7512ad7e7b03778fe1923116/context.go
If people use the codegangsta/cli
Other projects have done it themselves using pflags (what inspired this
patch)
https://github.com/GoogleCloudPlatform/kubernetes/blob/cd817aebd848facda29e0befbbd6e31bf22402e6/pkg/kubectl/cmd/util/helpers.go#L176
Lets just do it ourselves...
Diffstat (limited to 'float32.go')
| -rw-r--r-- | float32.go | 17 |
1 files changed, 17 insertions, 0 deletions
@@ -25,6 +25,23 @@ func (f *float32Value) Type() string { func (f *float32Value) String() string { return fmt.Sprintf("%v", *f) } +func float32Conv(sval string) (interface{}, error) { + v, err := strconv.ParseFloat(sval, 32) + if err != nil { + return 0, err + } + return float32(v), nil +} + +// GetFloat32 return the float32 value of a flag with the given name +func (f *FlagSet) GetFloat32(name string) (float32, error) { + val, err := f.getFlagType(name, "float32", float32Conv) + if err != nil { + return 0, err + } + return val.(float32), nil +} + // Float32Var defines a float32 flag with specified name, default value, and usage string. // The argument p points to a float32 variable in which to store the value of the flag. func (f *FlagSet) Float32Var(p *float32, name string, value float32, usage string) { |
