Repository navigation
Expand file tree
/
Copy pathusage.go
More file actions
256 lines (213 loc) · 5.97 KB
/
Copy pathusage.go
File metadata and controls
256 lines (213 loc) · 5.97 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
package conf
import (
"fmt"
"os"
"path"
"reflect"
"sort"
"strings"
"text/tabwriter"
)
const (
buildKey = "Build"
descKey = "Desc"
helpKey = "help"
versionKey = "version"
)
type sortedFields struct {
fields []Field
}
func (sf sortedFields) Len() int {
return len(sf.fields)
}
func (sf sortedFields) Swap(i, j int) {
sf.fields[i], sf.fields[j] = sf.fields[j], sf.fields[i]
}
func (sf sortedFields) Less(i, j int) bool {
s1 := strings.ToLower(strings.Join(sf.fields[i].FlagKey, `-`))
s2 := strings.ToLower(strings.Join(sf.fields[j].FlagKey, `-`))
return s1 < s2
}
func containsField(fields []Field, name string) bool {
for i := range fields {
if name == fields[i].Name {
return true
}
}
return false
}
func fmtUsage(namespace string, fields []Field) string {
var sb strings.Builder
fields = append(fields, Field{
Name: "help",
BoolField: true,
Field: reflect.ValueOf(true),
FlagKey: []string{"help"},
Options: FieldOptions{
ShortFlagChar: 'h',
Help: "display this help message",
}})
if containsField(fields, buildKey) {
fields = append(fields, Field{
Name: "version",
BoolField: true,
Field: reflect.ValueOf(true),
FlagKey: []string{"version"},
Options: FieldOptions{
ShortFlagChar: 'v',
Help: "display version",
}})
}
sf := sortedFields{fields: fields}
sort.Sort(&sf)
_, file := path.Split(os.Args[0])
fmt.Fprintf(&sb, "Usage: %s [options...] [arguments...]\n\n", file)
w := new(tabwriter.Writer)
w.Init(&sb, 0, 4, 2, ' ', tabwriter.TabIndent)
fmt.Fprintln(&sb, "OPTIONS")
writeOptions(w, sf.fields)
fmt.Fprintln(&sb, "ENVIRONMENT")
writeEnv(w, namespace, sf.fields)
return sb.String()
}
func writeOptions(w *tabwriter.Writer, fields []Field) {
for _, fld := range fields {
// Skip printing usage info for fields that just hold arguments.
if fld.Field.Type() == argsT {
continue
}
// Do not display version fields Build and Description
if fld.Name == buildKey || fld.Name == descKey {
continue
}
fmt.Fprintf(w, " %s", flagUsage(fld))
typeName, help := getTypeAndHelp(&fld)
// Do not display type info for help because it would show <bool> but our
// parsing does not really treat --help as a boolean field. Its presence
// always indicates true even if they do --help=false.
if fld.Name != helpKey && fld.Name != versionKey {
fmt.Fprintf(w, "\t%s", typeName)
fmt.Fprintf(w, "\t%s", getOptString(fld))
} else {
fmt.Fprint(w, "\t\t")
}
fmt.Fprintf(w, "\t%s", help)
fmt.Fprint(w, "\n")
}
fmt.Fprint(w, "\n")
w.Flush()
}
func writeEnv(w *tabwriter.Writer, namespace string, fields []Field) {
for _, fld := range fields {
// Skip printing usage info for fields that just hold arguments.
if fld.Field.Type() == argsT {
continue
}
// Do not display version fields and Description
// Do not display env vars for help since they aren't respected.
if fld.Name == buildKey || fld.Name == descKey ||
fld.Name == helpKey || fld.Name == versionKey {
continue
}
fmt.Fprintf(w, " %s", envUsage(namespace, fld))
typeName, help := getTypeAndHelp(&fld)
fmt.Fprintf(w, "\t%s", typeName)
fmt.Fprintf(w, "\t%s", getOptString(fld))
fmt.Fprintf(w, "\t%s", help)
fmt.Fprint(w, "\n")
}
w.Flush()
}
// getTypeAndHelp extracts the type and help message for a single field for
// printing in the usage message. If the help message contains text in
// single quotes ('), this is assumed to be a more specific "type", and will
// be returned as such. If there are no back quotes, it attempts to make a
// guess as to the type of the field. Boolean flags are not printed with a
// type, manually-specified or not, since their presence is equated with a
// 'true' value and their absence with a 'false' value. If a type cannot be
// determined, it will simply give the name "value". Slices will be annotated
// as "<Type>;[Type...]", where "Type" is whatever type name was chosen.
// (adapted from package flag).
func getTypeAndHelp(fld *Field) (name string, usage string) {
// Look for a single-quoted name.
usage = fld.Options.Help
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:]
}
}
break // Only one single quote; use type name.
}
}
var isSlice bool
if fld.Field.IsValid() {
t := fld.Field.Type()
// If it's a pointer, we want to deref.
if t.Kind() == reflect.Pointer {
t = t.Elem()
}
// If it's a slice, we want the type of the slice elements.
if t.Kind() == reflect.Slice {
t = t.Elem()
isSlice = true
}
// If no explicit name was provided, attempt to get the type
if name == "" {
switch t.Kind() {
case reflect.Bool:
name = "bool"
case reflect.Float32, reflect.Float64:
name = "float"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
typ := fld.Field.Type()
if typ.PkgPath() == "time" && typ.Name() == "Duration" {
name = "duration"
} else {
name = "int"
}
case reflect.String:
name = "string"
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
name = "uint"
default:
name = "value"
}
}
}
switch {
case isSlice:
name = fmt.Sprintf("<%s>;[%s...]", name, name)
case name != "":
name = fmt.Sprintf("<%s>", name)
default:
}
return
}
func getOptString(fld Field) string {
opts := make([]string, 0, 3)
if fld.Options.Required {
opts = append(opts, "required")
}
if fld.Options.NotZero {
opts = append(opts, "notzero")
}
if fld.Options.Noprint {
opts = append(opts, "noprint")
}
if fld.Options.Immutable {
opts = append(opts, "immutable")
}
if fld.Options.Mask {
fld.Options.DefaultVal = maskVal(fld.Options.DefaultVal)
}
if fld.Options.DefaultVal != "" {
opts = append(opts, fmt.Sprintf("default: %s", fld.Options.DefaultVal))
}
if len(opts) > 0 {
return fmt.Sprintf("(%s)", strings.Join(opts, `,`))
}
return ""
}