/
opts_help.go
297 lines (262 loc) · 6.34 KB
/
opts_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
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
package opts
import (
"bytes"
"fmt"
"log"
"path"
"regexp"
"strings"
"text/template"
"github.com/kardianos/osext"
)
//data is only used for templating below
type data struct {
datum //data is also a datum
ArgList *datum
Opts []*datum
Args []*datum
Cmds []*datum
Order []string
Version string
Repo, Author string
ErrMsg string
}
type datum struct {
Name, Help, Pad string //Pad is Opt.PadWidth many spaces
}
type text struct {
Text, Def, Env string
}
var DefaultOrder = []string{
"usage",
"args",
"arglist",
"options",
"cmds",
"author",
"version",
"repo",
"errmsg",
}
var DefaultTemplates = map[string]string{
//the root template simply loops through
//the 'order' and renders each template by name
"help": `{{ $root := . }}` +
`{{range $t := .Order}}{{ templ $t $root }}{{end}}`,
//sections, from top to bottom
"usage": `Usage: {{.Name }}` +
`{{template "usageoptions" .}}` +
`{{template "usageargs" .}}` +
`{{template "usagearglist" .}}` +
`{{template "usagecmd" .}}` + "\n",
"usageoptions": `{{if .Opts}} [options]{{end}}`,
"usageargs": `{{range .Args}} {{.Name}}{{end}}`,
"usagearglist": `{{if .ArgList}} {{.ArgList.Name}}{{end}}`,
"usagecmd": `{{if .Cmds}} <command>{{end}}`,
//extra help text
"helpextra": `{{if .Def}}default {{.Def}}{{end}}` +
`{{if and .Def .Env}}, {{end}}` +
`{{if .Env}}env {{.Env}}{{end}}`,
//args and arg section
"args": `{{range .Args}}{{template "arg" .}}{{end}}`,
"arg": "{{if .Help}}\n{{.Help}}\n{{end}}",
"arglist": "{{if .ArgList}}{{ if .ArgList.Help}}\n{{.ArgList.Help}}\n{{end}}{{end}}",
//options
"options": `{{if .Opts}}` + "\nOptions:\n" +
`{{ range $opt := .Opts}}{{template "option" $opt}}{{end}}{{end}}`,
"option": `{{.Name}}{{if .Help}}{{.Pad}}{{.Help}}{{end}}` + "\n",
//cmds
"cmds": "{{if .Cmds}}\nCommands:\n" +
`{{ range $sub := .Cmds}}{{template "cmd" $sub}}{{end}}{{end}}`,
"cmd": "• {{ .Name }}{{if .Help}} - {{ .Help }}{{end}}\n",
//extras
"version": "{{if .Version}}\nVersion:\n{{.Pad}}{{.Version}}\n{{end}}",
"repo": "{{if .Repo}}\nRead more:\n{{.Pad}}{{.Repo}}\n{{end}}",
"author": "{{if .Author}}\nAuthor:\n{{.Pad}}{{.Author}}\n{{end}}",
"errmsg": "{{if .ErrMsg}}\nError:\n{{.Pad}}{{.ErrMsg}}\n{{end}}",
}
var trailingSpaces = regexp.MustCompile(`(?m)\ +$`)
//Help renders the help text as a string
func (o *Opts) Help() string {
var err error
//final attempt at finding the program name
root := o
for root.parent != nil {
root = root.parent
}
if root.name == "" {
if exe, err := osext.Executable(); err == nil {
_, root.name = path.Split(exe)
} else {
root.name = "main"
}
}
//add default templates
for name, str := range DefaultTemplates {
if _, ok := o.templates[name]; !ok {
o.templates[name] = str
}
}
//prepare templates
t := template.New(o.name)
t = t.Funcs(map[string]interface{}{
//reimplementation of "template" except with dynamic name
"templ": func(name string, data interface{}) (string, error) {
b := &bytes.Buffer{}
err = t.ExecuteTemplate(b, name, data)
if err != nil {
return "", err
}
return b.String(), nil
},
})
//verify all templates
for name, str := range o.templates {
t, err = t.Parse(fmt.Sprintf(`{{define "%s"}}%s{{end}}`, name, str))
if err != nil {
log.Fatalf("Template error: %s: %s", name, err)
}
}
//convert Opts into template data
tf := convert(o)
//execute all templates
b := &bytes.Buffer{}
err = t.ExecuteTemplate(b, "help", tf)
if err != nil {
log.Fatalf("Template execute: %s", err)
}
out := b.String()
if o.PadAll {
/*
"foo
bar"
becomes
"
foo
bar
"
*/
lines := strings.Split(out, "\n")
for i, l := range lines {
lines[i] = tf.Pad + l
}
out = "\n" + strings.Join(lines, "\n")
}
out = trailingSpaces.ReplaceAllString(out, "")
return out
}
var anyspace = regexp.MustCompile(`[\s]+`)
func convert(o *Opts) *data {
names := []string{}
curr := o
for curr != nil {
names = append([]string{curr.name}, names...)
curr = curr.parent
}
name := strings.Join(names, " ")
//get item help, with optional default values and env names and
//constrain to a specific line width
extratmpl, _ := template.New("").Parse(o.templates["helpextra"])
itemHelp := func(i *item, width int) string {
b := bytes.Buffer{}
extratmpl.Execute(&b, &text{Def: i.defstr, Env: i.envName})
extra := b.String()
help := i.help
if help == "" {
help = extra
} else if extra != "" {
help += " (" + extra + ")"
}
return constrain(help, width)
}
args := make([]*datum, len(o.args))
for i, arg := range o.args {
//mark argument as required
n := "<" + arg.name + ">"
if arg.defstr != "" { //or optional
n = "[" + arg.name + "]"
}
args[i] = &datum{
Name: n,
Help: itemHelp(arg, o.LineWidth),
}
}
var arglist *datum = nil
if o.arglist != nil {
n := o.arglist.name + "..."
if o.arglist.min == 0 { //optional
n = "[" + n + "]"
}
arglist = &datum{
Name: n,
Help: itemHelp(&o.arglist.item, o.LineWidth),
}
}
opts := make([]*datum, len(o.opts))
//calculate padding etc.
max := 0
pad := nletters(' ', o.PadWidth)
for i, opt := range o.opts {
to := &datum{Pad: pad}
to.Name = "--" + opt.name
if opt.shortName != "" {
to.Name += ", -" + opt.shortName
}
l := len(to.Name)
if l > max {
max = l
}
opts[i] = to
}
padsInOption := o.PadWidth
optionNameWidth := max + padsInOption
spaces := nletters(' ', optionNameWidth)
helpWidth := o.LineWidth - optionNameWidth
//render each option
for i, to := range opts {
//pad all option names to be the same length
to.Name += spaces[:max-len(to.Name)]
//constrain help text
help := itemHelp(o.opts[i], helpWidth)
//add a margin
lines := strings.Split(help, "\n")
for i, l := range lines {
if i > 0 {
lines[i] = spaces + l
}
}
to.Help = strings.Join(lines, "\n")
}
//commands
subs := make([]*datum, len(o.cmds))
i := 0
for _, s := range o.cmds {
subs[i] = &datum{
Name: s.name,
Help: s.help,
Pad: pad,
}
i++
}
//convert error to string
err := ""
if o.erred != nil {
err = o.erred.Error()
}
return &data{
datum: datum{
Name: name,
Help: o.help,
Pad: pad,
},
Args: args,
ArgList: arglist,
Opts: opts,
Cmds: subs,
Order: o.order,
Version: o.version,
Repo: o.repo,
Author: o.author,
ErrMsg: err,
}
}