@@ -29,7 +29,6 @@ type helpFields struct {
29
29
Indent string
30
30
Usage string
31
31
Path string
32
- ArgUsage string
33
32
Tagline string
34
33
Arguments string
35
34
Options string
@@ -49,7 +48,7 @@ type helpFields struct {
49
48
// `
50
49
func (f * helpFields ) TrimNewlines () {
51
50
f .Path = strings .Trim (f .Path , "\n " )
52
- f .ArgUsage = strings .Trim (f .ArgUsage , "\n " )
51
+ f .Usage = strings .Trim (f .Usage , "\n " )
53
52
f .Tagline = strings .Trim (f .Tagline , "\n " )
54
53
f .Arguments = strings .Trim (f .Arguments , "\n " )
55
54
f .Options = strings .Trim (f .Options , "\n " )
@@ -67,17 +66,16 @@ func (f *helpFields) IndentAll() {
67
66
return indentString (s , indentStr )
68
67
}
69
68
69
+ f .Usage = indent (f .Usage )
70
70
f .Arguments = indent (f .Arguments )
71
71
f .Options = indent (f .Options )
72
72
f .Synopsis = indent (f .Synopsis )
73
73
f .Subcommands = indent (f .Subcommands )
74
74
f .Description = indent (f .Description )
75
75
}
76
76
77
- const usageFormat = "{{if .Usage}}{{.Usage}}{{else}}{{.Path}}{{if .ArgUsage}} {{.ArgUsage}}{{end}} - {{.Tagline}}{{end}}"
78
-
79
77
const longHelpFormat = `USAGE
80
- {{.Indent}}{{template "usage" . }}
78
+ {{.Usage }}
81
79
82
80
{{if .Synopsis}}SYNOPSIS
83
81
{{.Synopsis}}
@@ -101,7 +99,7 @@ const longHelpFormat = `USAGE
101
99
{{end}}
102
100
`
103
101
const shortHelpFormat = `USAGE
104
- {{.Indent}}{{template "usage" . }}
102
+ {{.Usage }}
105
103
{{if .Synopsis}}
106
104
{{.Synopsis}}
107
105
{{end}}{{if .Description}}
@@ -114,14 +112,12 @@ Use '{{.Path}} --help' for more information about this command.
114
112
{{end}}
115
113
`
116
114
117
- var usageTemplate * template.Template
118
115
var longHelpTemplate * template.Template
119
116
var shortHelpTemplate * template.Template
120
117
121
118
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 ))
125
121
}
126
122
127
123
var ErrNoHelpRequested = errors .New ("no help requested" )
@@ -155,7 +151,6 @@ func LongHelp(rootName string, root *cmds.Command, path []string, out io.Writer)
155
151
fields := helpFields {
156
152
Indent : indentStr ,
157
153
Path : pathStr ,
158
- ArgUsage : usageText (cmd ),
159
154
Tagline : cmd .Helptext .Tagline ,
160
155
Arguments : cmd .Helptext .Arguments ,
161
156
Options : cmd .Helptext .Options ,
@@ -173,6 +168,11 @@ func LongHelp(rootName string, root *cmds.Command, path []string, out io.Writer)
173
168
}
174
169
175
170
// 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
+ }
176
176
if len (fields .Arguments ) == 0 {
177
177
fields .Arguments = strings .Join (argumentText (width , cmd ), "\n " )
178
178
}
@@ -215,18 +215,21 @@ func ShortHelp(rootName string, root *cmds.Command, path []string, out io.Writer
215
215
fields := helpFields {
216
216
Indent : indentStr ,
217
217
Path : pathStr ,
218
- ArgUsage : usageText (cmd ),
219
218
Tagline : cmd .Helptext .Tagline ,
220
219
Synopsis : cmd .Helptext .Synopsis ,
221
220
Description : cmd .Helptext .ShortDescription ,
222
221
Subcommands : cmd .Helptext .Subcommands ,
223
- Usage : cmd .Helptext .Usage ,
224
222
MoreHelp : (cmd != root ),
225
223
}
226
224
227
225
width := terminalWidth - len (indentStr )
228
226
229
227
// 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
+ }
230
233
if len (fields .Subcommands ) == 0 {
231
234
fields .Subcommands = strings .Join (subcommandText (width , cmd , rootName , path ), "\n " )
232
235
}
@@ -421,6 +424,17 @@ func subcommandText(width int, cmd *cmds.Command, rootName string, path []string
421
424
return lines
422
425
}
423
426
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
+
424
438
func usageText (cmd * cmds.Command ) string {
425
439
s := ""
426
440
for i , arg := range cmd .Arguments {
0 commit comments