-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgoflags.go
826 lines (731 loc) · 24.5 KB
/
goflags.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
package goflags
import (
"bytes"
"flag"
"fmt"
"io"
"os"
"path"
"reflect"
"strconv"
"strings"
"text/tabwriter"
"time"
"github.com/cnf/structhash"
"github.com/google/shlex"
fileutil "github.com/projectdiscovery/utils/file"
folderutil "github.com/projectdiscovery/utils/folder"
permissionutil "github.com/projectdiscovery/utils/permission"
"golang.org/x/exp/maps"
"gopkg.in/yaml.v3"
)
var (
DisableAutoConfigMigration = false
)
// FlagSet is a list of flags for an application
type FlagSet struct {
CaseSensitive bool
Marshal bool
description string
customHelpText string
flagKeys InsertionOrderedMap
groups []groupData
CommandLine *flag.FlagSet
configFilePath string
// OtherOptionsGroupName is the name for all flags not in a group
OtherOptionsGroupName string
configOnlyKeys InsertionOrderedMap
}
type groupData struct {
name string
description string
}
type FlagData struct {
usage string
short string
long string
group string // unused unless set later
defaultValue interface{}
skipMarshal bool
field flag.Value
}
// Group sets the group for a flag data
func (flagData *FlagData) Group(name string) {
flagData.group = name
}
// NewFlagSet creates a new flagSet structure for the application
func NewFlagSet() *FlagSet {
flag.CommandLine.ErrorHandling()
return &FlagSet{
flagKeys: newInsertionOrderedMap(),
OtherOptionsGroupName: "other options",
CommandLine: flag.NewFlagSet(os.Args[0], flag.ExitOnError),
configOnlyKeys: newInsertionOrderedMap(),
}
}
func newInsertionOrderedMap() InsertionOrderedMap {
return InsertionOrderedMap{values: make(map[string]*FlagData)}
}
// Hash returns the unique hash for a flagData structure
// NOTE: Hash panics when the structure cannot be hashed.
func (flagData *FlagData) Hash() string {
hash, _ := structhash.Hash(flagData, 1)
return hash
}
// SetDescription sets the description field for a flagSet to a value.
func (flagSet *FlagSet) SetDescription(description string) {
flagSet.description = description
}
// SetCustomHelpText sets the help text for a flagSet to a value. This variable appends text to the default help text.
func (flagSet *FlagSet) SetCustomHelpText(helpText string) {
flagSet.customHelpText = helpText
}
// SetGroup sets a group with name and description for the command line options
//
// The order in which groups are passed is also kept as is, similar to flags.
func (flagSet *FlagSet) SetGroup(name, description string) {
flagSet.groups = append(flagSet.groups, groupData{name: name, description: description})
}
// MergeConfigFile reads a config file to merge values from.
func (flagSet *FlagSet) MergeConfigFile(file string) error {
return flagSet.readConfigFile(file)
}
// Parse parses the flags provided to the library.
func (flagSet *FlagSet) Parse(args ...string) error {
flagSet.CommandLine.SetOutput(os.Stdout)
flagSet.CommandLine.Usage = flagSet.usageFunc
toParse := os.Args[1:]
if len(args) > 0 {
toParse = args
}
_ = flagSet.CommandLine.Parse(toParse)
configFilePath, _ := flagSet.GetConfigFilePath()
// migrate data from old config dir to new one
// Ref: https://github.com/projectdiscovery/nuclei/issues/3576
if !DisableAutoConfigMigration {
AttemptConfigMigration()
}
// if config file doesn't exist, create one
if !fileutil.FileExists(configFilePath) {
configData := flagSet.generateDefaultConfig()
configFileDir := flagSet.GetToolConfigDir()
if !fileutil.FolderExists(configFileDir) {
_ = fileutil.CreateFolder(configFileDir)
}
return os.WriteFile(configFilePath, configData, permissionutil.ConfigFilePermission)
}
_ = flagSet.MergeConfigFile(configFilePath) // try to read default config after parsing flags
return nil
}
// AttemptConfigMigration attempts to migrate config from old config dir to new one
// migration condition
// 1. old config dir exists
// 2. new config dir doesn't exist
// 3. old config dir is not same as new config dir
func AttemptConfigMigration() {
// migration condition
// 1. old config dir exists
// 2. new config dir doesn't exist
// 3. old config dir is not same as new config dir
flagSet := FlagSet{} // dummy flagset
toolConfigDir := flagSet.GetToolConfigDir()
if toolConfigDir != oldAppConfigDir && fileutil.FolderExists(oldAppConfigDir) && !fileutil.FolderExists(toolConfigDir) {
_ = fileutil.CreateFolder(toolConfigDir)
// move old config dir to new one
_ = folderutil.SyncDirectory(oldAppConfigDir, toolConfigDir)
}
}
// generateDefaultConfig generates a default YAML config file for a flagset.
func (flagSet *FlagSet) generateDefaultConfig() []byte {
hashes := make(map[string]struct{})
configBuffer := &bytes.Buffer{}
configBuffer.WriteString("# ")
configBuffer.WriteString(path.Base(os.Args[0]))
configBuffer.WriteString(" config file\n# generated by https://github.com/projectdiscovery/goflags\n\n")
// Attempts to marshal natively if proper flag is set, in case of errors fallback to normal mechanism
if flagSet.Marshal {
flagsToMarshall := make(map[string]interface{})
flagSet.flagKeys.forEach(func(key string, data *FlagData) {
if !data.skipMarshal {
flagsToMarshall[key] = data.defaultValue
}
})
flagSetBytes, err := yaml.Marshal(flagsToMarshall)
if err == nil {
configBuffer.Write(flagSetBytes)
return configBuffer.Bytes()
}
}
flagSet.flagKeys.forEach(func(key string, data *FlagData) {
dataHash := data.Hash()
if _, ok := hashes[dataHash]; ok {
return
}
hashes[dataHash] = struct{}{}
configBuffer.WriteString("# ")
configBuffer.WriteString(strings.ToLower(data.usage))
configBuffer.WriteString("\n")
configBuffer.WriteString("#")
configBuffer.WriteString(data.long)
configBuffer.WriteString(": ")
switch dv := data.defaultValue.(type) {
case string:
configBuffer.WriteString(dv)
case flag.Value:
configBuffer.WriteString(dv.String())
case StringSlice:
configBuffer.WriteString(dv.String())
}
configBuffer.WriteString("\n\n")
})
return bytes.TrimSuffix(configBuffer.Bytes(), []byte("\n\n"))
}
// CreateGroup within the flagset
func (flagSet *FlagSet) CreateGroup(groupName, description string, flags ...*FlagData) {
flagSet.SetGroup(groupName, description)
for _, currentFlag := range flags {
currentFlag.Group(groupName)
}
}
// readConfigFile reads the config file and returns any flags
// that might have been set by the config file.
//
// Command line flags however always take precedence over config file ones.
func (flagSet *FlagSet) readConfigFile(filePath string) error {
file, err := os.Open(filePath)
if err != nil {
return err
}
defer file.Close()
data := make(map[string]interface{})
err = yaml.NewDecoder(file).Decode(&data)
if err != nil {
return err
}
flagSet.CommandLine.VisitAll(func(fl *flag.Flag) {
item, ok := data[fl.Name]
value := fl.Value.String()
if strings.EqualFold(fl.DefValue, value) && ok {
switch itemValue := item.(type) {
case string:
_ = fl.Value.Set(itemValue)
case bool:
_ = fl.Value.Set(strconv.FormatBool(itemValue))
case int:
_ = fl.Value.Set(strconv.Itoa(itemValue))
case time.Duration:
_ = fl.Value.Set(itemValue.String())
case []interface{}:
for _, v := range itemValue {
vStr, ok := v.(string)
if ok {
_ = fl.Value.Set(vStr)
}
}
}
}
})
flagSet.configOnlyKeys.forEach(func(key string, flagData *FlagData) {
item, ok := data[key]
if ok {
fl := flag.Lookup(key)
if fl == nil {
flag.Var(flagData.field, key, flagData.usage)
fl = flag.Lookup(key)
}
switch data := item.(type) {
case string:
_ = fl.Value.Set(data)
case bool:
_ = fl.Value.Set(strconv.FormatBool(data))
case int:
_ = fl.Value.Set(strconv.Itoa(data))
case []interface{}:
for _, v := range data {
vStr, ok := v.(string)
if ok {
_ = fl.Value.Set(vStr)
}
}
}
}
})
return nil
}
// VarP adds a Var flag with a shortname and longname
func (flagSet *FlagSet) VarP(field flag.Value, long, short, usage string) *FlagData {
flagData := &FlagData{
usage: usage,
long: long,
defaultValue: field,
}
if short != "" {
flagData.short = short
flagSet.CommandLine.Var(field, short, usage)
flagSet.flagKeys.Set(short, flagData)
}
flagSet.CommandLine.Var(field, long, usage)
flagSet.flagKeys.Set(long, flagData)
return flagData
}
// Var adds a Var flag with a longname
func (flagSet *FlagSet) Var(field flag.Value, long, usage string) *FlagData {
return flagSet.VarP(field, long, "", usage)
}
// StringVarEnv adds a string flag with a shortname and longname with a default value read from env variable
// with a default value fallback
func (flagSet *FlagSet) StringVarEnv(field *string, long, short, defaultValue, envName, usage string) *FlagData {
if envValue, exists := os.LookupEnv(envName); exists {
defaultValue = envValue
}
return flagSet.StringVarP(field, long, short, defaultValue, usage)
}
// StringVarP adds a string flag with a shortname and longname
func (flagSet *FlagSet) StringVarP(field *string, long, short, defaultValue, usage string) *FlagData {
flagData := &FlagData{
usage: usage,
long: long,
defaultValue: defaultValue,
}
if short != "" {
flagData.short = short
flagSet.CommandLine.StringVar(field, short, defaultValue, usage)
flagSet.flagKeys.Set(short, flagData)
}
flagSet.CommandLine.StringVar(field, long, defaultValue, usage)
flagSet.flagKeys.Set(long, flagData)
return flagData
}
// StringVar adds a string flag with a longname
func (flagSet *FlagSet) StringVar(field *string, long, defaultValue, usage string) *FlagData {
return flagSet.StringVarP(field, long, "", defaultValue, usage)
}
// BoolVarP adds a bool flag with a shortname and longname
func (flagSet *FlagSet) BoolVarP(field *bool, long, short string, defaultValue bool, usage string) *FlagData {
flagData := &FlagData{
usage: usage,
long: long,
defaultValue: strconv.FormatBool(defaultValue),
}
if short != "" {
flagData.short = short
flagSet.CommandLine.BoolVar(field, short, defaultValue, usage)
flagSet.flagKeys.Set(short, flagData)
}
flagSet.CommandLine.BoolVar(field, long, defaultValue, usage)
flagSet.flagKeys.Set(long, flagData)
return flagData
}
// BoolVar adds a bool flag with a longname
func (flagSet *FlagSet) BoolVar(field *bool, long string, defaultValue bool, usage string) *FlagData {
return flagSet.BoolVarP(field, long, "", defaultValue, usage)
}
// IntVarP adds a int flag with a shortname and longname
func (flagSet *FlagSet) IntVarP(field *int, long, short string, defaultValue int, usage string) *FlagData {
flagData := &FlagData{
usage: usage,
short: short,
long: long,
defaultValue: strconv.Itoa(defaultValue),
}
if short != "" {
flagData.short = short
flagSet.CommandLine.IntVar(field, short, defaultValue, usage)
flagSet.flagKeys.Set(short, flagData)
}
flagSet.CommandLine.IntVar(field, long, defaultValue, usage)
flagSet.flagKeys.Set(long, flagData)
return flagData
}
// IntVar adds a int flag with a longname
func (flagSet *FlagSet) IntVar(field *int, long string, defaultValue int, usage string) *FlagData {
return flagSet.IntVarP(field, long, "", defaultValue, usage)
}
// StringSliceVarP adds a string slice flag with a shortname and longname
// Use options to customize the behavior
func (flagSet *FlagSet) StringSliceVarP(field *StringSlice, long, short string, defaultValue StringSlice, usage string, options Options) *FlagData {
optionMap[field] = options
for _, defaultItem := range defaultValue {
values, _ := ToStringSlice(defaultItem, options)
for _, value := range values {
_ = field.Set(value)
}
}
optionDefaultValues[field] = *field
flagData := &FlagData{
usage: usage,
long: long,
defaultValue: defaultValue,
}
if short != "" {
flagData.short = short
flagSet.CommandLine.Var(field, short, usage)
flagSet.flagKeys.Set(short, flagData)
}
flagSet.CommandLine.Var(field, long, usage)
flagSet.flagKeys.Set(long, flagData)
return flagData
}
// StringSliceVar adds a string slice flag with a longname
// Supports ONE value at a time. Adding multiple values require repeating the argument (-flag value1 -flag value2)
// No value normalization is happening.
func (flagSet *FlagSet) StringSliceVar(field *StringSlice, long string, defaultValue []string, usage string, options Options) *FlagData {
return flagSet.StringSliceVarP(field, long, "", defaultValue, usage, options)
}
// StringSliceVarConfigOnly adds a string slice config value (without flag) with a longname
func (flagSet *FlagSet) StringSliceVarConfigOnly(field *StringSlice, long string, defaultValue []string, usage string) *FlagData {
for _, item := range defaultValue {
_ = field.Set(item)
}
flagData := &FlagData{
usage: usage,
long: long,
defaultValue: defaultValue,
field: field,
}
flagSet.configOnlyKeys.Set(long, flagData)
flagSet.flagKeys.Set(long, flagData)
return flagData
}
// RuntimeMapVarP adds a runtime only map flag with a longname
func (flagSet *FlagSet) RuntimeMapVar(field *RuntimeMap, long string, defaultValue []string, usage string) *FlagData {
return flagSet.RuntimeMapVarP(field, long, "", defaultValue, usage)
}
// RuntimeMapVarP adds a runtime only map flag with a shortname and longname
func (flagSet *FlagSet) RuntimeMapVarP(field *RuntimeMap, long, short string, defaultValue []string, usage string) *FlagData {
for _, item := range defaultValue {
_ = field.Set(item)
}
flagData := &FlagData{
usage: usage,
long: long,
defaultValue: defaultValue,
skipMarshal: true,
}
if short != "" {
flagData.short = short
flagSet.CommandLine.Var(field, short, usage)
flagSet.flagKeys.Set(short, flagData)
}
flagSet.CommandLine.Var(field, long, usage)
flagSet.flagKeys.Set(long, flagData)
return flagData
}
// PortVar adds a port flag with a longname
func (flagSet *FlagSet) PortVar(field *Port, long string, defaultValue []string, usage string) *FlagData {
return flagSet.PortVarP(field, long, "", defaultValue, usage)
}
// PortVarP adds a port flag with a shortname and longname
func (flagSet *FlagSet) PortVarP(field *Port, long, short string, defaultValue []string, usage string) *FlagData {
for _, item := range defaultValue {
_ = field.Set(item)
}
portOptionDefaultValues[field] = maps.Clone(field.kv)
flagData := &FlagData{
usage: usage,
long: long,
defaultValue: defaultValue,
skipMarshal: true,
}
if short != "" {
flagData.short = short
flagSet.CommandLine.Var(field, short, usage)
flagSet.flagKeys.Set(short, flagData)
}
flagSet.CommandLine.Var(field, long, usage)
flagSet.flagKeys.Set(long, flagData)
return flagData
}
// EnumVar adds a enum flag with a longname
func (flagSet *FlagSet) EnumVar(field *string, long string, defaultValue EnumVariable, usage string, allowedTypes AllowdTypes) *FlagData {
return flagSet.EnumVarP(field, long, "", defaultValue, usage, allowedTypes)
}
// EnumVarP adds a enum flag with a shortname and longname
func (flagSet *FlagSet) EnumVarP(field *string, long, short string, defaultValue EnumVariable, usage string, allowedTypes AllowdTypes) *FlagData {
var hasDefaultValue bool
for k, v := range allowedTypes {
if v == defaultValue {
hasDefaultValue = true
*field = k
}
}
if !hasDefaultValue {
panic("undefined default value")
}
flagData := &FlagData{
usage: usage,
long: long,
defaultValue: *field,
}
if short != "" {
flagData.short = short
flagSet.CommandLine.Var(&EnumVar{allowedTypes, field}, short, usage)
flagSet.flagKeys.Set(short, flagData)
}
flagSet.CommandLine.Var(&EnumVar{allowedTypes, field}, long, usage)
flagSet.flagKeys.Set(long, flagData)
return flagData
}
// EnumVar adds a enum flag with a longname
func (flagSet *FlagSet) EnumSliceVar(field *[]string, long string, defaultValues []EnumVariable, usage string, allowedTypes AllowdTypes) *FlagData {
return flagSet.EnumSliceVarP(field, long, "", defaultValues, usage, allowedTypes)
}
// EnumVarP adds a enum flag with a shortname and longname
func (flagSet *FlagSet) EnumSliceVarP(field *[]string, long, short string, defaultValues []EnumVariable, usage string, allowedTypes AllowdTypes) *FlagData {
var defaults []string
for k, v := range allowedTypes {
for _, defaultValue := range defaultValues {
if v == defaultValue {
defaults = append(defaults, k)
}
}
}
if len(defaults) == 0 {
panic("undefined default value")
}
*field = defaults
flagData := &FlagData{
usage: usage,
long: long,
defaultValue: strings.Join(*field, ","),
}
if short != "" {
flagData.short = short
flagSet.CommandLine.Var(&EnumSliceVar{allowedTypes, field}, short, usage)
flagSet.flagKeys.Set(short, flagData)
}
flagSet.CommandLine.Var(&EnumSliceVar{allowedTypes, field}, long, usage)
flagSet.flagKeys.Set(long, flagData)
return flagData
}
func (flagSet *FlagSet) usageFunc() {
var helpAsked bool
// Only show help usage if asked by user
for _, arg := range os.Args {
argStripped := strings.Trim(arg, "-")
if argStripped == "h" || argStripped == "help" {
helpAsked = true
}
}
if !helpAsked {
return
}
cliOutput := flagSet.CommandLine.Output()
fmt.Fprintf(cliOutput, "%s\n\n", flagSet.description)
fmt.Fprintf(cliOutput, "Usage:\n %s [flags]\n\n", os.Args[0])
fmt.Fprintf(cliOutput, "Flags:\n")
writer := tabwriter.NewWriter(cliOutput, 0, 0, 1, ' ', 0)
// If a user has specified a group with help, and we have groups, return with the tool's usage function
if len(flagSet.groups) > 0 && len(os.Args) == 3 {
group := flagSet.getGroupbyName(strings.ToLower(os.Args[2]))
if group.name != "" {
flagSet.displayGroupUsageFunc(newUniqueDeduper(), group, cliOutput, writer)
return
}
flag := flagSet.getFlagByName(os.Args[2])
if flag != nil {
flagSet.displaySingleFlagUsageFunc(os.Args[2], flag, cliOutput, writer)
return
}
}
if len(flagSet.groups) > 0 {
flagSet.usageFuncForGroups(cliOutput, writer)
} else {
flagSet.usageFuncInternal(writer)
}
// If there is a custom help text specified, print it
if !isEmpty(flagSet.customHelpText) {
fmt.Fprintf(cliOutput, "\n%s\n", flagSet.customHelpText)
}
}
func (flagSet *FlagSet) getGroupbyName(name string) groupData {
for _, group := range flagSet.groups {
if strings.EqualFold(group.name, name) || strings.EqualFold(group.description, name) {
return group
}
}
return groupData{}
}
func (flagSet *FlagSet) getFlagByName(name string) *FlagData {
var flagData *FlagData
flagSet.flagKeys.forEach(func(key string, data *FlagData) {
// check if the items are equal
// - Case sensitive
equal := flagSet.CaseSensitive && (data.long == name || data.short == name)
// - Case insensitive
equalFold := !flagSet.CaseSensitive && (strings.EqualFold(data.long, name) || strings.EqualFold(data.short, name))
if equal || equalFold {
flagData = data
return
}
})
return flagData
}
// usageFuncInternal prints usage for command line flags
func (flagSet *FlagSet) usageFuncInternal(writer *tabwriter.Writer) {
uniqueDeduper := newUniqueDeduper()
flagSet.flagKeys.forEach(func(key string, data *FlagData) {
if currentFlag := flagSet.CommandLine.Lookup(key); currentFlag != nil {
if !uniqueDeduper.isUnique(data) {
return
}
result := createUsageString(data, currentFlag)
fmt.Fprint(writer, result, "\n")
}
})
writer.Flush()
}
// usageFuncForGroups prints usage for command line flags with grouping enabled
func (flagSet *FlagSet) usageFuncForGroups(cliOutput io.Writer, writer *tabwriter.Writer) {
uniqueDeduper := newUniqueDeduper()
var otherOptions []string
for _, group := range flagSet.groups {
otherOptions = append(otherOptions, flagSet.displayGroupUsageFunc(uniqueDeduper, group, cliOutput, writer)...)
}
// Print Any additional flag that may have been left
if len(otherOptions) > 0 {
fmt.Fprintf(cliOutput, "%s:\n", normalizeGroupDescription(flagSet.OtherOptionsGroupName))
for _, option := range otherOptions {
fmt.Fprint(writer, option, "\n")
}
writer.Flush()
}
}
// displayGroupUsageFunc displays usage for a group
func (flagSet *FlagSet) displayGroupUsageFunc(uniqueDeduper *uniqueDeduper, group groupData, cliOutput io.Writer, writer *tabwriter.Writer) []string {
fmt.Fprintf(cliOutput, "%s:\n", normalizeGroupDescription(group.description))
var otherOptions []string
flagSet.flagKeys.forEach(func(key string, data *FlagData) {
if currentFlag := flagSet.CommandLine.Lookup(key); currentFlag != nil {
if data.group == "" {
if !uniqueDeduper.isUnique(data) {
return
}
otherOptions = append(otherOptions, createUsageString(data, currentFlag))
return
}
// Ignore the flag if it's not in our intended group
if !strings.EqualFold(data.group, group.name) {
return
}
if !uniqueDeduper.isUnique(data) {
return
}
result := createUsageString(data, currentFlag)
fmt.Fprint(writer, result, "\n")
}
})
writer.Flush()
fmt.Printf("\n")
return otherOptions
}
// displaySingleFlagUsageFunc displays usage for a single flag
func (flagSet *FlagSet) displaySingleFlagUsageFunc(name string, data *FlagData, _ io.Writer, writer *tabwriter.Writer) {
if currentFlag := flagSet.CommandLine.Lookup(name); currentFlag != nil {
result := createUsageString(data, currentFlag)
fmt.Fprint(writer, result, "\n")
writer.Flush()
}
}
type uniqueDeduper struct {
hashes map[string]interface{}
}
func newUniqueDeduper() *uniqueDeduper {
return &uniqueDeduper{hashes: make(map[string]interface{})}
}
// isUnique returns true if the flag is unique during iteration
func (u *uniqueDeduper) isUnique(data *FlagData) bool {
dataHash := data.Hash()
if _, ok := u.hashes[dataHash]; ok {
return false // Don't print the value if printed previously
}
u.hashes[dataHash] = struct{}{}
return true
}
func createUsageString(data *FlagData, currentFlag *flag.Flag) string {
valueType := reflect.TypeOf(currentFlag.Value)
result := createUsageFlagNames(data)
result += createUsageTypeAndDescription(currentFlag, valueType)
result += createUsageDefaultValue(data, currentFlag, valueType)
return result
}
func createUsageDefaultValue(data *FlagData, currentFlag *flag.Flag, valueType reflect.Type) string {
if !isZeroValue(currentFlag, currentFlag.DefValue) {
defaultValueTemplate := " (default "
switch valueType.String() { // ugly hack because "flag.stringValue" is not exported from the parent library
case "*flag.stringValue":
defaultValueTemplate += "%q"
default:
defaultValueTemplate += "%v"
}
defaultValueTemplate += ")"
return fmt.Sprintf(defaultValueTemplate, data.defaultValue)
}
return ""
}
func createUsageTypeAndDescription(currentFlag *flag.Flag, valueType reflect.Type) string {
var result string
flagDisplayType, usage := flag.UnquoteUsage(currentFlag)
if len(flagDisplayType) > 0 {
if flagDisplayType == "value" { // hardcoded in the goflags library
switch valueType.Kind() {
case reflect.Ptr:
pointerTypeElement := valueType.Elem()
switch pointerTypeElement.Kind() {
case reflect.Slice, reflect.Array:
switch pointerTypeElement.Elem().Kind() {
case reflect.String:
flagDisplayType = "string[]"
default:
flagDisplayType = "value[]"
}
}
}
}
result += " " + flagDisplayType
}
result += "\t\t"
result += strings.ReplaceAll(usage, "\n", "\n"+strings.Repeat(" ", 4)+"\t")
return result
}
func createUsageFlagNames(data *FlagData) string {
flagNames := strings.Repeat(" ", 2) + "\t"
var validFlags []string
addValidParam := func(value string) {
if !isEmpty(value) {
validFlags = append(validFlags, fmt.Sprintf("-%s", value))
}
}
addValidParam(data.short)
addValidParam(data.long)
if len(validFlags) == 0 {
panic("CLI arguments cannot be empty.")
}
flagNames += strings.Join(validFlags, ", ")
return flagNames
}
// isZeroValue determines whether the string represents the zero
// value for a flag.
func isZeroValue(f *flag.Flag, value string) bool {
// Build a zero value of the flag's Value type, and see if the
// result of calling its String method equals the value passed in.
// This works unless the Value type is itself an interface type.
valueType := reflect.TypeOf(f.Value)
var zeroValue reflect.Value
if valueType.Kind() == reflect.Ptr {
zeroValue = reflect.New(valueType.Elem())
} else {
zeroValue = reflect.Zero(valueType)
}
return value == zeroValue.Interface().(flag.Value).String()
}
// normalizeGroupDescription returns normalized description field for group
func normalizeGroupDescription(description string) string {
return strings.ToUpper(description)
}
// GetArgsFromString allows splitting a string into arguments
// following the same rules as the shell.
func GetArgsFromString(str string) []string {
args, _ := shlex.Split(str)
return args
}