blob: aa126e44d1c83255b9283a73ef38e4068c2f5922 [file] [log] [blame]
khenaidooab1f7bd2019-11-14 14:00:27 -05001package pflag
2
3import "strconv"
4
5// -- count Value
6type countValue int
7
8func newCountValue(val int, p *int) *countValue {
9 *p = val
10 return (*countValue)(p)
11}
12
13func (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
24func (i *countValue) Type() string {
25 return "count"
26}
27
28func (i *countValue) String() string { return strconv.Itoa(int(*i)) }
29
30func 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
39func (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 evey time it is found on the command line
50func (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.
55func (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
61func 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.
66func 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 evey time it is found on the command line
73func (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.
80func (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 evey time it is found on the command line
89func 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.
94func CountP(name, shorthand string, usage string) *int {
95 return CommandLine.CountP(name, shorthand, usage)
96}