-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag_values.go
359 lines (318 loc) · 9.46 KB
/
flag_values.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package cline
import (
"fmt"
"strconv"
"strings"
)
// AnyValue is an string type alias which represents
// an input value for a command flag.
type AnyValue string
// ToBool converts current flag value into `bool`.
func (v AnyValue) ToBool() (bool, error) {
return strconv.ParseBool(v.ToString())
}
// ToInt converts current flag value into `int`.
func (v AnyValue) ToInt() (int, error) {
return strconv.Atoi(v.ToString())
}
// ToString converts current flag value into `string`.
func (v AnyValue) ToString() string {
return string(v)
}
// ToStringSlice converts current flag value into a string slice.
func (v AnyValue) ToStringSlice() []string {
var strs []string
for _, s := range strings.Split(string(v), ",") {
strs = append(strs, strings.TrimSpace(s))
}
return strs
}
// FlagBoolValue represents a `bool` type flag value.
type FlagBoolValue struct {
flag FlagBool
}
// Value unwraps the plain `bool` value of the current flag.
func (v *FlagBoolValue) Value() (bool, error) {
return v.flag.flagValue.ToBool()
}
// IsProvided checks if current `bool` flag was provided from stdin.
func (v *FlagBoolValue) IsProvided() bool {
return v.flag.flagProvided
}
// IsProvidedShort checks if current `bool` flag was provided from stdin but using its short name.
func (v *FlagBoolValue) IsProvidedShort() bool {
return v.flag.flagProvided && v.flag.flagProvidedAsAlias
}
// IsProvidedLong checks if current `bool` flag was provided from stdin but using its long name.
func (v *FlagBoolValue) IsProvidedLong() bool {
return v.flag.flagProvided && !v.flag.flagProvidedAsAlias
}
// GetFlagType returns the associated flag type.
func (v *FlagBoolValue) GetFlagType() FlagBool {
return v.flag
}
// FlagIntValue represents an `int` type flag value.
type FlagIntValue struct {
flag FlagInt
}
// Value unwraps the plain `int` value of the current flag.
func (v *FlagIntValue) Value() (int, error) {
return v.flag.flagValue.ToInt()
}
// IsProvided checks if current `int` flag was provided from stdin.
func (v *FlagIntValue) IsProvided() bool {
return v.flag.flagProvided
}
// IsProvidedShort checks if current `int` flag was provided from stdin but using its short name.
func (v *FlagIntValue) IsProvidedShort() bool {
return v.flag.flagProvided && v.flag.flagProvidedAsAlias
}
// IsProvidedLong checks if current `int` flag was provided from stdin but using its long name.
func (v *FlagIntValue) IsProvidedLong() bool {
return v.flag.flagProvided && !v.flag.flagProvidedAsAlias
}
// GetFlagType returns the associated flag type.
func (v *FlagIntValue) GetFlagType() FlagInt {
return v.flag
}
// FlagStringValue represents a `string` type flag value.
type FlagStringValue struct {
flag FlagString
}
// Value unwraps the plain `string` value of the current flag.
func (v *FlagStringValue) Value() string {
return v.flag.flagValue.ToString()
}
// IsProvided checks if current `string` flag was provided from stdin.
func (v *FlagStringValue) IsProvided() bool {
return v.flag.flagProvided
}
// IsProvidedShort checks if current `string` flag was provided from stdin but using its short name.
func (v *FlagStringValue) IsProvidedShort() bool {
return v.flag.flagProvided && v.flag.flagProvidedAsAlias
}
// IsProvidedLong checks if current `string` flag was provided from stdin but using its long name.
func (v *FlagStringValue) IsProvidedLong() bool {
return v.flag.flagProvided && !v.flag.flagProvidedAsAlias
}
// GetFlagType returns the associated flag type.
func (v *FlagStringValue) GetFlagType() FlagString {
return v.flag
}
// FlagStringSliceValue represents a string slice type flag value.
type FlagStringSliceValue struct {
flag FlagStringSlice
}
// Value unwraps the plain string slice value of the current flag.
func (v *FlagStringSliceValue) Value() []string {
return v.flag.flagValue.ToStringSlice()
}
// IsProvided checks if current string slice flag was provided from stdin.
func (v *FlagStringSliceValue) IsProvided() bool {
return v.flag.flagProvided
}
// IsProvidedShort checks if current string slice flag was provided from stdin but using its short name.
func (v *FlagStringSliceValue) IsProvidedShort() bool {
return v.flag.flagProvided && v.flag.flagProvidedAsAlias
}
// IsProvidedLong checks if current string slice flag was provided from stdin but using its long name.
func (v *FlagStringSliceValue) IsProvidedLong() bool {
return v.flag.flagProvided && !v.flag.flagProvidedAsAlias
}
// GetFlagType returns the associated flag type.
func (v *FlagStringSliceValue) GetFlagType() FlagStringSlice {
return v.flag
}
// FlagValues defines list of flag values.
type FlagValues struct {
flags []Flag
}
// It finds a `Flag` by its string key in the inner list.
func (v *FlagValues) findByKey(longFlagName string) (flag Flag) {
longFlagName = strings.TrimSpace(longFlagName)
if longFlagName == "" {
return
}
for _, fl := range v.flags {
switch f := fl.(type) {
case FlagBool:
if f.Name == longFlagName {
flag = f
return
}
case FlagInt:
if f.Name == longFlagName {
flag = f
return
}
case FlagString:
if f.Name == longFlagName {
flag = f
return
}
case FlagStringSlice:
if f.Name == longFlagName {
flag = f
return
}
}
}
return
}
// It returns provided flags by specified filters.
func (v *FlagValues) getProvidedFlags(providedOnly bool, providedAliasOnly bool) (flags []Flag) {
if !providedOnly && !providedAliasOnly {
flags = v.flags
return
}
for _, fl := range v.flags {
switch f := fl.(type) {
case FlagBool:
if !f.flagProvided {
continue
}
if providedOnly {
flags = append(flags, f)
continue
}
if providedAliasOnly && f.flagProvidedAsAlias {
flags = append(flags, f)
continue
}
case FlagInt:
if !f.flagProvided {
continue
}
if providedOnly {
flags = append(flags, f)
continue
}
if providedAliasOnly && f.flagProvidedAsAlias {
flags = append(flags, f)
continue
}
case FlagString:
if !f.flagProvided {
continue
}
if providedOnly {
flags = append(flags, f)
continue
}
if providedAliasOnly && f.flagProvidedAsAlias {
flags = append(flags, f)
continue
}
case FlagStringSlice:
if !f.flagProvided {
continue
}
if providedOnly {
flags = append(flags, f)
continue
}
if providedAliasOnly && f.flagProvidedAsAlias {
flags = append(flags, f)
continue
}
}
}
return
}
// GetProvided returns all flags that were provided from stdin only.
func (v *FlagValues) GetProvided() []Flag {
return v.getProvidedFlags(true, false)
}
// GetProvidedLong returns all flags that were provided from stdin but using long names only.
func (v *FlagValues) GetProvidedLong() []Flag {
return v.getProvidedFlags(false, false)
}
// GetProvidedShort returns all flags that were provided from stdin but using short names (alias) only.
func (v *FlagValues) GetProvidedShort() []Flag {
return v.getProvidedFlags(false, true)
}
// Any gets the current flag value but ignoring its type.
// However, the resulted value is convertible into other supported types.
// And since the `AnyValue` is just an alias of built-in `string` type
// it can be easily converted too into string like `string(AnyValue)`.
func (v *FlagValues) Any(longFlagName string) AnyValue {
switch f := v.findByKey(longFlagName).(type) {
case FlagBool:
return f.flagValue
case FlagInt:
return f.flagValue
case FlagString:
return f.flagValue
case FlagStringSlice:
return f.flagValue
}
return AnyValue("")
}
// Bool gets a `bool` flag value which value type should match
// with its flag definition type, otherwise it returns an error.
func (v *FlagValues) Bool(longFlagName string) (val *FlagBoolValue, err error) {
switch f := v.findByKey(longFlagName).(type) {
case FlagBool:
val = &FlagBoolValue{flag: f}
return
default:
t := strings.ReplaceAll(fmt.Sprintf("%T", f), "cline.", "")
err = fmt.Errorf(
"flag `--%s` value used as `FlagBoolValue` but declared as `%s`",
longFlagName,
t,
)
return
}
}
// Int finds a `int` flag value which value type should match
// with its flag definition type, otherwise it returns an error.
func (v *FlagValues) Int(longFlagName string) (val *FlagIntValue, err error) {
switch f := v.findByKey(longFlagName).(type) {
case FlagInt:
val = &FlagIntValue{flag: f}
return
default:
t := strings.ReplaceAll(fmt.Sprintf("%T", f), "cline.", "")
err = fmt.Errorf(
"flag `--%s` value used as `FlagIntValue` but declared as `%s`",
longFlagName,
t,
)
return
}
}
// String finds a `string` flag value which value type should match
// with its flag definition type, otherwise it returns an error.
func (v *FlagValues) String(longFlagName string) (val *FlagStringValue, err error) {
switch f := v.findByKey(longFlagName).(type) {
case FlagString:
val = &FlagStringValue{flag: f}
return
default:
t := strings.ReplaceAll(fmt.Sprintf("%T", f), "cline.", "")
err = fmt.Errorf(
"flag `--%s` value used as `FlagStringValue` but declared as `%s`",
longFlagName,
t,
)
return
}
}
// StringSlice finds a string slice which value type should match
// with its flag definition type, otherwise it returns an error.
func (v *FlagValues) StringSlice(longFlagName string) (val *FlagStringSliceValue, err error) {
switch f := v.findByKey(longFlagName).(type) {
case FlagStringSlice:
val = &FlagStringSliceValue{flag: f}
return
default:
t := strings.ReplaceAll(fmt.Sprintf("%T", f), "cline.", "")
err = fmt.Errorf(
"flag `--%s` value used as `FlagStringSliceValue` but declared as `%s`",
longFlagName,
t,
)
return
}
}