Path: blob/main/vendor/github.com/spf13/pflag/int.go
2875 views
package pflag12import "strconv"34// -- int Value5type intValue int67func newIntValue(val int, p *int) *intValue {8*p = val9return (*intValue)(p)10}1112func (i *intValue) Set(s string) error {13v, err := strconv.ParseInt(s, 0, 64)14*i = intValue(v)15return err16}1718func (i *intValue) Type() string {19return "int"20}2122func (i *intValue) String() string { return strconv.Itoa(int(*i)) }2324func intConv(sval string) (interface{}, error) {25return strconv.Atoi(sval)26}2728// GetInt return the int value of a flag with the given name29func (f *FlagSet) GetInt(name string) (int, error) {30val, err := f.getFlagType(name, "int", intConv)31if err != nil {32return 0, err33}34return val.(int), nil35}3637// IntVar defines an int flag with specified name, default value, and usage string.38// The argument p points to an int variable in which to store the value of the flag.39func (f *FlagSet) IntVar(p *int, name string, value int, usage string) {40f.VarP(newIntValue(value, p), name, "", usage)41}4243// IntVarP is like IntVar, but accepts a shorthand letter that can be used after a single dash.44func (f *FlagSet) IntVarP(p *int, name, shorthand string, value int, usage string) {45f.VarP(newIntValue(value, p), name, shorthand, usage)46}4748// IntVar defines an int flag with specified name, default value, and usage string.49// The argument p points to an int variable in which to store the value of the flag.50func IntVar(p *int, name string, value int, usage string) {51CommandLine.VarP(newIntValue(value, p), name, "", usage)52}5354// IntVarP is like IntVar, but accepts a shorthand letter that can be used after a single dash.55func IntVarP(p *int, name, shorthand string, value int, usage string) {56CommandLine.VarP(newIntValue(value, p), name, shorthand, usage)57}5859// Int defines an int flag with specified name, default value, and usage string.60// The return value is the address of an int variable that stores the value of the flag.61func (f *FlagSet) Int(name string, value int, usage string) *int {62p := new(int)63f.IntVarP(p, name, "", value, usage)64return p65}6667// IntP is like Int, but accepts a shorthand letter that can be used after a single dash.68func (f *FlagSet) IntP(name, shorthand string, value int, usage string) *int {69p := new(int)70f.IntVarP(p, name, shorthand, value, usage)71return p72}7374// Int defines an int flag with specified name, default value, and usage string.75// The return value is the address of an int variable that stores the value of the flag.76func Int(name string, value int, usage string) *int {77return CommandLine.IntP(name, "", value, usage)78}7980// IntP is like Int, but accepts a shorthand letter that can be used after a single dash.81func IntP(name, shorthand string, value int, usage string) *int {82return CommandLine.IntP(name, shorthand, value, usage)83}848586