-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflag.go
More file actions
414 lines (366 loc) · 10.1 KB
/
flag.go
File metadata and controls
414 lines (366 loc) · 10.1 KB
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package cli
import (
"fmt"
"reflect"
"strconv"
"strings"
"time"
)
// FlagSet manages command flags
type FlagSet struct {
flags []*Flag // Array storage for flags (pointers to preserve modifications)
}
// Flag represents a command flag
type Flag struct {
names []string // All names: ["port", "p"] - first is primary, rest are aliases
flagType reflect.Type // Go type of the flag
defValue interface{} // Default value
usage string // Help text
value reflect.Value // Pointer to actual variable
required bool // Whether flag is required (future)
hidden bool // Whether to hide from help (future)
set bool // Whether flag was actually set by user
}
// Getter methods
func (f *Flag) GetNames() []string {
return f.names
}
func (f *Flag) GetType() string {
if f.flagType == nil {
return "string"
}
switch f.flagType.Kind() {
case reflect.Bool:
return "bool"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if f.flagType == reflect.TypeOf(time.Duration(0)) {
return "duration"
}
return "int"
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return "uint"
case reflect.Float32, reflect.Float64:
return "float"
case reflect.String:
return "string"
case reflect.Slice:
return "array"
default:
return "string"
}
}
func (f *Flag) GetDefault() interface{} {
return f.defValue
}
func (f *Flag) GetUsage() string {
return f.usage
}
func (f *Flag) GetValue() interface{} {
if f.value.IsValid() && f.value.CanInterface() {
// Dereference pointer to get actual value
if f.value.Kind() == reflect.Ptr && !f.value.IsNil() {
return f.value.Elem().Interface()
}
return f.value.Interface()
}
return nil
}
func (f *Flag) IsRequired() bool {
return f.required
}
func (f *Flag) IsHidden() bool {
return f.hidden
}
func (f *Flag) IsSet() bool {
return f.set
}
// Helper methods
func (f *Flag) PrimaryName() string {
if len(f.names) > 0 {
return f.names[0]
}
return ""
}
func (f *Flag) ShortName() string {
if len(f.names) > 1 {
return f.names[1]
}
return ""
}
func (f *Flag) HasName(name string) bool {
for _, n := range f.names {
if n == name {
return true
}
}
return false
}
// Setter for value (internal use)
func (f *Flag) setValue(val reflect.Value) {
f.value = val
}
// NewFlagSet creates a new flag set
func NewFlagSet() *FlagSet {
return &FlagSet{
flags: []*Flag{},
}
}
// Add adds a flag to the flag set
func (fs *FlagSet) Add(ptr interface{}, name, shorthand string, defaultValue interface{}, usage string) {
flagType, err := inferType(ptr)
if err != nil {
panic(fmt.Sprintf("failed to infer flag type for %s: %v", name, err))
}
flagValue := reflect.ValueOf(ptr).Elem()
// Set default value if provided
if defaultValue != nil {
defaultVal := reflect.ValueOf(defaultValue)
if defaultVal.Type().ConvertibleTo(flagType) {
flagValue.Set(defaultVal.Convert(flagType))
}
}
// Build names array: [primary, short] if shorthand provided
names := []string{name}
if shorthand != "" {
names = append(names, shorthand)
}
flag := Flag{
names: names,
flagType: flagType,
defValue: defaultValue,
usage: usage,
value: flagValue,
required: false,
hidden: false,
}
fs.flags = append(fs.flags, &flag)
}
// Get returns a flag by name
func (fs *FlagSet) GetFlag(name string) *Flag {
for _, flag := range fs.flags {
if flag.HasName(name) {
return flag
}
}
return nil
}
// GetFlags returns all flags
func (fs *FlagSet) GetFlags() []*Flag {
return fs.flags
}
// GetAll is an alias for GetFlags for backward compatibility
func (fs *FlagSet) GetAll() []Flag {
result := make([]Flag, len(fs.flags))
for i, flag := range fs.flags {
result[i] = *flag
}
return result
}
// Parse parses command line arguments and sets flag values
func (fs *FlagSet) Parse(args []string) ([]string, error) {
remaining := make([]string, 0)
for i := 0; i < len(args); i++ {
arg := args[i]
if !strings.HasPrefix(arg, "-") {
remaining = append(remaining, arg)
continue
}
var flagName string
var flagValue string
var hasValue bool
if strings.HasPrefix(arg, "--") {
// Long flag: --flag=value or --flag (for booleans)
name := arg[2:]
if idx := strings.Index(name, "="); idx >= 0 {
flagName = name[:idx]
flagValue = name[idx+1:]
hasValue = true
} else {
flagName = name
}
} else {
// Short flag: -f=value or -f (for booleans)
name := arg[1:]
if idx := strings.Index(name, "="); idx >= 0 {
flagName = name[:idx]
flagValue = name[idx+1:]
hasValue = true
} else {
flagName = name
}
}
flag := fs.GetFlag(flagName)
if flag == nil {
return nil, fmt.Errorf("unknown flag: %s", flagName)
}
// Handle boolean flags
if flag.flagType.Kind() == reflect.Bool {
if hasValue {
// Parse boolean value: --flag=true, --flag=1, etc.
if err := fs.setValue(flag, flagValue); err != nil {
return nil, fmt.Errorf("invalid value %q for flag %s: %v", flagValue, flagName, err)
}
} else {
// Standalone boolean flag means true
flag.value.SetBool(true)
}
flag.set = true
continue
}
// Non-boolean flags MUST have a value with =
if !hasValue {
return nil, fmt.Errorf("flag %s requires a value (use --flag=value)", flagName)
}
// Parse and set the value
if err := fs.setValue(flag, flagValue); err != nil {
return nil, fmt.Errorf("invalid value %q for flag %s: %v", flagValue, flagName, err)
}
flag.set = true
}
return remaining, nil
}
// setValue parses a string value and sets it on the flag
func (fs *FlagSet) setValue(flag *Flag, value string) error {
switch flag.flagType.Kind() {
case reflect.String:
flag.value.SetString(value)
case reflect.Bool:
if val, err := strconv.ParseBool(value); err != nil {
return err
} else {
flag.value.SetBool(val)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if flag.flagType == reflect.TypeOf(time.Duration(0)) {
if dur, err := time.ParseDuration(value); err != nil {
return err
} else {
flag.value.SetInt(int64(dur))
}
} else {
if val, err := strconv.ParseInt(value, 10, flag.flagType.Bits()); err != nil {
return err
} else {
flag.value.SetInt(val)
}
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if val, err := strconv.ParseUint(value, 10, flag.flagType.Bits()); err != nil {
return err
} else {
flag.value.SetUint(val)
}
case reflect.Float32, reflect.Float64:
if val, err := strconv.ParseFloat(value, flag.flagType.Bits()); err != nil {
return err
} else {
flag.value.SetFloat(val)
}
case reflect.Slice:
if flag.flagType.Elem().Kind() == reflect.String {
// Handle string slices
currentSlice := flag.value
newSlice := reflect.Append(currentSlice, reflect.ValueOf(value))
flag.value.Set(newSlice)
} else {
return fmt.Errorf("unsupported slice type: %v", flag.flagType.Elem().Kind())
}
default:
// Try to handle custom types that implement flag.Value interface
if flag.value.Addr().Type().Implements(reflect.TypeOf((*interface{ Set(string) error })(nil)).Elem()) {
method := flag.value.Addr().MethodByName("Set")
if method.IsValid() {
result := method.Call([]reflect.Value{reflect.ValueOf(value)})
if len(result) > 0 && !result[0].IsNil() {
return result[0].Interface().(error)
}
}
} else {
return fmt.Errorf("unsupported flag type: %v", flag.flagType.Kind())
}
}
return nil
}
// BindStruct binds struct fields as flags using struct tags
func (fs *FlagSet) BindStruct(structPtr interface{}) {
v := reflect.ValueOf(structPtr)
if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct {
panic("BindStruct requires a pointer to a struct")
}
structValue := v.Elem()
structType := structValue.Type()
for i := 0; i < structType.NumField(); i++ {
field := structType.Field(i)
fieldValue := structValue.Field(i)
// Skip unexported fields
if !fieldValue.CanSet() {
continue
}
// Get the field's address for the flag binding
fieldPtr := fieldValue.Addr().Interface()
// Parse struct tag
tag := field.Tag.Get("cli")
if tag == "" {
continue
}
// Parse tag format: "name,shorthand"
parts := strings.Split(tag, ",")
name := strings.TrimSpace(parts[0])
shorthand := ""
if len(parts) > 1 {
shorthand = strings.TrimSpace(parts[1])
}
// Get usage and default from tags
usage := field.Tag.Get("usage")
defaultTag := field.Tag.Get("default")
var defaultValue interface{}
if defaultTag != "" {
defaultValue = parseDefaultValue(defaultTag, field.Type)
}
fs.Add(fieldPtr, name, shorthand, defaultValue, usage)
}
}
// parseDefaultValue parses a default value string to the appropriate type
func parseDefaultValue(value string, targetType reflect.Type) interface{} {
switch targetType.Kind() {
case reflect.String:
return value
case reflect.Bool:
if val, err := strconv.ParseBool(value); err == nil {
return val
}
return false
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if targetType == reflect.TypeOf(time.Duration(0)) {
if dur, err := time.ParseDuration(value); err == nil {
return dur
}
return time.Duration(0)
} else {
if val, err := strconv.ParseInt(value, 10, 64); err == nil {
return reflect.ValueOf(val).Convert(targetType).Interface()
}
return reflect.Zero(targetType).Interface()
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if val, err := strconv.ParseUint(value, 10, 64); err == nil {
return reflect.ValueOf(val).Convert(targetType).Interface()
}
return reflect.Zero(targetType).Interface()
case reflect.Float32, reflect.Float64:
if val, err := strconv.ParseFloat(value, 64); err == nil {
return reflect.ValueOf(val).Convert(targetType).Interface()
}
return reflect.Zero(targetType).Interface()
default:
return nil
}
}
// inferType infers the type from a pointer using reflection
func inferType(ptr interface{}) (reflect.Type, error) {
v := reflect.ValueOf(ptr)
if v.Kind() != reflect.Ptr {
return nil, fmt.Errorf("expected pointer, got %v", v.Kind())
}
return v.Elem().Type(), nil
}