-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefaults.go
73 lines (62 loc) · 1.58 KB
/
defaults.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
package dynflags
import (
"fmt"
"sort"
"strings"
"text/tabwriter"
)
// PrintDefaults prints all registered flags
func (df *DynFlags) PrintDefaults() {
w := tabwriter.NewWriter(df.output, 0, 8, 2, ' ', 0)
defer w.Flush()
// Print title if present
if df.title != "" {
fmt.Fprintln(df.output, df.title)
fmt.Fprintln(df.output)
}
// Print description if present
if df.description != "" {
fmt.Fprintln(df.output, df.description)
fmt.Fprintln(df.output)
}
// Sort group names
if df.SortGroups {
sort.Strings(df.groupOrder)
}
// Iterate over groups in the order they were added
for _, groupName := range df.groupOrder {
group := df.configGroups[groupName]
// Print group usage or fallback to uppercase group name
if group.usage != "" {
fmt.Fprintln(w, group.usage)
} else {
fmt.Fprintln(w, strings.ToUpper(groupName))
}
// Sort flag names
if df.SortFlags {
sort.Strings(group.flagOrder)
}
// Print flags for the group
if len(group.flagOrder) > 0 {
fmt.Fprintln(w, " Flag\tUsage")
for _, flagName := range group.flagOrder {
flag := group.Flags[flagName]
usage := flag.Usage
if flag.Default != nil && flag.Default != "" {
usage = fmt.Sprintf("%s (default: %v)", flag.Usage, flag.Default)
}
metavar := string(flag.Type)
if flag.metaVar != "" {
metavar = flag.metaVar
}
fmt.Fprintf(w, " --%s.<IDENTIFIER>.%s %s\t%s\n", groupName, flagName, metavar, usage)
}
fmt.Fprintln(w, "")
}
}
// Print epilog if present
if df.epilog != "" {
fmt.Fprintln(df.output)
fmt.Fprintln(df.output, df.epilog)
}
}