1package pflag 2 3import ( 4 "fmt" 5 "strconv" 6) 7 8// -- int32 Value 9type int32Value int32 10 11func newInt32Value(val int32, p *int32) *int32Value { 12 *p = val 13 return (*int32Value)(p) 14} 15 16func (i *int32Value) Set(s string) error { 17 v, err := strconv.ParseInt(s, 0, 32) 18 *i = int32Value(v) 19 return err 20} 21 22func (i *int32Value) Type() string { 23 return "int32" 24} 25 26func (i *int32Value) String() string { return fmt.Sprintf("%v", *i) } 27 28func int32Conv(sval string) (interface{}, error) { 29 v, err := strconv.ParseInt(sval, 0, 32) 30 if err != nil { 31 return 0, err 32 } 33 return int32(v), nil 34} 35 36// GetInt32 return the int32 value of a flag with the given name 37func (f *FlagSet) GetInt32(name string) (int32, error) { 38 val, err := f.getFlagType(name, "int32", int32Conv) 39 if err != nil { 40 return 0, err 41 } 42 return val.(int32), nil 43} 44 45// Int32Var defines an int32 flag with specified name, default value, and usage string. 46// The argument p points to an int32 variable in which to store the value of the flag. 47func (f *FlagSet) Int32Var(p *int32, name string, value int32, usage string) { 48 f.VarP(newInt32Value(value, p), name, "", usage) 49} 50 51// Int32VarP is like Int32Var, but accepts a shorthand letter that can be used after a single dash. 52func (f *FlagSet) Int32VarP(p *int32, name, shorthand string, value int32, usage string) { 53 f.VarP(newInt32Value(value, p), name, shorthand, usage) 54} 55 56// Int32Var defines an int32 flag with specified name, default value, and usage string. 57// The argument p points to an int32 variable in which to store the value of the flag. 58func Int32Var(p *int32, name string, value int32, usage string) { 59 CommandLine.VarP(newInt32Value(value, p), name, "", usage) 60} 61 62// Int32VarP is like Int32Var, but accepts a shorthand letter that can be used after a single dash. 63func Int32VarP(p *int32, name, shorthand string, value int32, usage string) { 64 CommandLine.VarP(newInt32Value(value, p), name, shorthand, usage) 65} 66 67// Int32 defines an int32 flag with specified name, default value, and usage string. 68// The return value is the address of an int32 variable that stores the value of the flag. 69func (f *FlagSet) Int32(name string, value int32, usage string) *int32 { 70 p := new(int32) 71 f.Int32VarP(p, name, "", value, usage) 72 return p 73} 74 75// Int32P is like Int32, but accepts a shorthand letter that can be used after a single dash. 76func (f *FlagSet) Int32P(name, shorthand string, value int32, usage string) *int32 { 77 p := new(int32) 78 f.Int32VarP(p, name, shorthand, value, usage) 79 return p 80} 81 82// Int32 defines an int32 flag with specified name, default value, and usage string. 83// The return value is the address of an int32 variable that stores the value of the flag. 84func Int32(name string, value int32, usage string) *int32 { 85 return CommandLine.Int32P(name, "", value, usage) 86} 87 88// Int32P is like Int32, but accepts a shorthand letter that can be used after a single dash. 89func Int32P(name, shorthand string, value int32, usage string) *int32 { 90 return CommandLine.Int32P(name, shorthand, value, usage) 91} 92