commands/cli: Added short help text function
Matt Bell committed
Nov 11, 2014 at 22:13 UTC
93c69a83e839cecc0c906d6c56562e15bc1e3545
1 file changed
+40
-1
commands/cli/helptext.go
+40
-1
@@ -57,9 +57,17 @@ const longHelpFormat = `
57
58
{{end}}
59
`
60
+const shortHelpFormat = `USAGE:
61
+{{.Indent}}{{template "usage" .}}
62
+{{if .Description}}
63
+{{.Indent}}{{.Description}}
64
+{{end}}
65
+Use '{{.Path}} --help' for more information about this command.
66
+`
67
61
-var longHelpTemplate *template.Template
68
var usageTemplate *template.Template
69
+var longHelpTemplate *template.Template
70
+var shortHelpTemplate *template.Template
71
72
func init() {
73
tmpl, err := template.New("usage").Parse(usageFormat)
@@ -73,6 +81,12 @@ func init() {
81
panic(err)
82
}
83
longHelpTemplate = tmpl
84
+
85
+ tmpl, err = usageTemplate.New("shortHelp").Parse(shortHelpFormat)
86
+ if err != nil {
87
+ panic(err)
88
+ }
89
+ shortHelpTemplate = tmpl
90
}
91
92
// LongHelp returns a formatted CLI helptext string, generated for the given command
@@ -117,6 +131,31 @@ func LongHelp(rootName string, root *cmds.Command, path []string, out io.Writer)
131
return longHelpTemplate.Execute(out, fields)
132
}
133
134
+// ShortHelp returns a formatted CLI helptext string, generated for the given command
135
+func ShortHelp(rootName string, root *cmds.Command, path []string, out io.Writer) error {
136
+ cmd, err := root.Get(path)
137
+ if err != nil {
138
+ return err
139
+ }
140
+
141
+ pathStr := rootName
142
+ if len(path) > 0 {
143
+ pathStr += " " + strings.Join(path, " ")
144
+ }
145
+
146
+ fields := helpFields{
147
+ Indent: indentStr,
148
+ Path: pathStr,
149
+ ArgUsage: usageText(cmd),
150
+ Tagline: cmd.Description,
151
+ Description: cmd.Help,
152
+ }
153
+
154
+ fields.Description = indentString(fields.Description, indentStr)
155
+
156
+ return shortHelpTemplate.Execute(out, fields)
157
+}
158
+
159
func argumentText(cmd *cmds.Command) []string {
160
lines := make([]string, len(cmd.Arguments))
161