commands/cli: Transitionary commit - Generate helptext from HelpText fields if they aren't empty
Matt Bell committed
Nov 12, 2014 at 01:26 UTC
47987f5df5a481d9c7f62886c62c598ef9de2754
1 file changed
+36
-1
commands/cli/helptext.go
+36
-1
@@ -23,6 +23,7 @@ const (
23
24
type helpFields struct {
25
Indent string
26
+ Usage string
27
Path string
28
ArgUsage string
29
Tagline string
@@ -32,7 +33,7 @@ type helpFields struct {
33
Description string
34
}
35
35
-const usageFormat = "{{.Path}}{{if .ArgUsage}} {{.ArgUsage}}{{end}} - {{.Tagline}}"
36
+const usageFormat = "{{if .Usage}}{{.Usage}}{{else}}{{.Path}}{{if .ArgUsage}} {{.ArgUsage}}{{end}} - {{.Tagline}}{{end}}"
37
38
const longHelpFormat = `
39
{{.Indent}}{{template "usage" .}}
@@ -101,6 +102,7 @@ func LongHelp(rootName string, root *cmds.Command, path []string, out io.Writer)
102
pathStr += " " + strings.Join(path, " ")
103
}
104
105
+ // TODO: get the fields from the HelpText struct by default (when commands are ported to use it)
106
fields := helpFields{
107
Indent: indentStr,
108
Path: pathStr,
@@ -112,6 +114,17 @@ func LongHelp(rootName string, root *cmds.Command, path []string, out io.Writer)
114
Description: cmd.Help,
115
}
116
117
+ // TODO: don't do these checks, just use these fields by default (when commands get ported to it)
118
+ if len(cmd.Helptext.Tagline) > 0 {
119
+ fields.Tagline = cmd.Helptext.Tagline
120
+ }
121
+ if len(cmd.Helptext.ShortDescription) > 0 {
122
+ fields.Description = cmd.Helptext.ShortDescription
123
+ }
124
+ if len(cmd.Helptext.Usage) > 0 {
125
+ fields.Usage = cmd.Helptext.Subcommands
126
+ }
127
+
128
// autogen fields that are empty
129
if len(cmd.ArgumentHelp) == 0 {
130
fields.Arguments = strings.Join(argumentText(cmd), "\n")
@@ -151,6 +164,28 @@ func ShortHelp(rootName string, root *cmds.Command, path []string, out io.Writer
164
Description: cmd.Help,
165
}
166
167
+ // TODO: don't do these checks, just use these fields by default (when commands get ported to it)
168
+ if len(cmd.Helptext.Tagline) > 0 {
169
+ fields.Tagline = cmd.Helptext.Tagline
170
+ }
171
+ if len(cmd.Helptext.Arguments) > 0 {
172
+ fields.Arguments = cmd.Helptext.Arguments
173
+ }
174
+ if len(cmd.Helptext.Options) > 0 {
175
+ fields.Options = cmd.Helptext.Options
176
+ }
177
+ if len(cmd.Helptext.Subcommands) > 0 {
178
+ fields.Subcommands = cmd.Helptext.Subcommands
179
+ }
180
+ if len(cmd.Helptext.LongDescription) > 0 {
181
+ fields.Description = cmd.Helptext.LongDescription
182
+ } else if len(cmd.Helptext.ShortDescription) > 0 {
183
+ fields.Description = cmd.Helptext.ShortDescription
184
+ }
185
+ if len(cmd.Helptext.Usage) > 0 {
186
+ fields.Usage = cmd.Helptext.Subcommands
187
+ }
188
+
189
fields.Description = indentString(fields.Description, indentStr)
190
191
return shortHelpTemplate.Execute(out, fields)