count.go raw

   1  package pflag
   2  
   3  import "strconv"
   4  
   5  // -- count Value
   6  type countValue int
   7  
   8  func newCountValue(val int, p *int) *countValue {
   9  	*p = val
  10  	return (*countValue)(p)
  11  }
  12  
  13  func (i *countValue) Set(s string) error {
  14  	// "+1" means that no specific value was passed, so increment
  15  	if s == "+1" {
  16  		*i = countValue(*i + 1)
  17  		return nil
  18  	}
  19  	v, err := strconv.ParseInt(s, 0, 0)
  20  	*i = countValue(v)
  21  	return err
  22  }
  23  
  24  func (i *countValue) Type() string {
  25  	return "count"
  26  }
  27  
  28  func (i *countValue) String() string { return strconv.Itoa(int(*i)) }
  29  
  30  func countConv(sval string) (interface{}, error) {
  31  	i, err := strconv.Atoi(sval)
  32  	if err != nil {
  33  		return nil, err
  34  	}
  35  	return i, nil
  36  }
  37  
  38  // GetCount return the int value of a flag with the given name
  39  func (f *FlagSet) GetCount(name string) (int, error) {
  40  	val, err := f.getFlagType(name, "count", countConv)
  41  	if err != nil {
  42  		return 0, err
  43  	}
  44  	return val.(int), nil
  45  }
  46  
  47  // CountVar defines a count flag with specified name, default value, and usage string.
  48  // The argument p points to an int variable in which to store the value of the flag.
  49  // A count flag will add 1 to its value every time it is found on the command line
  50  func (f *FlagSet) CountVar(p *int, name string, usage string) {
  51  	f.CountVarP(p, name, "", usage)
  52  }
  53  
  54  // CountVarP is like CountVar only take a shorthand for the flag name.
  55  func (f *FlagSet) CountVarP(p *int, name, shorthand string, usage string) {
  56  	flag := f.VarPF(newCountValue(0, p), name, shorthand, usage)
  57  	flag.NoOptDefVal = "+1"
  58  }
  59  
  60  // CountVar like CountVar only the flag is placed on the CommandLine instead of a given flag set
  61  func CountVar(p *int, name string, usage string) {
  62  	CommandLine.CountVar(p, name, usage)
  63  }
  64  
  65  // CountVarP is like CountVar only take a shorthand for the flag name.
  66  func CountVarP(p *int, name, shorthand string, usage string) {
  67  	CommandLine.CountVarP(p, name, shorthand, usage)
  68  }
  69  
  70  // Count defines a count flag with specified name, default value, and usage string.
  71  // The return value is the address of an int variable that stores the value of the flag.
  72  // A count flag will add 1 to its value every time it is found on the command line
  73  func (f *FlagSet) Count(name string, usage string) *int {
  74  	p := new(int)
  75  	f.CountVarP(p, name, "", usage)
  76  	return p
  77  }
  78  
  79  // CountP is like Count only takes a shorthand for the flag name.
  80  func (f *FlagSet) CountP(name, shorthand string, usage string) *int {
  81  	p := new(int)
  82  	f.CountVarP(p, name, shorthand, usage)
  83  	return p
  84  }
  85  
  86  // Count defines a count flag with specified name, default value, and usage string.
  87  // The return value is the address of an int variable that stores the value of the flag.
  88  // A count flag will add 1 to its value every time it is found on the command line
  89  func Count(name string, usage string) *int {
  90  	return CommandLine.CountP(name, "", usage)
  91  }
  92  
  93  // CountP is like Count only takes a shorthand for the flag name.
  94  func CountP(name, shorthand string, usage string) *int {
  95  	return CommandLine.CountP(name, shorthand, usage)
  96  }
  97