-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelp.go
More file actions
181 lines (164 loc) · 4.07 KB
/
Copy pathhelp.go
File metadata and controls
181 lines (164 loc) · 4.07 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
package pennant
import (
"bytes"
"fmt"
"io"
"sort"
"strings"
)
// UnquoteUsage extracts a back-quoted name from the usage string and returns
// it and the un-quoted usage. Given "a]name to `search` for" it returns
// ("search", "a name to search for").
func UnquoteUsage(flag *Flag) (name string, usage string) {
usage = flag.Usage
for i := 0; i < len(usage); i++ {
if usage[i] == '`' {
for j := i + 1; j < len(usage); j++ {
if usage[j] == '`' {
name = usage[i+1 : j]
usage = usage[:i] + name + usage[j+1:]
return name, usage
}
}
break
}
}
name = flag.Value.Type()
switch name {
case "bool":
name = ""
case "float64":
name = "float"
case "int64":
name = "int"
case "uint64":
name = "uint"
case "stringSlice", "stringArray":
name = "strings"
case "intSlice":
name = "ints"
case "float64Slice":
name = "floats"
case "boolSlice":
name = "bools"
case "durationSlice":
name = "durations"
case "ipSlice":
name = "ips"
}
return
}
// FlagUsages returns a string containing the usage information for all flags
// in the FlagSet.
func (f *FlagSet) FlagUsages() string {
return f.FlagUsagesWrapped(0)
}
// FlagUsagesWrapped returns a string containing the usage information for all
// flags in the FlagSet, wrapping lines at the specified column (0 = no wrap).
func (f *FlagSet) FlagUsagesWrapped(cols int) string {
var buf bytes.Buffer
var flags []*Flag
f.VisitAll(func(flag *Flag) {
if flag.Hidden {
return
}
flags = append(flags, flag)
})
if f.SortFlags {
sort.Slice(flags, func(i, j int) bool {
return flags[i].Name < flags[j].Name
})
}
// Compute max width of the flag column for alignment
maxLen := 0
lines := make([]struct {
flagCol string
usage string
}, len(flags))
for i, flag := range flags {
var line strings.Builder
if flag.Shorthand != "" {
fmt.Fprintf(&line, " -%s, --%s", flag.Shorthand, flag.Name)
} else {
fmt.Fprintf(&line, " --%s", flag.Name)
}
name, usage := UnquoteUsage(flag)
if name != "" {
fmt.Fprintf(&line, " %s", name)
}
flagCol := line.String()
if len(flagCol) > maxLen {
maxLen = len(flagCol)
}
defValue := flag.DefValue
if flag.Value.Type() == "string" {
defValue = fmt.Sprintf("%q", defValue)
}
if flag.DefValue != "" && flag.DefValue != "0" && flag.DefValue != "false" && flag.DefValue != "[]" {
usage += fmt.Sprintf(" (default %s)", defValue)
}
lines[i].flagCol = flagCol
lines[i].usage = usage
}
for _, l := range lines {
// Pad flag column to maxLen
padding := maxLen - len(l.flagCol) + 3
if padding < 1 {
padding = 1
}
if cols > 0 && len(l.flagCol)+padding+len(l.usage) > cols {
// Wrap: flag column on one line, usage on next
fmt.Fprintf(&buf, "%s\n", l.flagCol)
wrapUsage(&buf, l.usage, maxLen+3, cols)
} else {
fmt.Fprintf(&buf, "%s%s%s\n", l.flagCol, strings.Repeat(" ", padding), l.usage)
}
}
return buf.String()
}
// wrapUsage writes usage text with word wrapping, indented to the given offset.
func wrapUsage(w io.Writer, usage string, indent, cols int) {
if cols <= indent {
_, _ = fmt.Fprintf(w, "%s%s\n", strings.Repeat(" ", indent), usage)
return
}
width := cols - indent
prefix := strings.Repeat(" ", indent)
words := strings.Fields(usage)
lineLen := 0
first := true
for _, word := range words {
if !first && lineLen+1+len(word) > width {
_, _ = fmt.Fprintf(w, "\n%s", prefix)
lineLen = 0
first = true
}
if !first {
_, _ = fmt.Fprint(w, " ")
lineLen++
} else {
_, _ = fmt.Fprint(w, prefix)
}
_, _ = fmt.Fprint(w, word)
lineLen += len(word)
first = false
}
if !first {
_, _ = fmt.Fprintln(w)
}
}
// PrintDefaults prints, to the FlagSet's output writer, the default values
// of all defined flags in the set.
func (f *FlagSet) PrintDefaults() {
usages := f.FlagUsages()
_, _ = fmt.Fprint(f.GetOutput(), usages)
}
// defaultUsage is the default Usage function.
func (f *FlagSet) defaultUsage() {
if f.name == "" {
_, _ = fmt.Fprintf(f.GetOutput(), "Usage:\n")
} else {
_, _ = fmt.Fprintf(f.GetOutput(), "Usage of %s:\n", f.name)
}
f.PrintDefaults()
}