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