-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelp.go
131 lines (107 loc) · 2.77 KB
/
help.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
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
package cli
import (
"fmt"
"io"
"strings"
"text/tabwriter"
"text/template"
)
var ErrHelp = fmt.Errorf("cli: help requested")
var helpTemplateString = `
{{- if 0}}{{end -}}
USAGE:
{{.FullName}}{{if .Fields}} [OPTIONS]{{end}}{{if .Commands}} <COMMAND>{{end}}{{if .Args}} [ARGS]{{end}}
{{- if .SupportsHelpCommand}}
{{.FullName}} help{{if .Commands}} [COMMAND...]{{end}}
{{- end}}
{{- if .Fields}}
OPTIONS:
{{- range .Fields}}{{if not .Hidden}}
\t \t
{{- if .ShortName}}-{{.ShortName}}, {{end}}--{{.Name}}
{{- if .HasArg}} <{{if .Placeholder}}{{.Placeholder}}{{else}}VALUE{{end}}>{{end}}\t
{{- if .EnvVarName}} {{.EnvVarName}}{{end}}\t
{{- if .Help}} {{.Help}}{{end}}
{{- if and .HasArg }}{{if and .Default (not .Required)}} (default: {{.Default}}){{else if .Required}} (required){{end}}{{end}}
{{- end}}
{{- end}}{{end}}
{{- if .Commands}}
COMMANDS:
{{- range .Commands}}
\t \t{{.Name}}\t{{ if .Help}} {{.Help}}{{end}}
{{- end}}
{{- end}}
{{- if .Description}}
DESCRIPTION:
{{.Description}}
{{- end}}
`
var helpTemplate *template.Template
func init() {
helpTemplate = template.Must(
template.New("help").Parse(helpTemplateString),
)
}
func (cmd *Command) fullName() string {
sb := strings.Builder{}
if cmd.parent != nil {
sb.WriteString(cmd.parent.fullName())
sb.WriteString(" ")
}
sb.WriteString(cmd.name)
return sb.String()
}
func (cmd *Command) HelpString() string {
sb := strings.Builder{}
cmd.WriteHelp(&sb)
return sb.String()
}
func (cmd *Command) WriteHelp(w io.Writer) {
type subcommandData struct {
Name string
Help string
}
data := struct {
FullName string
Description string
Fields []field
Commands []subcommandData
Args bool
SupportsHelpCommand bool
}{
FullName: cmd.fullName(),
Description: strings.ReplaceAll(strings.TrimSpace(cmd.description), "\n", "\n "),
Fields: cmd.fields,
Commands: []subcommandData{},
Args: cmd.argsField != nil,
SupportsHelpCommand: cmd.parent == nil && cmd.argsField == nil,
}
for _, cmd := range cmd.commands {
data.Commands = append(data.Commands, subcommandData{
Name: cmd.name,
Help: cmd.help,
})
}
tw := newEscapedTabWriter(w)
err := helpTemplate.Execute(tw, data)
if err != nil {
panic(fmt.Sprintf("cli: error executing help template: %s", err))
}
tw.Flush()
}
type escapedTabWriter struct {
replacer *strings.Replacer
tabWriter *tabwriter.Writer
}
func newEscapedTabWriter(w io.Writer) escapedTabWriter {
return escapedTabWriter{
replacer: strings.NewReplacer(`\t`, "\t", `\f`, "\f"),
tabWriter: tabwriter.NewWriter(w, 0, 0, 0, ' ', 0),
}
}
func (w escapedTabWriter) Write(p []byte) (int, error) {
return w.replacer.WriteString(w.tabWriter, string(p))
}
func (w escapedTabWriter) Flush() error {
return w.tabWriter.Flush()
}