vanity

vanity - A tiny server for golang vanity redirects
Log | Files | Refs | README | LICENSE

int.go (2780B)


      1 package pflag
      2 
      3 import "strconv"
      4 
      5 // -- int Value
      6 type intValue int
      7 
      8 func newIntValue(val int, p *int) *intValue {
      9 	*p = val
     10 	return (*intValue)(p)
     11 }
     12 
     13 func (i *intValue) Set(s string) error {
     14 	v, err := strconv.ParseInt(s, 0, 64)
     15 	*i = intValue(v)
     16 	return err
     17 }
     18 
     19 func (i *intValue) Type() string {
     20 	return "int"
     21 }
     22 
     23 func (i *intValue) String() string { return strconv.Itoa(int(*i)) }
     24 
     25 func 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
     30 func (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.
     40 func (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.
     45 func (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.
     51 func 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.
     56 func 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.
     62 func (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.
     69 func (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.
     77 func 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.
     82 func IntP(name, shorthand string, value int, usage string) *int {
     83 	return CommandLine.IntP(name, shorthand, value, usage)
     84 }