Path: blob/main/vendor/github.com/spf13/pflag/count.go
2875 views
package pflag12import "strconv"34// -- count Value5type countValue int67func newCountValue(val int, p *int) *countValue {8*p = val9return (*countValue)(p)10}1112func (i *countValue) Set(s string) error {13// "+1" means that no specific value was passed, so increment14if s == "+1" {15*i = countValue(*i + 1)16return nil17}18v, err := strconv.ParseInt(s, 0, 0)19*i = countValue(v)20return err21}2223func (i *countValue) Type() string {24return "count"25}2627func (i *countValue) String() string { return strconv.Itoa(int(*i)) }2829func countConv(sval string) (interface{}, error) {30i, err := strconv.Atoi(sval)31if err != nil {32return nil, err33}34return i, nil35}3637// GetCount return the int value of a flag with the given name38func (f *FlagSet) GetCount(name string) (int, error) {39val, err := f.getFlagType(name, "count", countConv)40if err != nil {41return 0, err42}43return val.(int), nil44}4546// CountVar defines a count flag with specified name, default value, and usage string.47// The argument p points to an int variable in which to store the value of the flag.48// A count flag will add 1 to its value every time it is found on the command line49func (f *FlagSet) CountVar(p *int, name string, usage string) {50f.CountVarP(p, name, "", usage)51}5253// CountVarP is like CountVar only take a shorthand for the flag name.54func (f *FlagSet) CountVarP(p *int, name, shorthand string, usage string) {55flag := f.VarPF(newCountValue(0, p), name, shorthand, usage)56flag.NoOptDefVal = "+1"57}5859// CountVar like CountVar only the flag is placed on the CommandLine instead of a given flag set60func CountVar(p *int, name string, usage string) {61CommandLine.CountVar(p, name, usage)62}6364// CountVarP is like CountVar only take a shorthand for the flag name.65func CountVarP(p *int, name, shorthand string, usage string) {66CommandLine.CountVarP(p, name, shorthand, usage)67}6869// Count defines a count flag with specified name, default value, and usage string.70// The return value is the address of an int variable that stores the value of the flag.71// A count flag will add 1 to its value every time it is found on the command line72func (f *FlagSet) Count(name string, usage string) *int {73p := new(int)74f.CountVarP(p, name, "", usage)75return p76}7778// CountP is like Count only takes a shorthand for the flag name.79func (f *FlagSet) CountP(name, shorthand string, usage string) *int {80p := new(int)81f.CountVarP(p, name, shorthand, usage)82return p83}8485// Count defines a count flag with specified name, default value, and usage string.86// The return value is the address of an int variable that stores the value of the flag.87// A count flag will add 1 to its value every time it is found on the command line88func Count(name string, usage string) *int {89return CommandLine.CountP(name, "", usage)90}9192// CountP is like Count only takes a shorthand for the flag name.93func CountP(name, shorthand string, usage string) *int {94return CommandLine.CountP(name, shorthand, usage)95}969798