-
Notifications
You must be signed in to change notification settings - Fork 4
/
enum.go
283 lines (255 loc) · 6.93 KB
/
enum.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package flagvar
import (
"fmt"
"sort"
"strings"
)
// Enum is a `flag.Value` for one-of-a-fixed-set string arguments.
// The value of the `Choices` field defines the valid choices.
// If `CaseSensitive` is set to `true` (default `false`), the comparison is case-sensitive.
type Enum struct {
Choices []string
CaseSensitive bool
Value string
Text string
}
// Help returns a string suitable for inclusion in a flag help message.
func (fv *Enum) Help() string {
if fv.CaseSensitive {
return fmt.Sprintf("one of %v (case-sensitive)", fv.Choices)
}
return fmt.Sprintf("one of %v", fv.Choices)
}
// Set is flag.Value.Set
func (fv *Enum) Set(v string) error {
fv.Text = v
equal := strings.EqualFold
if fv.CaseSensitive {
equal = func(a, b string) bool { return a == b }
}
for _, c := range fv.Choices {
if equal(c, v) {
fv.Value = c
return nil
}
}
return fmt.Errorf(`"%s" must be one of [%s]`, v, strings.Join(fv.Choices, " "))
}
func (fv *Enum) String() string {
return fv.Value
}
// Enums is a `flag.Value` for one-of-a-fixed-set string arguments.
// The value of the `Choices` field defines the valid choices.
// If `CaseSensitive` is set to `true` (default `false`), the comparison is case-sensitive.
type Enums struct {
Choices []string
CaseSensitive bool
Values []string
Texts []string
}
// Help returns a string suitable for inclusion in a flag help message.
func (fv *Enums) Help() string {
if fv.CaseSensitive {
return fmt.Sprintf("one of %v (case-sensitive)", fv.Choices)
}
return fmt.Sprintf("one of %v", fv.Choices)
}
// Set is flag.Value.Set
func (fv *Enums) Set(v string) error {
equal := strings.EqualFold
if fv.CaseSensitive {
equal = func(a, b string) bool { return a == b }
}
for _, c := range fv.Choices {
if equal(c, v) {
fv.Values = append(fv.Values, c)
fv.Texts = append(fv.Texts, v)
return nil
}
}
return fmt.Errorf(`"%s" must be one of [%s]`, v, strings.Join(fv.Choices, " "))
}
func (fv *Enums) String() string {
return strings.Join(fv.Values, ",")
}
// EnumsCSV is a `flag.Value` for comma-separated enum arguments.
// The value of the `Choices` field defines the valid choices.
// If `Accumulate` is set, the values of all instances of the flag are accumulated.
// The `Separator` field is used instead of the comma when set.
// If `CaseSensitive` is set to `true` (default `false`), the comparison is case-sensitive.
type EnumsCSV struct {
Choices []string
Separator string
Accumulate bool
CaseSensitive bool
Values []string
Texts []string
}
// Help returns a string suitable for inclusion in a flag help message.
func (fv *EnumsCSV) Help() string {
separator := ","
if fv.Separator != "" {
separator = fv.Separator
}
if fv.CaseSensitive {
return fmt.Sprintf("%q-separated list of values from %v (case-sensitive)", separator, fv.Choices)
}
return fmt.Sprintf("%q-separated list of values from %v", separator, fv.Choices)
}
// Set is flag.Value.Set
func (fv *EnumsCSV) Set(v string) error {
equal := strings.EqualFold
if fv.CaseSensitive {
equal = func(a, b string) bool { return a == b }
}
separator := fv.Separator
if separator == "" {
separator = ","
}
if !fv.Accumulate {
fv.Values = fv.Values[:0]
}
parts := strings.Split(v, separator)
for _, part := range parts {
part = strings.TrimSpace(part)
var ok bool
var value string
for _, c := range fv.Choices {
if equal(c, part) {
value = c
ok = true
break
}
}
if !ok {
return fmt.Errorf(`"%s" must be one of [%s]`, v, strings.Join(fv.Choices, " "))
}
fv.Values = append(fv.Values, value)
fv.Texts = append(fv.Texts, part)
}
return nil
}
func (fv *EnumsCSV) String() string {
return strings.Join(fv.Values, ",")
}
// EnumSet is a `flag.Value` for one-of-a-fixed-set string arguments.
// Only distinct values are returned.
// The value of the `Choices` field defines the valid choices.
// If `CaseSensitive` is set to `true` (default `false`), the comparison is case-sensitive.
type EnumSet struct {
Choices []string
CaseSensitive bool
Value map[string]bool
Texts []string
}
// Help returns a string suitable for inclusion in a flag help message.
func (fv *EnumSet) Help() string {
if fv.CaseSensitive {
return fmt.Sprintf("one of %v (case-sensitive)", fv.Choices)
}
return fmt.Sprintf("one of %v", fv.Choices)
}
// Values returns a string slice of specified values.
func (fv *EnumSet) Values() (out []string) {
for v := range fv.Value {
out = append(out, v)
}
sort.Strings(out)
return
}
// Set is flag.Value.Set
func (fv *EnumSet) Set(v string) error {
equal := strings.EqualFold
if fv.CaseSensitive {
equal = func(a, b string) bool { return a == b }
}
var ok bool
for _, c := range fv.Choices {
if equal(c, v) {
v = c
ok = true
break
}
}
if !ok {
return fmt.Errorf(`"%s" must be one of [%s]`, v, strings.Join(fv.Choices, " "))
}
if fv.Value == nil {
fv.Value = make(map[string]bool)
}
fv.Value[v] = true
fv.Texts = append(fv.Texts, v)
return nil
}
func (fv *EnumSet) String() string {
return strings.Join(fv.Values(), ",")
}
// EnumSetCSV is a `flag.Value` for comma-separated enum arguments.
// Only distinct values are returned.
// The value of the `Choices` field defines the valid choices.
// If `Accumulate` is set, the values of all instances of the flag are accumulated.
// The `Separator` field is used instead of the comma when set.
// If `CaseSensitive` is set to `true` (default `false`), the comparison is case-sensitive.
type EnumSetCSV struct {
Choices []string
Separator string
Accumulate bool
CaseSensitive bool
Value map[string]bool
Texts []string
}
// Help returns a string suitable for inclusion in a flag help message.
func (fv *EnumSetCSV) Help() string {
separator := ","
if fv.Separator != "" {
separator = fv.Separator
}
if fv.CaseSensitive {
return fmt.Sprintf("%q-separated list of values from %v (case-sensitive)", separator, fv.Choices)
}
return fmt.Sprintf("%q-separated list of values from %v", separator, fv.Choices)
}
// Values returns a string slice of specified values.
func (fv *EnumSetCSV) Values() (out []string) {
for v := range fv.Value {
out = append(out, v)
}
sort.Strings(out)
return
}
// Set is flag.Value.Set
func (fv *EnumSetCSV) Set(v string) error {
equal := strings.EqualFold
if fv.CaseSensitive {
equal = func(a, b string) bool { return a == b }
}
separator := fv.Separator
if separator == "" {
separator = ","
}
if !fv.Accumulate || fv.Value == nil {
fv.Value = make(map[string]bool)
}
parts := strings.Split(v, separator)
for _, part := range parts {
part = strings.TrimSpace(part)
var ok bool
var value string
for _, c := range fv.Choices {
if equal(c, part) {
value = c
ok = true
break
}
}
if !ok {
return fmt.Errorf(`"%s" must be one of [%s]`, v, strings.Join(fv.Choices, " "))
}
fv.Value[value] = true
fv.Texts = append(fv.Texts, part)
}
return nil
}
func (fv *EnumSetCSV) String() string {
return strings.Join(fv.Values(), ",")
}