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