Path: blob/main/vendor/github.com/spf13/pflag/string_to_int64.go
2875 views
package pflag12import (3"bytes"4"fmt"5"strconv"6"strings"7)89// -- stringToInt64 Value10type stringToInt64Value struct {11value *map[string]int6412changed bool13}1415func newStringToInt64Value(val map[string]int64, p *map[string]int64) *stringToInt64Value {16ssv := new(stringToInt64Value)17ssv.value = p18*ssv.value = val19return ssv20}2122// Format: a=1,b=223func (s *stringToInt64Value) Set(val string) error {24ss := strings.Split(val, ",")25out := make(map[string]int64, 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.ParseInt(kv[1], 10, 64)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 *stringToInt64Value) Type() string {49return "stringToInt64"50}5152func (s *stringToInt64Value) 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.FormatInt(v, 10))62i++63}64return "[" + buf.String() + "]"65}6667func stringToInt64Conv(val string) (interface{}, error) {68val = strings.Trim(val, "[]")69// An empty string would cause an empty map70if len(val) == 0 {71return map[string]int64{}, nil72}73ss := strings.Split(val, ",")74out := make(map[string]int64, 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.ParseInt(kv[1], 10, 64)82if err != nil {83return nil, err84}85}86return out, nil87}8889// GetStringToInt64 return the map[string]int64 value of a flag with the given name90func (f *FlagSet) GetStringToInt64(name string) (map[string]int64, error) {91val, err := f.getFlagType(name, "stringToInt64", stringToInt64Conv)92if err != nil {93return map[string]int64{}, err94}95return val.(map[string]int64), nil96}9798// StringToInt64Var defines a string flag with specified name, default value, and usage string.99// The argument p point64s to a map[string]int64 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) StringToInt64Var(p *map[string]int64, name string, value map[string]int64, usage string) {102f.VarP(newStringToInt64Value(value, p), name, "", usage)103}104105// StringToInt64VarP is like StringToInt64Var, but accepts a shorthand letter that can be used after a single dash.106func (f *FlagSet) StringToInt64VarP(p *map[string]int64, name, shorthand string, value map[string]int64, usage string) {107f.VarP(newStringToInt64Value(value, p), name, shorthand, usage)108}109110// StringToInt64Var defines a string flag with specified name, default value, and usage string.111// The argument p point64s to a map[string]int64 variable in which to store the value of the flag.112// The value of each argument will not try to be separated by comma113func StringToInt64Var(p *map[string]int64, name string, value map[string]int64, usage string) {114CommandLine.VarP(newStringToInt64Value(value, p), name, "", usage)115}116117// StringToInt64VarP is like StringToInt64Var, but accepts a shorthand letter that can be used after a single dash.118func StringToInt64VarP(p *map[string]int64, name, shorthand string, value map[string]int64, usage string) {119CommandLine.VarP(newStringToInt64Value(value, p), name, shorthand, usage)120}121122// StringToInt64 defines a string flag with specified name, default value, and usage string.123// The return value is the address of a map[string]int64 variable that stores the value of the flag.124// The value of each argument will not try to be separated by comma125func (f *FlagSet) StringToInt64(name string, value map[string]int64, usage string) *map[string]int64 {126p := map[string]int64{}127f.StringToInt64VarP(&p, name, "", value, usage)128return &p129}130131// StringToInt64P is like StringToInt64, but accepts a shorthand letter that can be used after a single dash.132func (f *FlagSet) StringToInt64P(name, shorthand string, value map[string]int64, usage string) *map[string]int64 {133p := map[string]int64{}134f.StringToInt64VarP(&p, name, shorthand, value, usage)135return &p136}137138// StringToInt64 defines a string flag with specified name, default value, and usage string.139// The return value is the address of a map[string]int64 variable that stores the value of the flag.140// The value of each argument will not try to be separated by comma141func StringToInt64(name string, value map[string]int64, usage string) *map[string]int64 {142return CommandLine.StringToInt64P(name, "", value, usage)143}144145// StringToInt64P is like StringToInt64, but accepts a shorthand letter that can be used after a single dash.146func StringToInt64P(name, shorthand string, value map[string]int64, usage string) *map[string]int64 {147return CommandLine.StringToInt64P(name, shorthand, value, usage)148}149150151