-
Notifications
You must be signed in to change notification settings - Fork 18
/
command.go
1334 lines (1176 loc) · 35.2 KB
/
command.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
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package clir
import (
"errors"
"flag"
"fmt"
"os"
"reflect"
"strconv"
"strings"
)
// Command represents a command that may be run by the user
type Command struct {
name string
commandPath string
shortdescription string
longdescription string
subCommands []*Command
subCommandsMap map[string]*Command
longestSubcommand int
actionCallback Action
app *Cli
flags *flag.FlagSet
flagCount int
helpFlag bool
hidden bool
positionalArgsMap map[string]reflect.Value
sliceSeparator map[string]string
}
// NewCommand creates a new Command
// func NewCommand(name string, description string, app *Cli, parentCommandPath string) *Command {
func NewCommand(name string, description string) *Command {
result := &Command{
name: name,
shortdescription: description,
subCommandsMap: make(map[string]*Command),
hidden: false,
positionalArgsMap: make(map[string]reflect.Value),
sliceSeparator: make(map[string]string),
}
return result
}
func (c *Command) setParentCommandPath(parentCommandPath string) {
// Set up command path
if parentCommandPath != "" {
c.commandPath += parentCommandPath + " "
}
c.commandPath += c.name
// Set up flag set
c.flags = flag.NewFlagSet(c.commandPath, flag.ContinueOnError)
c.BoolFlag("help", "Get help on the '"+strings.ToLower(c.commandPath)+"' command.", &c.helpFlag)
// result.Flags.Usage = result.PrintHelp
}
func (c *Command) inheritFlags(inheritFlags *flag.FlagSet) {
// inherit flags
inheritFlags.VisitAll(func(f *flag.Flag) {
if f.Name != "help" {
c.flags.Var(f.Value, f.Name, f.Usage)
}
})
}
func (c *Command) setApp(app *Cli) {
c.app = app
}
// parseFlags parses the given flags
func (c *Command) parseFlags(args []string) error {
// Parse flags
tmp := os.Stderr
os.Stderr = nil
defer func() {
os.Stderr = tmp
}()
// Credit: https://stackoverflow.com/a/74146375
var positionalArgs []string
for {
if err := c.flags.Parse(args); err != nil {
return err
}
// Consume all the flags that were parsed as flags.
args = args[len(args)-c.flags.NArg():]
if len(args) == 0 {
break
}
// There's at least one flag remaining and it must be a positional arg since
// we consumed all args that were parsed as flags. Consume just the first
// one, and retry parsing, since subsequent args may be flags.
positionalArgs = append(positionalArgs, args[0])
args = args[1:]
}
// Parse just the positional args so that flagset.Args()/flagset.NArgs()
// return the expected value.
// Note: This should never return an error.
err := c.flags.Parse(positionalArgs)
if err != nil {
return err
}
if len(positionalArgs) > 0 {
return c.parsePositionalArgs(positionalArgs)
}
return nil
}
// Run - Runs the Command with the given arguments
func (c *Command) run(args []string) error {
// If we have arguments, process them
if len(args) > 0 {
// Check for subcommand
subcommand := c.subCommandsMap[args[0]]
if subcommand != nil {
return subcommand.run(args[1:])
}
// Parse flags
err := c.parseFlags(args)
if err != nil {
if c.app.errorHandler != nil {
return c.app.errorHandler(c.commandPath, err)
}
return fmt.Errorf("Error: %s\nSee '%s --help' for usage", err, c.commandPath)
}
// Help takes precedence
if c.helpFlag {
c.PrintHelp()
return nil
}
}
// Do we have an action?
if c.actionCallback != nil {
return c.actionCallback()
}
// If we haven't specified a subcommand
// check for an app level default command
if c.app.defaultCommand != nil {
// Prevent recursion!
if c.app.defaultCommand != c {
// only run default command if no args passed
if len(args) == 0 {
return c.app.defaultCommand.run(args)
}
}
}
// Nothing left we can do
c.PrintHelp()
return nil
}
// Action - Define an action from this command
func (c *Command) Action(callback Action) *Command {
c.actionCallback = callback
return c
}
// PrintHelp - Output the help text for this command
func (c *Command) PrintHelp() {
c.app.PrintBanner()
commandTitle := c.commandPath
if c.shortdescription != "" {
commandTitle += " - " + c.shortdescription
}
// Ignore root command
if c.commandPath != c.name {
fmt.Println(commandTitle)
}
if c.longdescription != "" {
fmt.Println(c.longdescription + "\n")
}
if len(c.subCommands) > 0 {
fmt.Println("Available commands:")
fmt.Println("")
for _, subcommand := range c.subCommands {
if subcommand.isHidden() {
continue
}
spacer := strings.Repeat(" ", 3+c.longestSubcommand-len(subcommand.name))
isDefault := ""
if subcommand.isDefaultCommand() {
isDefault = "[default]"
}
fmt.Printf(" %s%s%s %s\n", subcommand.name, spacer, subcommand.shortdescription, isDefault)
}
fmt.Println("")
}
if c.flagCount > 0 {
fmt.Println("Flags:")
fmt.Println()
c.flags.SetOutput(os.Stdout)
c.flags.PrintDefaults()
c.flags.SetOutput(os.Stderr)
}
fmt.Println()
}
// isDefaultCommand returns true if called on the default command
func (c *Command) isDefaultCommand() bool {
return c.app.defaultCommand == c
}
// isHidden returns true if the command is a hidden command
func (c *Command) isHidden() bool {
return c.hidden
}
// Hidden hides the command from the Help system
func (c *Command) Hidden() {
c.hidden = true
}
// NewSubCommand - Creates a new subcommand
func (c *Command) NewSubCommand(name, description string) *Command {
result := NewCommand(name, description)
c.AddCommand(result)
return result
}
// AddCommand - Adds a subcommand
func (c *Command) AddCommand(command *Command) {
command.setApp(c.app)
command.setParentCommandPath(c.commandPath)
name := command.name
c.subCommands = append(c.subCommands, command)
c.subCommandsMap[name] = command
if len(name) > c.longestSubcommand {
c.longestSubcommand = len(name)
}
}
// NewSubCommandInheritFlags - Creates a new subcommand, inherits flags from command
func (c *Command) NewSubCommandInheritFlags(name, description string) *Command {
result := c.NewSubCommand(name, description)
result.inheritFlags(c.flags)
return result
}
func (c *Command) AddFlags(optionStruct interface{}) *Command {
// use reflection to determine if this is a pointer to a struct
// if not, panic
t := reflect.TypeOf(optionStruct)
// Check for a pointer to a struct
if t.Kind() != reflect.Ptr {
panic("AddFlags() requires a pointer to a struct")
}
if t.Elem().Kind() != reflect.Struct {
panic("AddFlags() requires a pointer to a struct")
}
// Iterate through the fields of the struct reading the struct tags
// and adding the flags
v := reflect.ValueOf(optionStruct).Elem()
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
fieldType := t.Elem().Field(i)
if !fieldType.IsExported() {
continue
}
// If this is an embedded struct, recurse
if fieldType.Type.Kind() == reflect.Struct {
c.AddFlags(field.Addr().Interface())
continue
}
tag := t.Elem().Field(i).Tag
name := tag.Get("name")
description := tag.Get("description")
defaultValue := tag.Get("default")
pos := tag.Get("pos")
sep := tag.Get("sep")
c.positionalArgsMap[pos] = field
if sep != "" {
c.sliceSeparator[pos] = sep
}
if name == "" {
name = strings.ToLower(t.Elem().Field(i).Name)
}
switch field.Kind() {
case reflect.Bool:
var defaultValueBool bool
if defaultValue != "" {
var err error
defaultValueBool, err = strconv.ParseBool(defaultValue)
if err != nil {
panic("Invalid default value for bool flag")
}
}
field.SetBool(defaultValueBool)
c.BoolFlag(name, description, field.Addr().Interface().(*bool))
case reflect.String:
if defaultValue != "" {
// set value of field to default value
field.SetString(defaultValue)
}
c.StringFlag(name, description, field.Addr().Interface().(*string))
case reflect.Int:
if defaultValue != "" {
// set value of field to default value
value, err := strconv.Atoi(defaultValue)
if err != nil {
panic("Invalid default value for int flag")
}
field.SetInt(int64(value))
}
c.IntFlag(name, description, field.Addr().Interface().(*int))
case reflect.Int8:
if defaultValue != "" {
// set value of field to default value
value, err := strconv.Atoi(defaultValue)
if err != nil {
panic("Invalid default value for int8 flag")
}
field.SetInt(int64(value))
}
c.Int8Flag(name, description, field.Addr().Interface().(*int8))
case reflect.Int16:
if defaultValue != "" {
// set value of field to default value
value, err := strconv.Atoi(defaultValue)
if err != nil {
panic("Invalid default value for int16 flag")
}
field.SetInt(int64(value))
}
c.Int16Flag(name, description, field.Addr().Interface().(*int16))
case reflect.Int32:
if defaultValue != "" {
// set value of field to default value
value, err := strconv.Atoi(defaultValue)
if err != nil {
panic("Invalid default value for int32 flag")
}
field.SetInt(int64(value))
}
c.Int32Flag(name, description, field.Addr().Interface().(*int32))
case reflect.Int64:
if defaultValue != "" {
// set value of field to default value
value, err := strconv.Atoi(defaultValue)
if err != nil {
panic("Invalid default value for int64 flag")
}
field.SetInt(int64(value))
}
c.Int64Flag(name, description, field.Addr().Interface().(*int64))
case reflect.Uint:
if defaultValue != "" {
// set value of field to default value
value, err := strconv.Atoi(defaultValue)
if err != nil {
panic("Invalid default value for uint flag")
}
field.SetUint(uint64(value))
}
c.UintFlag(name, description, field.Addr().Interface().(*uint))
case reflect.Uint8:
if defaultValue != "" {
// set value of field to default value
value, err := strconv.Atoi(defaultValue)
if err != nil {
panic("Invalid default value for uint8 flag")
}
field.SetUint(uint64(value))
}
c.Uint8Flag(name, description, field.Addr().Interface().(*uint8))
case reflect.Uint16:
if defaultValue != "" {
// set value of field to default value
value, err := strconv.Atoi(defaultValue)
if err != nil {
panic("Invalid default value for uint16 flag")
}
field.SetUint(uint64(value))
}
c.Uint16Flag(name, description, field.Addr().Interface().(*uint16))
case reflect.Uint32:
if defaultValue != "" {
// set value of field to default value
value, err := strconv.Atoi(defaultValue)
if err != nil {
panic("Invalid default value for uint32 flag")
}
field.SetUint(uint64(value))
}
c.Uint32Flag(name, description, field.Addr().Interface().(*uint32))
case reflect.Uint64:
if defaultValue != "" {
// set value of field to default value
value, err := strconv.Atoi(defaultValue)
if err != nil {
panic("Invalid default value for uint64 flag")
}
field.SetUint(uint64(value))
}
c.UInt64Flag(name, description, field.Addr().Interface().(*uint64))
case reflect.Float32:
if defaultValue != "" {
// set value of field to default value
value, err := strconv.ParseFloat(defaultValue, 64)
if err != nil {
panic("Invalid default value for float32 flag")
}
field.SetFloat(value)
}
c.Float32Flag(name, description, field.Addr().Interface().(*float32))
case reflect.Float64:
if defaultValue != "" {
// set value of field to default value
value, err := strconv.ParseFloat(defaultValue, 64)
if err != nil {
panic("Invalid default value for float64 flag")
}
field.SetFloat(value)
}
c.Float64Flag(name, description, field.Addr().Interface().(*float64))
case reflect.Slice:
c.addSliceField(field, defaultValue, sep)
c.addSliceFlags(name, description, field)
default:
if pos != "" {
println("WARNING: Unsupported type for flag: ", fieldType.Type.Kind(), name)
}
}
}
return c
}
func (c *Command) addSliceFlags(name, description string, field reflect.Value) *Command {
if field.Kind() != reflect.Slice {
panic("addSliceFlags() requires a pointer to a slice")
}
t := reflect.TypeOf(field.Addr().Interface())
if t.Kind() != reflect.Ptr {
panic("addSliceFlags() requires a pointer to a slice")
}
if t.Elem().Kind() != reflect.Slice {
panic("addSliceFlags() requires a pointer to a slice")
}
switch t.Elem().Elem().Kind() {
case reflect.Bool:
c.BoolsFlag(name, description, field.Addr().Interface().(*[]bool))
case reflect.String:
c.StringsFlag(name, description, field.Addr().Interface().(*[]string))
case reflect.Int:
c.IntsFlag(name, description, field.Addr().Interface().(*[]int))
case reflect.Int8:
c.Int8sFlag(name, description, field.Addr().Interface().(*[]int8))
case reflect.Int16:
c.Int16sFlag(name, description, field.Addr().Interface().(*[]int16))
case reflect.Int32:
c.Int32sFlag(name, description, field.Addr().Interface().(*[]int32))
case reflect.Int64:
c.Int64sFlag(name, description, field.Addr().Interface().(*[]int64))
case reflect.Uint:
c.UintsFlag(name, description, field.Addr().Interface().(*[]uint))
case reflect.Uint8:
c.Uint8sFlag(name, description, field.Addr().Interface().(*[]uint8))
case reflect.Uint16:
c.Uint16sFlag(name, description, field.Addr().Interface().(*[]uint16))
case reflect.Uint32:
c.Uint32sFlag(name, description, field.Addr().Interface().(*[]uint32))
case reflect.Uint64:
c.Uint64sFlag(name, description, field.Addr().Interface().(*[]uint64))
case reflect.Float32:
c.Float32sFlag(name, description, field.Addr().Interface().(*[]float32))
case reflect.Float64:
c.Float64sFlag(name, description, field.Addr().Interface().(*[]float64))
default:
panic(fmt.Sprintf("addSliceFlags() not supported slice type %s", t.Elem().Elem().Kind().String()))
}
return c
}
func (c *Command) addSliceField(field reflect.Value, defaultValue, separator string) *Command {
if defaultValue == "" {
return c
}
if field.Kind() != reflect.Slice {
panic("addSliceField() requires a pointer to a slice")
}
t := reflect.TypeOf(field.Addr().Interface())
if t.Kind() != reflect.Ptr {
panic("addSliceField() requires a pointer to a slice")
}
if t.Elem().Kind() != reflect.Slice {
panic("addSliceField() requires a pointer to a slice")
}
defaultSlice := []string{defaultValue}
if separator != "" {
defaultSlice = strings.Split(defaultValue, separator)
}
switch t.Elem().Elem().Kind() {
case reflect.Bool:
defaultValues := make([]bool, 0, len(defaultSlice))
for _, value := range defaultSlice {
val, err := strconv.ParseBool(value)
if err != nil {
panic("Invalid default value for bool flag")
}
defaultValues = append(defaultValues, val)
}
field.Set(reflect.ValueOf(defaultValues))
case reflect.String:
field.Set(reflect.ValueOf(defaultSlice))
case reflect.Int:
defaultValues := make([]int, 0, len(defaultSlice))
for _, value := range defaultSlice {
val, err := strconv.Atoi(value)
if err != nil {
panic("Invalid default value for int flag")
}
defaultValues = append(defaultValues, val)
}
field.Set(reflect.ValueOf(defaultValues))
case reflect.Int8:
defaultValues := make([]int8, 0, len(defaultSlice))
for _, value := range defaultSlice {
val, err := strconv.Atoi(value)
if err != nil {
panic("Invalid default value for int8 flag")
}
defaultValues = append(defaultValues, int8(val))
}
field.Set(reflect.ValueOf(defaultValues))
case reflect.Int16:
defaultValues := make([]int16, 0, len(defaultSlice))
for _, value := range defaultSlice {
val, err := strconv.Atoi(value)
if err != nil {
panic("Invalid default value for int16 flag")
}
defaultValues = append(defaultValues, int16(val))
}
field.Set(reflect.ValueOf(defaultValues))
case reflect.Int32:
defaultValues := make([]int32, 0, len(defaultSlice))
for _, value := range defaultSlice {
val, err := strconv.ParseInt(value, 10, 32)
if err != nil {
panic("Invalid default value for int32 flag")
}
defaultValues = append(defaultValues, int32(val))
}
field.Set(reflect.ValueOf(defaultValues))
case reflect.Int64:
defaultValues := make([]int64, 0, len(defaultSlice))
for _, value := range defaultSlice {
val, err := strconv.ParseInt(value, 10, 64)
if err != nil {
panic("Invalid default value for int64 flag")
}
defaultValues = append(defaultValues, val)
}
field.Set(reflect.ValueOf(defaultValues))
case reflect.Uint:
defaultValues := make([]uint, 0, len(defaultSlice))
for _, value := range defaultSlice {
val, err := strconv.Atoi(value)
if err != nil {
panic("Invalid default value for uint flag")
}
defaultValues = append(defaultValues, uint(val))
}
field.Set(reflect.ValueOf(defaultValues))
case reflect.Uint8:
defaultValues := make([]uint8, 0, len(defaultSlice))
for _, value := range defaultSlice {
val, err := strconv.Atoi(value)
if err != nil {
panic("Invalid default value for uint8 flag")
}
defaultValues = append(defaultValues, uint8(val))
}
field.Set(reflect.ValueOf(defaultValues))
case reflect.Uint16:
defaultValues := make([]uint16, 0, len(defaultSlice))
for _, value := range defaultSlice {
val, err := strconv.Atoi(value)
if err != nil {
panic("Invalid default value for uint16 flag")
}
defaultValues = append(defaultValues, uint16(val))
}
field.Set(reflect.ValueOf(defaultValues))
case reflect.Uint32:
defaultValues := make([]uint32, 0, len(defaultSlice))
for _, value := range defaultSlice {
val, err := strconv.Atoi(value)
if err != nil {
panic("Invalid default value for uint32 flag")
}
defaultValues = append(defaultValues, uint32(val))
}
field.Set(reflect.ValueOf(defaultValues))
case reflect.Uint64:
defaultValues := make([]uint64, 0, len(defaultSlice))
for _, value := range defaultSlice {
val, err := strconv.Atoi(value)
if err != nil {
panic("Invalid default value for uint64 flag")
}
defaultValues = append(defaultValues, uint64(val))
}
field.Set(reflect.ValueOf(defaultValues))
case reflect.Float32:
defaultValues := make([]float32, 0, len(defaultSlice))
for _, value := range defaultSlice {
val, err := strconv.Atoi(value)
if err != nil {
panic("Invalid default value for float32 flag")
}
defaultValues = append(defaultValues, float32(val))
}
field.Set(reflect.ValueOf(defaultValues))
case reflect.Float64:
defaultValues := make([]float64, 0, len(defaultSlice))
for _, value := range defaultSlice {
val, err := strconv.Atoi(value)
if err != nil {
panic("Invalid default value for float64 flag")
}
defaultValues = append(defaultValues, float64(val))
}
field.Set(reflect.ValueOf(defaultValues))
default:
panic(fmt.Sprintf("addSliceField() not supported slice type %s", t.Elem().Elem().Kind().String()))
}
return c
}
// BoolFlag - Adds a boolean flag to the command
func (c *Command) BoolFlag(name, description string, variable *bool) *Command {
c.flags.BoolVar(variable, name, *variable, description)
c.flagCount++
return c
}
// BoolsFlag - Adds a booleans flag to the command
func (c *Command) BoolsFlag(name, description string, variable *[]bool) *Command {
c.flags.Var(newBoolsValue(*variable, variable), name, description)
c.flagCount++
return c
}
// StringFlag - Adds a string flag to the command
func (c *Command) StringFlag(name, description string, variable *string) *Command {
c.flags.StringVar(variable, name, *variable, description)
c.flagCount++
return c
}
// StringsFlag - Adds a strings flag to the command
func (c *Command) StringsFlag(name, description string, variable *[]string) *Command {
c.flags.Var(newStringsValue(*variable, variable), name, description)
c.flagCount++
return c
}
// IntFlag - Adds an int flag to the command
func (c *Command) IntFlag(name, description string, variable *int) *Command {
c.flags.IntVar(variable, name, *variable, description)
c.flagCount++
return c
}
// IntsFlag - Adds an ints flag to the command
func (c *Command) IntsFlag(name, description string, variable *[]int) *Command {
c.flags.Var(newIntsValue(*variable, variable), name, description)
c.flagCount++
return c
}
// Int8Flag - Adds an int8 flag to the command
func (c *Command) Int8Flag(name, description string, variable *int8) *Command {
c.flags.Var(newInt8Value(*variable, variable), name, description)
c.flagCount++
return c
}
// Int8sFlag - Adds an int8 s flag to the command
func (c *Command) Int8sFlag(name, description string, variable *[]int8) *Command {
c.flags.Var(newInt8sValue(*variable, variable), name, description)
c.flagCount++
return c
}
// Int16Flag - Adds an int16 flag to the command
func (c *Command) Int16Flag(name, description string, variable *int16) *Command {
c.flags.Var(newInt16Value(*variable, variable), name, description)
c.flagCount++
return c
}
// Int16sFlag - Adds an int16s flag to the command
func (c *Command) Int16sFlag(name, description string, variable *[]int16) *Command {
c.flags.Var(newInt16sValue(*variable, variable), name, description)
c.flagCount++
return c
}
// Int32Flag - Adds an int32 flag to the command
func (c *Command) Int32Flag(name, description string, variable *int32) *Command {
c.flags.Var(newInt32Value(*variable, variable), name, description)
c.flagCount++
return c
}
// Int32sFlag - Adds an int32s flag to the command
func (c *Command) Int32sFlag(name, description string, variable *[]int32) *Command {
c.flags.Var(newInt32sValue(*variable, variable), name, description)
c.flagCount++
return c
}
// Int64Flag - Adds an int64 flag to the command
func (c *Command) Int64Flag(name, description string, variable *int64) *Command {
c.flags.Int64Var(variable, name, *variable, description)
c.flagCount++
return c
}
// Int64sFlag - Adds an int64s flag to the command
func (c *Command) Int64sFlag(name, description string, variable *[]int64) *Command {
c.flags.Var(newInt64sValue(*variable, variable), name, description)
c.flagCount++
return c
}
// UintFlag - Adds an uint flag to the command
func (c *Command) UintFlag(name, description string, variable *uint) *Command {
c.flags.UintVar(variable, name, *variable, description)
c.flagCount++
return c
}
// UintsFlag - Adds an uints flag to the command
func (c *Command) UintsFlag(name, description string, variable *[]uint) *Command {
c.flags.Var(newUintsValue(*variable, variable), name, description)
c.flagCount++
return c
}
// Uint8Flag - Adds an uint8 flag to the command
func (c *Command) Uint8Flag(name, description string, variable *uint8) *Command {
c.flags.Var(newUint8Value(*variable, variable), name, description)
c.flagCount++
return c
}
// Uint8sFlag - Adds an uint8 s flag to the command
func (c *Command) Uint8sFlag(name, description string, variable *[]uint8) *Command {
c.flags.Var(newUint8sValue(*variable, variable), name, description)
c.flagCount++
return c
}
// Uint16Flag - Adds an uint16 flag to the command
func (c *Command) Uint16Flag(name, description string, variable *uint16) *Command {
c.flags.Var(newUint16Value(*variable, variable), name, description)
c.flagCount++
return c
}
// Uint16sFlag - Adds an uint16s flag to the command
func (c *Command) Uint16sFlag(name, description string, variable *[]uint16) *Command {
c.flags.Var(newUint16sValue(*variable, variable), name, description)
c.flagCount++
return c
}
// Uint32Flag - Adds an uint32 flag to the command
func (c *Command) Uint32Flag(name, description string, variable *uint32) *Command {
c.flags.Var(newUint32Value(*variable, variable), name, description)
c.flagCount++
return c
}
// Uint32sFlag - Adds an uint32s flag to the command
func (c *Command) Uint32sFlag(name, description string, variable *[]uint32) *Command {
c.flags.Var(newUint32sValue(*variable, variable), name, description)
c.flagCount++
return c
}
// UInt64Flag - Adds an uint64 flag to the command
func (c *Command) UInt64Flag(name, description string, variable *uint64) *Command {
c.flags.Uint64Var(variable, name, *variable, description)
c.flagCount++
return c
}
// Uint64sFlag - Adds an uint64s flag to the command
func (c *Command) Uint64sFlag(name, description string, variable *[]uint64) *Command {
c.flags.Var(newUint64sValue(*variable, variable), name, description)
c.flagCount++
return c
}
// Float64Flag - Adds a float64 flag to the command
func (c *Command) Float64Flag(name, description string, variable *float64) *Command {
c.flags.Float64Var(variable, name, *variable, description)
c.flagCount++
return c
}
// Float32Flag - Adds a float32 flag to the command
func (c *Command) Float32Flag(name, description string, variable *float32) *Command {
c.flags.Var(newFloat32Value(*variable, variable), name, description)
c.flagCount++
return c
}
// Float32sFlag - Adds a float32s flag to the command
func (c *Command) Float32sFlag(name, description string, variable *[]float32) *Command {
c.flags.Var(newFloat32sValue(*variable, variable), name, description)
c.flagCount++
return c
}
// Float64sFlag - Adds a float64s flag to the command
func (c *Command) Float64sFlag(name, description string, variable *[]float64) *Command {
c.flags.Var(newFloat64sValue(*variable, variable), name, description)
c.flagCount++
return c
}
type boolsFlagVar []bool
func (f *boolsFlagVar) String() string { return fmt.Sprint([]bool(*f)) }
func (f *boolsFlagVar) Set(value string) error {
if value == "" {
*f = append(*f, false)
return nil
}
b, err := strconv.ParseBool(value)
if err != nil {
return err
}
*f = append(*f, b)
return nil
}
func (f *boolsFlagVar) IsBoolFlag() bool {
return true
}
func newBoolsValue(val []bool, p *[]bool) *boolsFlagVar {
*p = val
return (*boolsFlagVar)(p)
}
type stringsFlagVar []string
func (f *stringsFlagVar) String() string { return fmt.Sprint([]string(*f)) }
func (f *stringsFlagVar) Set(value string) error {
*f = append(*f, value)
return nil
}
func newStringsValue(val []string, p *[]string) *stringsFlagVar {
*p = val
return (*stringsFlagVar)(p)
}
type intsFlagVar []int
func (f *intsFlagVar) String() string { return fmt.Sprint([]int(*f)) }
func (f *intsFlagVar) Set(value string) error {
i, err := strconv.Atoi(value)
if err != nil {
return err
}
*f = append(*f, i)
return nil
}
func newIntsValue(val []int, p *[]int) *intsFlagVar {
*p = val
return (*intsFlagVar)(p)
}
type int8Value int8
func newInt8Value(val int8, p *int8) *int8Value {
*p = val
return (*int8Value)(p)
}
func (f *int8Value) Set(value string) error {
i, err := strconv.Atoi(value)
if err != nil {
return err
}
*f = int8Value(i)
return nil
}
func (f *int8Value) String() string { return fmt.Sprint(int8(*f)) }
type int8sFlagVar []int8
func (f *int8sFlagVar) String() string { return fmt.Sprint([]int8(*f)) }
func (f *int8sFlagVar) Set(value string) error {
i, err := strconv.Atoi(value)
if err != nil {
return err
}
*f = append(*f, int8(i))
return nil
}
func newInt8sValue(val []int8, p *[]int8) *int8sFlagVar {
*p = val
return (*int8sFlagVar)(p)
}
type int16Value int16
func newInt16Value(val int16, p *int16) *int16Value {
*p = val
return (*int16Value)(p)
}
func (f *int16Value) Set(value string) error {
i, err := strconv.Atoi(value)
if err != nil {
return err
}
*f = int16Value(i)
return nil
}
func (f *int16Value) String() string { return fmt.Sprint(int16(*f)) }
type int16sFlagVar []int16
func (f *int16sFlagVar) String() string { return fmt.Sprint([]int16(*f)) }
func (f *int16sFlagVar) Set(value string) error {
i, err := strconv.Atoi(value)
if err != nil {
return err
}
*f = append(*f, int16(i))
return nil
}
func newInt16sValue(val []int16, p *[]int16) *int16sFlagVar {
*p = val
return (*int16sFlagVar)(p)
}
type int32Value int32
func newInt32Value(val int32, p *int32) *int32Value {
*p = val
return (*int32Value)(p)
}
func (f *int32Value) Set(value string) error {
i, err := strconv.Atoi(value)
if err != nil {
return err
}
*f = int32Value(i)
return nil
}
func (f *int32Value) String() string { return fmt.Sprint(int32(*f)) }
type int32sFlagVar []int32
func (f *int32sFlagVar) String() string { return fmt.Sprint([]int32(*f)) }
func (f *int32sFlagVar) Set(value string) error {
i, err := strconv.Atoi(value)