8 func newCountValue(val int, p *int) *countValue {
10 return (*countValue)(p)
13 func (i *countValue) Set(s string) error {
14 // "+1" means that no specific value was passed, so increment
16 *i = countValue(*i + 1)
19 v, err := strconv.ParseInt(s, 0, 0)
24 func (i *countValue) Type() string {
28 func (i *countValue) String() string { return strconv.Itoa(int(*i)) }
30 func countConv(sval string) (interface{}, error) {
31 i, err := strconv.Atoi(sval)
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)
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 evey 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)
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"
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)
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)
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 evey time it is found on the command line
73 func (f *FlagSet) Count(name string, usage string) *int {
75 f.CountVarP(p, name, "", usage)
79 // CountP is like Count only takes a shorthand for the flag name.
80 func (f *FlagSet) CountP(name, shorthand string, usage string) *int {
82 f.CountVarP(p, name, shorthand, usage)
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 evey time it is found on the command line
89 func Count(name string, usage string) *int {
90 return CommandLine.CountP(name, "", usage)
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)