This repository has been archived by the owner on Jan 24, 2020. It is now read-only.
forked from smallstep/cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
usage.go
203 lines (154 loc) · 4.7 KB
/
usage.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
package usage
import (
"bytes"
"fmt"
"strings"
"text/template"
)
var usageTextTempl = " {{.Name}}\n {{.Usage}} {{if .Required}}(Required){{else}}(Optional){{end}}{{if .Multiple}} (Multiple can be specified){{end}}\n"
var templ *template.Template
func init() {
templ = template.Must(template.New("usageText").Parse(usageTextTempl))
}
// Argument specifies the Name, Usage, and whether or not an Argument is
// required or not
type Argument struct {
Required bool
Multiple bool
Name string
Usage string
}
// Decorate returns the name of an Argument and decorates it with notation to
// indicate whether its required or not
func (a Argument) Decorate() string {
name := a.Name
if a.Multiple {
name = name + "(s)..."
}
if a.Required {
return fmt.Sprintf("<%s>", name)
}
return fmt.Sprintf("[%s]", name)
}
// Arguments is an array of Argument structs that specify which arguments are
// accepted by a Command
type Arguments []Argument
// UsageText returns the value of the UsageText property for a cli.Command for
// these arguments
func (args Arguments) UsageText() string {
var buf bytes.Buffer
for _, a := range args {
data := map[string]interface{}{
"Name": a.Decorate(),
"Multiple": a.Multiple,
"Required": a.Required,
"Usage": a.Usage,
}
err := templ.Execute(&buf, data)
if err != nil {
panic(fmt.Sprintf("Could not generate args template for %s: %s", a.Name, err))
}
}
return "\n\n" + buf.String()
}
// ArgsUsage returns the value of the ArgsUsage property for a cli.Command for
// these arguments
func (args Arguments) ArgsUsage() string {
out := ""
for i, a := range args {
out += a.Decorate()
if i < len(args)-1 {
out += " "
}
}
return out
}
// AppHelpTemplate contains the modified template for the main app
var AppHelpTemplate = `## NAME
**{{.HelpName}}** -- {{.Usage}}
## USAGE
{{if .UsageText}}{{.UsageText}}{{else}}**{{.HelpName}}**{{if .Commands}} <command>{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}_[arguments]_{{end}}{{end}}{{if .Description}}
## DESCRIPTION
{{.Description}}{{end}}{{if .VisibleCommands}}
## COMMANDS
{{range .VisibleCategories}}{{if .Name}}{{.Name}}:{{end}}
|||
|---|---|{{range .VisibleCommands}}
| **{{join .Names ", "}}** | {{.Usage}} |{{end}}
{{end}}{{if .VisibleFlags}}{{end}}
## OPTIONS
{{range $index, $option := .VisibleFlags}}{{if $index}}
{{end}}{{$option}}
{{end}}{{end}}{{if .Copyright}}{{if len .Authors}}
## AUTHOR{{with $length := len .Authors}}{{if ne 1 $length}}S{{end}}{{end}}:
{{range $index, $author := .Authors}}{{if $index}}
{{end}}{{$author}}{{end}}{{end}}{{if .Version}}{{if not .HideVersion}}
## ONLINE
This documentation is available online at https://smallstep.com/docs/cli
## VERSION
{{.Version}}{{end}}{{end}}
## COPYRIGHT
{{.Copyright}}
{{end}}
`
// SubcommandHelpTemplate contains the modified template for a sub command
// Note that the weird "|||\n|---|---|" syntax sets up a markdown table with empty headers.
var SubcommandHelpTemplate = `## NAME
**{{.HelpName}}** -- {{.Usage}}
## USAGE
{{if .UsageText}}{{.UsageText}}{{else}}**{{.HelpName}}** <command>{{if .VisibleFlags}} _[options]_{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}_[arguments]_{{end}}{{end}}{{if .Description}}
## DESCRIPTION
{{.Description}}{{end}}
## COMMANDS
{{range .VisibleCategories}}{{if .Name}}{{.Name}}:{{end}}
|||
|---|---|{{range .VisibleCommands}}
| **{{join .Names ", "}}** | {{.Usage}} |{{end}}
{{end}}{{if .VisibleFlags}}
## OPTIONS
{{range .VisibleFlags}}
{{.}}
{{end}}{{end}}
`
// CommandHelpTemplate contains the modified template for a command
var CommandHelpTemplate = `## NAME
**{{.HelpName}}** -- {{.Usage}}
## USAGE
{{if .UsageText}}{{.UsageText}}{{else}}**{{.HelpName}}**{{if .VisibleFlags}} _[options]_{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}_[arguments]_{{end}}{{end}}{{if .Category}}
## CATEGORY
{{.Category}}{{end}}{{if .Description}}
## DESCRIPTION
{{.Description}}{{end}}{{if .VisibleFlags}}
## OPTIONS
{{range .VisibleFlags}}
{{.}}
{{end}}{{end}}
`
// FlagNamePrefixer converts a full flag name and its placeholder into the help
// message flag prefix. This is used by the default FlagStringer.
//
// This method clones urflave/cli functionality but adds a new line at the end.
func FlagNamePrefixer(fullName, placeholder string) string {
var prefixed string
parts := strings.Split(fullName, ",")
for i, name := range parts {
name = strings.Trim(name, " ")
prefixed += "**" + prefixFor(name) + name + "**"
if placeholder != "" {
prefixed += "=" + placeholder
}
if i < len(parts)-1 {
prefixed += ", "
}
}
//return "* " + prefixed + "\n"
return prefixed + "\n: "
}
func prefixFor(name string) (prefix string) {
if len(name) == 1 {
prefix = "-"
} else {
prefix = "--"
}
return
}