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