Path: blob/main/vendor/github.com/spf13/pflag/string_to_int.go
2875 views
package pflag12import (3"bytes"4"fmt"5"strconv"6"strings"7)89// -- stringToInt Value10type stringToIntValue struct {11value *map[string]int12changed bool13}1415func newStringToIntValue(val map[string]int, p *map[string]int) *stringToIntValue {16ssv := new(stringToIntValue)17ssv.value = p18*ssv.value = val19return ssv20}2122// Format: a=1,b=223func (s *stringToIntValue) Set(val string) error {24ss := strings.Split(val, ",")25out := make(map[string]int, len(ss))26for _, pair := range ss {27kv := strings.SplitN(pair, "=", 2)28if len(kv) != 2 {29return fmt.Errorf("%s must be formatted as key=value", pair)30}31var err error32out[kv[0]], err = strconv.Atoi(kv[1])33if err != nil {34return err35}36}37if !s.changed {38*s.value = out39} else {40for k, v := range out {41(*s.value)[k] = v42}43}44s.changed = true45return nil46}4748func (s *stringToIntValue) Type() string {49return "stringToInt"50}5152func (s *stringToIntValue) String() string {53var buf bytes.Buffer54i := 055for k, v := range *s.value {56if i > 0 {57buf.WriteRune(',')58}59buf.WriteString(k)60buf.WriteRune('=')61buf.WriteString(strconv.Itoa(v))62i++63}64return "[" + buf.String() + "]"65}6667func stringToIntConv(val string) (interface{}, error) {68val = strings.Trim(val, "[]")69// An empty string would cause an empty map70if len(val) == 0 {71return map[string]int{}, nil72}73ss := strings.Split(val, ",")74out := make(map[string]int, len(ss))75for _, pair := range ss {76kv := strings.SplitN(pair, "=", 2)77if len(kv) != 2 {78return nil, fmt.Errorf("%s must be formatted as key=value", pair)79}80var err error81out[kv[0]], err = strconv.Atoi(kv[1])82if err != nil {83return nil, err84}85}86return out, nil87}8889// GetStringToInt return the map[string]int value of a flag with the given name90func (f *FlagSet) GetStringToInt(name string) (map[string]int, error) {91val, err := f.getFlagType(name, "stringToInt", stringToIntConv)92if err != nil {93return map[string]int{}, err94}95return val.(map[string]int), nil96}9798// StringToIntVar defines a string flag with specified name, default value, and usage string.99// The argument p points to a map[string]int variable in which to store the values of the multiple flags.100// The value of each argument will not try to be separated by comma101func (f *FlagSet) StringToIntVar(p *map[string]int, name string, value map[string]int, usage string) {102f.VarP(newStringToIntValue(value, p), name, "", usage)103}104105// StringToIntVarP is like StringToIntVar, but accepts a shorthand letter that can be used after a single dash.106func (f *FlagSet) StringToIntVarP(p *map[string]int, name, shorthand string, value map[string]int, usage string) {107f.VarP(newStringToIntValue(value, p), name, shorthand, usage)108}109110// StringToIntVar defines a string flag with specified name, default value, and usage string.111// The argument p points to a map[string]int variable in which to store the value of the flag.112// The value of each argument will not try to be separated by comma113func StringToIntVar(p *map[string]int, name string, value map[string]int, usage string) {114CommandLine.VarP(newStringToIntValue(value, p), name, "", usage)115}116117// StringToIntVarP is like StringToIntVar, but accepts a shorthand letter that can be used after a single dash.118func StringToIntVarP(p *map[string]int, name, shorthand string, value map[string]int, usage string) {119CommandLine.VarP(newStringToIntValue(value, p), name, shorthand, usage)120}121122// StringToInt defines a string flag with specified name, default value, and usage string.123// The return value is the address of a map[string]int variable that stores the value of the flag.124// The value of each argument will not try to be separated by comma125func (f *FlagSet) StringToInt(name string, value map[string]int, usage string) *map[string]int {126p := map[string]int{}127f.StringToIntVarP(&p, name, "", value, usage)128return &p129}130131// StringToIntP is like StringToInt, but accepts a shorthand letter that can be used after a single dash.132func (f *FlagSet) StringToIntP(name, shorthand string, value map[string]int, usage string) *map[string]int {133p := map[string]int{}134f.StringToIntVarP(&p, name, shorthand, value, usage)135return &p136}137138// StringToInt defines a string flag with specified name, default value, and usage string.139// The return value is the address of a map[string]int variable that stores the value of the flag.140// The value of each argument will not try to be separated by comma141func StringToInt(name string, value map[string]int, usage string) *map[string]int {142return CommandLine.StringToIntP(name, "", value, usage)143}144145// StringToIntP is like StringToInt, but accepts a shorthand letter that can be used after a single dash.146func StringToIntP(name, shorthand string, value map[string]int, usage string) *map[string]int {147return CommandLine.StringToIntP(name, shorthand, value, usage)148}149150151