From 1e0a23de9163cb0706137856f1d060f88e3f277c Mon Sep 17 00:00:00 2001 From: Eric Paris Date: Sat, 30 May 2015 21:07:46 -0400 Subject: 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... --- int8.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'int8.go') diff --git a/int8.go b/int8.go index aab1022..c51cb4f 100644 --- a/int8.go +++ b/int8.go @@ -25,6 +25,23 @@ func (i *int8Value) Type() string { func (i *int8Value) String() string { return fmt.Sprintf("%v", *i) } +func int8Conv(sval string) (interface{}, error) { + v, err := strconv.ParseInt(sval, 0, 8) + if err != nil { + return 0, err + } + return int8(v), nil +} + +// GetInt8 return the int8 value of a flag with the given name +func (f *FlagSet) GetInt8(name string) (int8, error) { + val, err := f.getFlagType(name, "int8", int8Conv) + if err != nil { + return 0, err + } + return val.(int8), nil +} + // Int8Var defines an int8 flag with specified name, default value, and usage string. // The argument p points to an int8 variable in which to store the value of the flag. func (f *FlagSet) Int8Var(p *int8, name string, value int8, usage string) { -- cgit v1.2.3