podman
96 строк · 2.9 Кб
1package pflag
2
3import "strconv"
4
5// -- count Value
6type countValue int
7
8func newCountValue(val int, p *int) *countValue {
9*p = val
10return (*countValue)(p)
11}
12
13func (i *countValue) Set(s string) error {
14// "+1" means that no specific value was passed, so increment
15if s == "+1" {
16*i = countValue(*i + 1)
17return nil
18}
19v, err := strconv.ParseInt(s, 0, 0)
20*i = countValue(v)
21return err
22}
23
24func (i *countValue) Type() string {
25return "count"
26}
27
28func (i *countValue) String() string { return strconv.Itoa(int(*i)) }
29
30func countConv(sval string) (interface{}, error) {
31i, err := strconv.Atoi(sval)
32if err != nil {
33return nil, err
34}
35return 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) {
40val, err := f.getFlagType(name, "count", countConv)
41if err != nil {
42return 0, err
43}
44return 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
50func (f *FlagSet) CountVar(p *int, name string, usage string) {
51f.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) {
56flag := f.VarPF(newCountValue(0, p), name, shorthand, usage)
57flag.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) {
62CommandLine.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) {
67CommandLine.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
73func (f *FlagSet) Count(name string, usage string) *int {
74p := new(int)
75f.CountVarP(p, name, "", usage)
76return 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 {
81p := new(int)
82f.CountVarP(p, name, shorthand, usage)
83return 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 {
90return 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 {
95return CommandLine.CountP(name, shorthand, usage)
96}
97