Skip to content

Commit ec412e7

Browse files
Stebalienhannahhoward
authored andcommitted
wrap usage text
1 parent 2bd0524 commit ec412e7

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

cli/helptext.go

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ type helpFields struct {
2929
Indent string
3030
Usage string
3131
Path string
32-
ArgUsage string
3332
Tagline string
3433
Arguments string
3534
Options string
@@ -49,7 +48,7 @@ type helpFields struct {
4948
// `
5049
func (f *helpFields) TrimNewlines() {
5150
f.Path = strings.Trim(f.Path, "\n")
52-
f.ArgUsage = strings.Trim(f.ArgUsage, "\n")
51+
f.Usage = strings.Trim(f.Usage, "\n")
5352
f.Tagline = strings.Trim(f.Tagline, "\n")
5453
f.Arguments = strings.Trim(f.Arguments, "\n")
5554
f.Options = strings.Trim(f.Options, "\n")
@@ -67,17 +66,16 @@ func (f *helpFields) IndentAll() {
6766
return indentString(s, indentStr)
6867
}
6968

69+
f.Usage = indent(f.Usage)
7070
f.Arguments = indent(f.Arguments)
7171
f.Options = indent(f.Options)
7272
f.Synopsis = indent(f.Synopsis)
7373
f.Subcommands = indent(f.Subcommands)
7474
f.Description = indent(f.Description)
7575
}
7676

77-
const usageFormat = "{{if .Usage}}{{.Usage}}{{else}}{{.Path}}{{if .ArgUsage}} {{.ArgUsage}}{{end}} - {{.Tagline}}{{end}}"
78-
7977
const longHelpFormat = `USAGE
80-
{{.Indent}}{{template "usage" .}}
78+
{{.Usage}}
8179
8280
{{if .Synopsis}}SYNOPSIS
8381
{{.Synopsis}}
@@ -101,7 +99,7 @@ const longHelpFormat = `USAGE
10199
{{end}}
102100
`
103101
const shortHelpFormat = `USAGE
104-
{{.Indent}}{{template "usage" .}}
102+
{{.Usage}}
105103
{{if .Synopsis}}
106104
{{.Synopsis}}
107105
{{end}}{{if .Description}}
@@ -114,14 +112,12 @@ Use '{{.Path}} --help' for more information about this command.
114112
{{end}}
115113
`
116114

117-
var usageTemplate *template.Template
118115
var longHelpTemplate *template.Template
119116
var shortHelpTemplate *template.Template
120117

121118
func init() {
122-
usageTemplate = template.Must(template.New("usage").Parse(usageFormat))
123-
longHelpTemplate = template.Must(usageTemplate.New("longHelp").Parse(longHelpFormat))
124-
shortHelpTemplate = template.Must(usageTemplate.New("shortHelp").Parse(shortHelpFormat))
119+
longHelpTemplate = template.Must(template.New("longHelp").Parse(longHelpFormat))
120+
shortHelpTemplate = template.Must(template.New("shortHelp").Parse(shortHelpFormat))
125121
}
126122

127123
var ErrNoHelpRequested = errors.New("no help requested")
@@ -155,7 +151,6 @@ func LongHelp(rootName string, root *cmds.Command, path []string, out io.Writer)
155151
fields := helpFields{
156152
Indent: indentStr,
157153
Path: pathStr,
158-
ArgUsage: usageText(cmd),
159154
Tagline: cmd.Helptext.Tagline,
160155
Arguments: cmd.Helptext.Arguments,
161156
Options: cmd.Helptext.Options,
@@ -173,6 +168,11 @@ func LongHelp(rootName string, root *cmds.Command, path []string, out io.Writer)
173168
}
174169

175170
// autogen fields that are empty
171+
if len(cmd.Helptext.Usage) > 0 {
172+
fields.Usage = cmd.Helptext.Usage
173+
} else {
174+
fields.Usage = commandUsageText(width, cmd, rootName, path)
175+
}
176176
if len(fields.Arguments) == 0 {
177177
fields.Arguments = strings.Join(argumentText(width, cmd), "\n")
178178
}
@@ -215,18 +215,21 @@ func ShortHelp(rootName string, root *cmds.Command, path []string, out io.Writer
215215
fields := helpFields{
216216
Indent: indentStr,
217217
Path: pathStr,
218-
ArgUsage: usageText(cmd),
219218
Tagline: cmd.Helptext.Tagline,
220219
Synopsis: cmd.Helptext.Synopsis,
221220
Description: cmd.Helptext.ShortDescription,
222221
Subcommands: cmd.Helptext.Subcommands,
223-
Usage: cmd.Helptext.Usage,
224222
MoreHelp: (cmd != root),
225223
}
226224

227225
width := terminalWidth - len(indentStr)
228226

229227
// autogen fields that are empty
228+
if len(cmd.Helptext.Usage) > 0 {
229+
fields.Usage = cmd.Helptext.Usage
230+
} else {
231+
fields.Usage = commandUsageText(width, cmd, rootName, path)
232+
}
230233
if len(fields.Subcommands) == 0 {
231234
fields.Subcommands = strings.Join(subcommandText(width, cmd, rootName, path), "\n")
232235
}
@@ -421,6 +424,17 @@ func subcommandText(width int, cmd *cmds.Command, rootName string, path []string
421424
return lines
422425
}
423426

427+
func commandUsageText(width int, cmd *cmds.Command, rootName string, path []string) string {
428+
text := fmt.Sprintf("%v %v", rootName, strings.Join(path, " "))
429+
argUsage := usageText(cmd)
430+
if len(argUsage) > 0 {
431+
text += " " + argUsage
432+
}
433+
text += " - "
434+
text = appendWrapped(text, cmd.Helptext.Tagline, width)
435+
return text
436+
}
437+
424438
func usageText(cmd *cmds.Command) string {
425439
s := ""
426440
for i, arg := range cmd.Arguments {

0 commit comments

Comments
 (0)