-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhelp.go
139 lines (118 loc) · 2.86 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
132
133
134
135
136
137
138
139
package cli
import (
"bytes"
"fmt"
"log"
"regexp"
"strings"
"text/template"
"unicode"
)
func (c *Command) help(reason error) error {
if c.Flags().Lookup("help") == nil {
_ = initHelpFlag(c)
}
return ErrHelp{
Message: reason.Error(),
usage: c.Usage(),
error: true,
}
}
// ErrHelp wraps an actual error, showing the usage of the command afterwards
type ErrHelp struct {
Message string
usage string
error bool
}
func (e ErrHelp) Error() string {
pat := "%s\n\n%s"
if e.error {
pat = "Error: " + pat
}
return fmt.Sprintf(pat, e.Message, e.usage)
}
// helpable is a internal wrapper type of Command that defines functions
// required to generate the help output using a text/template.
type helpable struct {
Command
}
func (h *helpable) Generate() string {
tmpl := template.New("")
tmpl = tmpl.Funcs(template.FuncMap{
"trimRightSpace": func(s string) string {
return strings.TrimRightFunc(s, unicode.IsSpace)
},
"rpad": func(s string, padding int) string {
template := fmt.Sprintf("%%-%ds", padding)
return fmt.Sprintf(template, s)
},
})
tmpl = template.Must(tmpl.Parse(`
Usage:
{{- if .HasChildren }}
{{.CommandPath}} [command]
{{ else }}
{{ .Use }}
{{ end }}
{{if .HasChildren }}
Available Commands:
{{- range .Children }}
{{rpad .Name .CommandPadding }} {{.Short}}
{{- end}}
{{- end}}
{{if .Flags }}
Flags:
{{.Flags.FlagUsages}}
{{ end}}
{{ if .HasChildren }}
Use "{{.CommandPath}} [command] --help" for more information about a command.
{{ end}}
`))
var buf bytes.Buffer
if err := tmpl.Execute(&buf, h); err != nil {
log.Fatalln(err)
}
// at least two newlines
return strings.TrimSpace(regexp.MustCompile(`\n\n+`).ReplaceAllString(buf.String(), "\n\n"))
}
// CommandPadding computes the padding required to make all subcommand help
// texts line up
func (h *helpable) CommandPadding() int {
pad := 9
for _, c := range h.parentPtr.Commands {
l := len(c.Name())
if l > pad {
pad = l
}
}
return pad + 2
}
// Use returns the UseLine for the given command.
// Parent command names are prepended
func (h *helpable) Use() string {
use := h.Command.Use
if h.parentPtr != nil {
use = h.parentPtr.helpable().CommandPath() + " " + h.Command.Use
}
return fmt.Sprintf("%s [flags]", use)
}
// HasChildren reports whether this command has children
func (h *helpable) HasChildren() bool {
return h.Commands != nil
}
// Children returns the children of this command.
func (h *helpable) Children() []*helpable {
m := make([]*helpable, len(h.Commands))
for i, c := range h.Commands {
m[i] = c.helpable()
}
return m
}
// CommandPath returns the names of this and all parent commands joined, in the
// same order as they would be specified on the command line.
func (h *helpable) CommandPath() string {
if h.parentPtr != nil {
return fmt.Sprintf("%s %s", h.parentPtr.helpable().CommandPath(), h.Name())
}
return h.Name()
}