forked from cornfeedhobo/pflag
-
Notifications
You must be signed in to change notification settings - Fork 1
/
string_to_string.go
129 lines (107 loc) · 3.78 KB
/
string_to_string.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// 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"
)
// -- stringToString Value
type stringToStringValue struct {
value *map[string]string
changed bool
valueOptional bool
}
var _ Value = (*stringToStringValue)(nil)
var _ Getter = (*stringToStringValue)(nil)
var _ Typed = (*stringToStringValue)(nil)
func newStringToStringValue(val map[string]string, p *map[string]string) *stringToStringValue {
ssv := new(stringToStringValue)
ssv.value = p
*ssv.value = val
return ssv
}
func (s *stringToStringValue) Set(val string) error {
kv := strings.SplitN(val, "=", 2)
if !s.valueOptional && len(kv) != 2 {
return fmt.Errorf("%q must be formatted as key=value", val)
}
key := kv[0]
val = ""
if len(kv) == 2 {
val = kv[1]
}
if !s.changed {
*s.value = map[string]string{}
}
(*s.value)[key] = val
s.changed = true
return nil
}
func (s *stringToStringValue) Get() interface{} {
return *s.value
}
func (s *stringToStringValue) Type() string {
return "stringToString"
}
func (s *stringToStringValue) String() string {
records := make([]string, 0, len(*s.value)>>1)
for k, v := range *s.value {
records = append(records, k+"="+strconv.Quote(v))
}
return fmt.Sprintf("%s", records)
}
// GetStringToString return the map[string]string value of a flag with the given name
func (fs *FlagSet) GetStringToString(name string) (map[string]string, error) {
val, err := fs.getFlagValue(name, "stringToString")
if err != nil {
return map[string]string{}, err
}
return val.(map[string]string), nil
}
// MustGetStringToString is like GetStringToString, but panics on error.
func (fs *FlagSet) MustGetStringToString(name string) map[string]string {
val, err := fs.GetStringToString(name)
if err != nil {
panic(err)
}
return val
}
// StringToStringVar defines a map[string]string flag with specified name, default value, and usage string.
// The argument p points to a map[string]string variable in which to store the values of multiple flags.
func (fs *FlagSet) StringToStringVar(p *map[string]string, name string, value map[string]string, usage string, opts ...Opt) {
fs.Var(newStringToStringValue(value, p), name, usage, opts...)
}
// StringToStringVar defines a map[string]string flag with specified name, default value, and usage string.
// The argument p points to a map[string]string variable in which to store the values of multiple flags.
func StringToStringVar(p *map[string]string, name string, value map[string]string, usage string, opts ...Opt) {
CommandLine.StringToStringVar(p, name, value, usage, opts...)
}
// StringToString defines a map[string]string flag with specified name, default value, and usage string.
// The return value is the address of a map[string]string variable that stores the values of multiple flags.
func (fs *FlagSet) StringToString(name string, value map[string]string, usage string, opts ...Opt) *map[string]string {
var p map[string]string
fs.StringToStringVar(&p, name, value, usage, opts...)
return &p
}
// StringToString defines a map[string]string flag with specified name, default value, and usage string.
// The return value is the address of a map[string]string variable that stores the values of multiple flags.
func StringToString(name string, value map[string]string, usage string, opts ...Opt) *map[string]string {
return CommandLine.StringToString(name, value, usage, opts...)
}
func OptMapValueOptional() Opt {
return func(f *Flag) error {
switch v := f.Value.(type) {
case *stringToStringValue:
v.valueOptional = true
return nil
case *stringToIntValue:
v.valueOptional = true
return nil
case *stringToInt64Value:
v.valueOptional = true
return nil
}
return fmt.Errorf("value of type %T cannot be optional", f.Value)
}
}