forked from cornfeedhobo/pflag
-
Notifications
You must be signed in to change notification settings - Fork 1
/
string_to_int.go
113 lines (93 loc) · 3.29 KB
/
string_to_int.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zflag
import (
"fmt"
"strconv"
"strings"
)
// -- stringToInt Value
type stringToIntValue struct {
value *map[string]int
changed bool
valueOptional bool
}
var _ Value = (*stringToIntValue)(nil)
var _ Getter = (*stringToIntValue)(nil)
var _ Typed = (*stringToIntValue)(nil)
func newStringToIntValue(val map[string]int, p *map[string]int) *stringToIntValue {
ssv := new(stringToIntValue)
ssv.value = p
*ssv.value = val
return ssv
}
// Format: a=1
func (s *stringToIntValue) Set(val string) error {
kv := strings.SplitN(val, "=", 2)
if len(kv) != 2 {
return fmt.Errorf("%s must be formatted as key=value", val)
}
key, val := kv[0], kv[1]
val = strings.TrimSpace(val)
v, err := strconv.Atoi(val)
if err != nil {
return err
}
if !s.changed {
*s.value = map[string]int{}
}
(*s.value)[key] = v
s.changed = true
return nil
}
func (s *stringToIntValue) Get() interface{} {
return *s.value
}
func (s *stringToIntValue) Type() string {
return "stringToInt"
}
func (s *stringToIntValue) String() string {
records := make([]string, 0, len(*s.value)>>1)
for k, v := range *s.value {
records = append(records, k+"="+strconv.Itoa(v))
}
return fmt.Sprintf("%s", records)
}
// GetStringToInt return the map[string]int value of a flag with the given name
func (fs *FlagSet) GetStringToInt(name string) (map[string]int, error) {
val, err := fs.getFlagValue(name, "stringToInt")
if err != nil {
return map[string]int{}, err
}
return val.(map[string]int), nil
}
// MustGetStringToInt is like GetStringToInt, but panics on error.
func (fs *FlagSet) MustGetStringToInt(name string) map[string]int {
val, err := fs.GetStringToInt(name)
if err != nil {
panic(err)
}
return val
}
// StringToIntVar defines a map[string]int flag with specified name, default value, and usage string.
// The argument p points to a map[string]int variable in which to store the values of multiple flags.
func (fs *FlagSet) StringToIntVar(p *map[string]int, name string, value map[string]int, usage string, opts ...Opt) {
fs.Var(newStringToIntValue(value, p), name, usage, opts...)
}
// StringToIntVar defines a map[string]int flag with specified name, default value, and usage string.
// The argument p points to a map[string]int variable in which to store the values of multiple flags.
func StringToIntVar(p *map[string]int, name string, value map[string]int, usage string, opts ...Opt) {
CommandLine.StringToIntVar(p, name, value, usage, opts...)
}
// StringToInt defines a map[string]int flag with specified name, default value, and usage string.
// The return value is the address of a map[string]int variable that stores the values of multiple flags.
func (fs *FlagSet) StringToInt(name string, value map[string]int, usage string, opts ...Opt) *map[string]int {
var p map[string]int
fs.StringToIntVar(&p, name, value, usage, opts...)
return &p
}
// StringToInt defines a map[string]int flag with specified name, default value, and usage string.
// The return value is the address of a map[string]int variable that stores the values of multiple flags.
func StringToInt(name string, value map[string]int, usage string, opts ...Opt) *map[string]int {
return CommandLine.StringToInt(name, value, usage, opts...)
}