Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Oct 17, 2021. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathhelp.go
More file actions
Latest commit
91 lines (75 loc) · 1.97 KB
/
Copy pathhelp.go
File metadata and controls
91 lines (75 loc) · 1.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
package cli
import (
"fmt"
"io"
"strings"
"text/tabwriter"
"unicode"
"unicode/utf8"
"github.com/spf13/pflag"
)
funcflagDashes(namestring) string {
ifutf8.RuneCountInString(name) >1 {
return"--"
}
return"-"
}
// fmtDefValue adds quotes around default value strings that contain spaces so
// the help representation matches what you would need to do when running a
// command.
funcfmtDefValue(valueinterface{}) string {
switchv:=value.(type) {
casestring:
ifstrings.IndexFunc(v, unicode.IsSpace) !=-1 {
returnfmt.Sprintf(`"%v"`, v)
}
returnv
default:
returnfmt.Sprintf("%v", v)
}
}
// \xFF is used to escape a \t so tabwriter ignores it.
consttabEscape="\xFF"
funcrenderFlagHelp(fullNamestring, fl*pflag.FlagSet, w io.Writer) {
iffl.HasFlags() {
fmt.Fprintf(w, "\n%s flags:\n", fullName)
fmt.Fprint(w, fl.FlagUsages())
}
}
// renderHelp generates a command's help page.
funcrenderHelp(w io.Writer, fullNamestring, cmdCommand, fl*pflag.FlagSet) {
varb strings.Builder
spec:=cmd.Spec()
fmt.Fprintf(&b, "Usage: %v %v\n\n", fullName, spec.Usage)
// If the command has aliases, add them to the output as a comma-separated list.
ifspec.HasAliases() {
fmt.Fprintf(&b, "Aliases: %s\n\n", strings.Join(spec.Aliases, ", "))
}
// Print usage and description.
fmt.Fprintf(w, "%sDescription: %s\n", b.String(), spec.Desc)
tw:=tabwriter.NewWriter(w, 0, 4, 2, ' ', tabwriter.StripEscape)
defertw.Flush()
// Render flag help.
renderFlagHelp(fullName, fl, tw)
// Render subcommand summaries.
pc, ok:=cmd.(ParentCommand)
ifok {
iflen(pc.Subcommands()) >0 {
// Give some space from flags.
fmt.Fprintf(w, "\n")
fmt.Fprint(w, "Commands:\n")
}
for_, cmd:=rangepc.Subcommands() {
spec:=cmd.Spec()
ifspec.Hidden {
continue
}
allNames:=strings.Join(append(spec.Aliases, spec.Name), ", ")
fmt.Fprintf(tw,
tabEscape+"\t"+tabEscape+"%v\t- %v\n",
allNames,
spec.ShortDesc(),
)
}
}
}