Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame^] | 1 | package pflag |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | // -- stringToInt Value |
| 11 | type stringToIntValue struct { |
| 12 | value *map[string]int |
| 13 | changed bool |
| 14 | } |
| 15 | |
| 16 | func newStringToIntValue(val map[string]int, p *map[string]int) *stringToIntValue { |
| 17 | ssv := new(stringToIntValue) |
| 18 | ssv.value = p |
| 19 | *ssv.value = val |
| 20 | return ssv |
| 21 | } |
| 22 | |
| 23 | // Format: a=1,b=2 |
| 24 | func (s *stringToIntValue) Set(val string) error { |
| 25 | ss := strings.Split(val, ",") |
| 26 | out := make(map[string]int, len(ss)) |
| 27 | for _, pair := range ss { |
| 28 | kv := strings.SplitN(pair, "=", 2) |
| 29 | if len(kv) != 2 { |
| 30 | return fmt.Errorf("%s must be formatted as key=value", pair) |
| 31 | } |
| 32 | var err error |
| 33 | out[kv[0]], err = strconv.Atoi(kv[1]) |
| 34 | if err != nil { |
| 35 | return err |
| 36 | } |
| 37 | } |
| 38 | if !s.changed { |
| 39 | *s.value = out |
| 40 | } else { |
| 41 | for k, v := range out { |
| 42 | (*s.value)[k] = v |
| 43 | } |
| 44 | } |
| 45 | s.changed = true |
| 46 | return nil |
| 47 | } |
| 48 | |
| 49 | func (s *stringToIntValue) Type() string { |
| 50 | return "stringToInt" |
| 51 | } |
| 52 | |
| 53 | func (s *stringToIntValue) String() string { |
| 54 | var buf bytes.Buffer |
| 55 | i := 0 |
| 56 | for k, v := range *s.value { |
| 57 | if i > 0 { |
| 58 | buf.WriteRune(',') |
| 59 | } |
| 60 | buf.WriteString(k) |
| 61 | buf.WriteRune('=') |
| 62 | buf.WriteString(strconv.Itoa(v)) |
| 63 | i++ |
| 64 | } |
| 65 | return "[" + buf.String() + "]" |
| 66 | } |
| 67 | |
| 68 | func stringToIntConv(val string) (interface{}, error) { |
| 69 | val = strings.Trim(val, "[]") |
| 70 | // An empty string would cause an empty map |
| 71 | if len(val) == 0 { |
| 72 | return map[string]int{}, nil |
| 73 | } |
| 74 | ss := strings.Split(val, ",") |
| 75 | out := make(map[string]int, len(ss)) |
| 76 | for _, pair := range ss { |
| 77 | kv := strings.SplitN(pair, "=", 2) |
| 78 | if len(kv) != 2 { |
| 79 | return nil, fmt.Errorf("%s must be formatted as key=value", pair) |
| 80 | } |
| 81 | var err error |
| 82 | out[kv[0]], err = strconv.Atoi(kv[1]) |
| 83 | if err != nil { |
| 84 | return nil, err |
| 85 | } |
| 86 | } |
| 87 | return out, nil |
| 88 | } |
| 89 | |
| 90 | // GetStringToInt return the map[string]int value of a flag with the given name |
| 91 | func (f *FlagSet) GetStringToInt(name string) (map[string]int, error) { |
| 92 | val, err := f.getFlagType(name, "stringToInt", stringToIntConv) |
| 93 | if err != nil { |
| 94 | return map[string]int{}, err |
| 95 | } |
| 96 | return val.(map[string]int), nil |
| 97 | } |
| 98 | |
| 99 | // StringToIntVar defines a string flag with specified name, default value, and usage string. |
| 100 | // The argument p points to a map[string]int variable in which to store the values of the multiple flags. |
| 101 | // The value of each argument will not try to be separated by comma |
| 102 | func (f *FlagSet) StringToIntVar(p *map[string]int, name string, value map[string]int, usage string) { |
| 103 | f.VarP(newStringToIntValue(value, p), name, "", usage) |
| 104 | } |
| 105 | |
| 106 | // StringToIntVarP is like StringToIntVar, but accepts a shorthand letter that can be used after a single dash. |
| 107 | func (f *FlagSet) StringToIntVarP(p *map[string]int, name, shorthand string, value map[string]int, usage string) { |
| 108 | f.VarP(newStringToIntValue(value, p), name, shorthand, usage) |
| 109 | } |
| 110 | |
| 111 | // StringToIntVar defines a string flag with specified name, default value, and usage string. |
| 112 | // The argument p points to a map[string]int variable in which to store the value of the flag. |
| 113 | // The value of each argument will not try to be separated by comma |
| 114 | func StringToIntVar(p *map[string]int, name string, value map[string]int, usage string) { |
| 115 | CommandLine.VarP(newStringToIntValue(value, p), name, "", usage) |
| 116 | } |
| 117 | |
| 118 | // StringToIntVarP is like StringToIntVar, but accepts a shorthand letter that can be used after a single dash. |
| 119 | func StringToIntVarP(p *map[string]int, name, shorthand string, value map[string]int, usage string) { |
| 120 | CommandLine.VarP(newStringToIntValue(value, p), name, shorthand, usage) |
| 121 | } |
| 122 | |
| 123 | // StringToInt defines a string flag with specified name, default value, and usage string. |
| 124 | // The return value is the address of a map[string]int variable that stores the value of the flag. |
| 125 | // The value of each argument will not try to be separated by comma |
| 126 | func (f *FlagSet) StringToInt(name string, value map[string]int, usage string) *map[string]int { |
| 127 | p := map[string]int{} |
| 128 | f.StringToIntVarP(&p, name, "", value, usage) |
| 129 | return &p |
| 130 | } |
| 131 | |
| 132 | // StringToIntP is like StringToInt, but accepts a shorthand letter that can be used after a single dash. |
| 133 | func (f *FlagSet) StringToIntP(name, shorthand string, value map[string]int, usage string) *map[string]int { |
| 134 | p := map[string]int{} |
| 135 | f.StringToIntVarP(&p, name, shorthand, value, usage) |
| 136 | return &p |
| 137 | } |
| 138 | |
| 139 | // StringToInt defines a string flag with specified name, default value, and usage string. |
| 140 | // The return value is the address of a map[string]int variable that stores the value of the flag. |
| 141 | // The value of each argument will not try to be separated by comma |
| 142 | func StringToInt(name string, value map[string]int, usage string) *map[string]int { |
| 143 | return CommandLine.StringToIntP(name, "", value, usage) |
| 144 | } |
| 145 | |
| 146 | // StringToIntP is like StringToInt, but accepts a shorthand letter that can be used after a single dash. |
| 147 | func StringToIntP(name, shorthand string, value map[string]int, usage string) *map[string]int { |
| 148 | return CommandLine.StringToIntP(name, shorthand, value, usage) |
| 149 | } |