-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathhelpValues.go
More file actions
574 lines (514 loc) · 14 KB
/
helpValues.go
File metadata and controls
574 lines (514 loc) · 14 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
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
package flaggy
import (
"log"
"reflect"
"sort"
"strconv"
"strings"
"unicode/utf8"
)
// Help represents the values needed to render a Help page
type Help struct {
Subcommands []HelpSubcommand
Positionals []HelpPositional
Flags []HelpFlag
GlobalFlags []HelpFlag
UsageString string
CommandName string
PrependMessage string
AppendMessage string
ShowCompletion bool
Message string
Description string
Lines []string
}
// HelpSubcommand is used to template subcommand Help output
type HelpSubcommand struct {
ShortName string
LongName string
Description string
Position int
Spacer string
}
// HelpPositional is used to template positional Help output
type HelpPositional struct {
Name string
Description string
Required bool
Position int
DefaultValue string
Spacer string
}
// HelpFlag is used to template string flag Help output
type HelpFlag struct {
ShortName string
LongName string
Description string
DefaultValue string
ShortDisplay string
LongDisplay string
}
// ExtractValues extracts Help template values from a subcommand and its parent
// parser. The parser is required in order to detect default flag settings
// for help and version output.
func (h *Help) ExtractValues(p *Parser, message string) {
// accept message string for output
h.Message = message
ctx := p.subcommandContext
if ctx == nil || ctx == p.initialSubcommandContext {
ctx = &p.Subcommand
}
isRootContext := ctx == &p.Subcommand
// extract Help values from the current subcommand in context
// prependMessage string
h.PrependMessage = ctx.AdditionalHelpPrepend
// appendMessage string
h.AppendMessage = ctx.AdditionalHelpAppend
// command name
h.CommandName = ctx.Name
// description
h.Description = ctx.Description
// shell completion
showCompletion := p.ShowCompletion && p.isTopLevelHelpContext()
h.ShowCompletion = showCompletion
// determine the max length of subcommand names for spacer calculation.
maxLength := getLongestNameLength(ctx.Subcommands, 0)
// include the synthetic completion subcommand in spacer calculation
if showCompletion {
if l := len("completion"); l > maxLength {
maxLength = l
}
}
// subcommands []HelpSubcommand
for _, cmd := range ctx.Subcommands {
if cmd.Hidden {
continue
}
newHelpSubcommand := HelpSubcommand{
ShortName: cmd.ShortName,
LongName: cmd.Name,
Description: cmd.Description,
Position: cmd.Position,
Spacer: makeSpacer(cmd.Name, maxLength),
}
h.Subcommands = append(h.Subcommands, newHelpSubcommand)
}
// Append a synthetic completion subcommand at the end when enabled.
// This shows users the correct invocation: "./appName completion [bash|zsh]".
if showCompletion {
completionHelp := HelpSubcommand{
ShortName: "",
LongName: "completion",
Description: "Generate shell completion script for bash or zsh.",
Position: 0,
Spacer: makeSpacer("completion", maxLength),
}
h.Subcommands = append(h.Subcommands, completionHelp)
}
maxLength = getLongestNameLength(ctx.PositionalFlags, 0)
// parse positional flags into help output structs
for _, pos := range ctx.PositionalFlags {
if pos.Hidden {
continue
}
newHelpPositional := HelpPositional{
Name: pos.Name,
Position: pos.Position,
Description: pos.Description,
Required: pos.Required,
DefaultValue: pos.defaultValue,
Spacer: makeSpacer(pos.Name, maxLength),
}
h.Positionals = append(h.Positionals, newHelpPositional)
}
// if the built-in version flag is enabled, then add it to the appropriate help collection
if p.ShowVersionWithVersionFlag {
defaultVersionFlag := HelpFlag{
ShortName: "",
LongName: versionFlagLongName,
Description: "Displays the program version string.",
DefaultValue: "",
}
if isRootContext {
h.addFlagToSlice(&h.Flags, defaultVersionFlag)
} else {
h.addFlagToSlice(&h.GlobalFlags, defaultVersionFlag)
}
}
// if the built-in help flag exists, then add it as a help flag
if p.ShowHelpWithHFlag {
defaultHelpFlag := HelpFlag{
ShortName: helpFlagShortName,
LongName: helpFlagLongName,
Description: "Displays help with available flag, subcommand, and positional value parameters.",
DefaultValue: "",
}
if isRootContext {
h.addFlagToSlice(&h.Flags, defaultHelpFlag)
} else {
h.addFlagToSlice(&h.GlobalFlags, defaultHelpFlag)
}
}
// go through every flag in the subcommand and add it to help output
h.parseFlagsToHelpFlags(ctx.Flags, &h.Flags)
// go through every flag in the parent parser and add it to help output
if isRootContext {
h.parseFlagsToHelpFlags(p.Flags, &h.Flags)
} else {
h.parseFlagsToHelpFlags(p.Flags, &h.GlobalFlags)
}
// Optionally sort flags alphabetically by long name (fallback to short name)
if p.SortFlags {
sort.SliceStable(h.Flags, func(i, j int) bool {
a := h.Flags[i]
b := h.Flags[j]
aName := strings.ToLower(strings.TrimSpace(a.LongName))
bName := strings.ToLower(strings.TrimSpace(b.LongName))
if aName == "" {
aName = strings.ToLower(strings.TrimSpace(a.ShortName))
}
if bName == "" {
bName = strings.ToLower(strings.TrimSpace(b.ShortName))
}
if p.SortFlagsReverse {
return aName > bName
}
return aName < bName
})
sort.SliceStable(h.GlobalFlags, func(i, j int) bool {
a := h.GlobalFlags[i]
b := h.GlobalFlags[j]
aName := strings.ToLower(strings.TrimSpace(a.LongName))
bName := strings.ToLower(strings.TrimSpace(b.LongName))
if aName == "" {
aName = strings.ToLower(strings.TrimSpace(a.ShortName))
}
if bName == "" {
bName = strings.ToLower(strings.TrimSpace(b.ShortName))
}
if p.SortFlagsReverse {
return aName > bName
}
return aName < bName
})
}
// formulate the usage string
// first, we capture all the command and positional names by position
commandsByPosition := make(map[int]string)
for _, pos := range ctx.PositionalFlags {
if pos.Hidden {
continue
}
if len(commandsByPosition[pos.Position]) > 0 {
commandsByPosition[pos.Position] = commandsByPosition[pos.Position] + "|" + pos.Name
} else {
commandsByPosition[pos.Position] = pos.Name
}
}
for _, cmd := range ctx.Subcommands {
if cmd.Hidden {
continue
}
if len(commandsByPosition[cmd.Position]) > 0 {
commandsByPosition[cmd.Position] = commandsByPosition[cmd.Position] + "|" + cmd.Name
} else {
commandsByPosition[cmd.Position] = cmd.Name
}
}
// find the highest position count in the map
var highestPosition int
for i := range commandsByPosition {
if i > highestPosition {
highestPosition = i
}
}
// only have a usage string if there are positional items
var usageString string
if highestPosition > 0 {
// find each positional value and make our final string
usageString = ctx.Name
for i := 1; i <= highestPosition; i++ {
if len(commandsByPosition[i]) > 0 {
usageString = usageString + " [" + commandsByPosition[i] + "]"
} else {
// dont keep listing after the first position without any properties
// it will be impossible to reach anything beyond here anyway
break
}
}
}
h.UsageString = usageString
alignHelpFlags(h.Flags)
alignHelpFlags(h.GlobalFlags)
h.composeLines()
}
// parseFlagsToHelpFlags parses the specified slice of flags into
// help flags on the the calling help command
func (h *Help) parseFlagsToHelpFlags(flags []*Flag, dest *[]HelpFlag) {
for _, f := range flags {
if f.Hidden {
continue
}
// parse help values out if the flag hasn't been parsed yet
if !f.parsed {
f.parsed = true
// parse the default value as a string and remember it for help output
f.defaultValue, _ = f.returnAssignmentVarValueAsString()
}
// determine the default value based on the assignment variable
defaultValue := f.defaultValue
// dont show nils
if defaultValue == "<nil>" {
defaultValue = ""
}
// for bools, dont show a default of false
_, isBool := f.AssignmentVar.(*bool)
if isBool {
b := f.AssignmentVar.(*bool)
if !*b {
defaultValue = ""
}
}
newHelpFlag := HelpFlag{
ShortName: f.ShortName,
LongName: f.LongName,
Description: f.Description,
DefaultValue: defaultValue,
}
h.addFlagToSlice(dest, newHelpFlag)
}
}
// addFlagToSlice adds a flag to the provided slice if it does not exist already.
func (h *Help) addFlagToSlice(dest *[]HelpFlag, f HelpFlag) {
for _, existingFlag := range *dest {
if len(existingFlag.ShortName) > 0 && existingFlag.ShortName == f.ShortName {
return
}
if len(existingFlag.LongName) > 0 && existingFlag.LongName == f.LongName {
return
}
}
*dest = append(*dest, f)
}
// getLongestNameLength takes a slice of any supported flag and returns the length of the longest of their names
func getLongestNameLength(slice interface{}, min int) int {
var maxLength = min
s := reflect.ValueOf(slice)
if s.Kind() != reflect.Slice {
log.Panicf("Parameter given to getLongestNameLength() is of type %s. Expected slice", s.Kind())
}
for i := 0; i < s.Len(); i++ {
option := s.Index(i).Interface()
var name string
switch t := option.(type) {
case *Subcommand:
name = t.Name
case *Flag:
name = t.LongName
case *PositionalValue:
name = t.Name
default:
log.Panicf("Unexpected type %T found in slice passed to getLongestNameLength(). Possible types: *Subcommand, *Flag, *PositionalValue", t)
}
length := len(name)
if length > maxLength {
maxLength = length
}
}
return maxLength
}
// makeSpacer creates a string of whitespaces, with a length of the given
// maxLength minus the length of the given name
func makeSpacer(name string, maxLength int) string {
length := maxLength - utf8.RuneCountInString(name)
if length < 0 {
length = 0
}
return strings.Repeat(" ", length)
}
func alignHelpFlags(flags []HelpFlag) {
if len(flags) == 0 {
return
}
shortWidth := 0
longWidth := 0
for _, flag := range flags {
shortCol := flagShortColumn(flag.ShortName)
longCol := flagLongColumn(flag.LongName)
if l := utf8.RuneCountInString(shortCol); l > shortWidth {
shortWidth = l
}
if l := utf8.RuneCountInString(longCol); l > longWidth {
longWidth = l
}
}
const shortGap = " "
const descGap = " "
for i := range flags {
shortCol := flagShortColumn(flags[i].ShortName)
longCol := flagLongColumn(flags[i].LongName)
if shortWidth > 0 {
flags[i].ShortDisplay = padRight(shortCol, shortWidth) + shortGap
} else {
flags[i].ShortDisplay = shortGap
}
if longWidth > 0 {
flags[i].LongDisplay = padRight(longCol, longWidth) + descGap
} else {
flags[i].LongDisplay = descGap
}
}
}
func flagShortColumn(shortName string) string {
if shortName == "" {
return ""
}
return "-" + shortName
}
func flagLongColumn(longName string) string {
if longName == "" {
return ""
}
return "--" + longName
}
func padRight(input string, width int) string {
delta := width - utf8.RuneCountInString(input)
if delta <= 0 {
return input
}
return input + strings.Repeat(" ", delta)
}
func (h *Help) composeLines() {
lines := make([]string, 0, 16)
appendBlank := func() {
if len(lines) > 0 && lines[len(lines)-1] != "" {
lines = append(lines, "")
}
}
if h.CommandName != "" || h.Description != "" {
header := h.CommandName
if h.Description != "" {
if header != "" {
header += " - "
}
header += h.Description
}
lines = append(lines, header)
}
if h.PrependMessage != "" {
lines = append(lines, splitLines(h.PrependMessage)...)
}
appendSection := func(section []string) {
if len(section) == 0 {
return
}
appendBlank()
lines = append(lines, section...)
}
if h.UsageString != "" {
section := []string{
" Usage:",
" " + h.UsageString,
}
appendSection(section)
}
if len(h.Positionals) > 0 {
section := []string{" Positional Variables:"}
for _, pos := range h.Positionals {
line := " " + pos.Name + " " + pos.Spacer
if pos.Description != "" {
line += " " + pos.Description
}
if pos.DefaultValue != "" {
line += " (default: " + pos.DefaultValue + ")"
} else if pos.Required {
line += " (Required)"
}
section = append(section, line)
}
appendSection(section)
}
if len(h.Subcommands) > 0 {
section := []string{" Subcommands:"}
for _, sub := range h.Subcommands {
line := " " + sub.LongName
if sub.ShortName != "" {
line += " (" + sub.ShortName + ")"
}
if sub.Position > 1 {
line += " (position " + strconv.Itoa(sub.Position) + ")"
}
if sub.Description != "" {
line += " " + sub.Spacer + sub.Description
}
section = append(section, line)
}
appendSection(section)
}
if len(h.Flags) > 0 {
section := []string{" Flags:"}
for _, flag := range h.Flags {
line := " " + flag.ShortDisplay + flag.LongDisplay
descAdded := false
if flag.Description != "" {
line += flag.Description
descAdded = true
}
if flag.DefaultValue != "" {
if descAdded {
line += " (default: " + flag.DefaultValue + ")"
} else {
line += "(default: " + flag.DefaultValue + ")"
}
}
section = append(section, line)
}
appendSection(section)
}
if len(h.GlobalFlags) > 0 {
section := []string{" Global Flags:"}
for _, flag := range h.GlobalFlags {
line := " " + flag.ShortDisplay + flag.LongDisplay
descAdded := false
if flag.Description != "" {
line += flag.Description
descAdded = true
}
if flag.DefaultValue != "" {
if descAdded {
line += " (default: " + flag.DefaultValue + ")"
} else {
line += "(default: " + flag.DefaultValue + ")"
}
}
section = append(section, line)
}
appendSection(section)
}
appendText := func(text string) {
if text == "" {
return
}
appendBlank()
lines = append(lines, splitLines(text)...)
}
appendText(h.AppendMessage)
appendText(h.Message)
if len(lines) == 0 {
lines = append(lines, "")
} else {
if lines[0] != "" {
lines = append([]string{""}, lines...)
}
if lines[len(lines)-1] != "" {
lines = append(lines, "")
}
}
h.Lines = lines
}
func splitLines(input string) []string {
if input == "" {
return nil
}
return strings.Split(input, "\n")
}