1 package pflag
2
3 import "strconv"
4
5
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
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
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
48
49
50 func (f *FlagSet) CountVar(p *int, name string, usage string) {
51 f.CountVarP(p, name, "", usage)
52 }
53
54
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
61 func CountVar(p *int, name string, usage string) {
62 CommandLine.CountVar(p, name, usage)
63 }
64
65
66 func CountVarP(p *int, name, shorthand string, usage string) {
67 CommandLine.CountVarP(p, name, shorthand, usage)
68 }
69
70
71
72
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
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
87
88
89 func Count(name string, usage string) *int {
90 return CommandLine.CountP(name, "", usage)
91 }
92
93
94 func CountP(name, shorthand string, usage string) *int {
95 return CommandLine.CountP(name, shorthand, usage)
96 }
97
View as plain text