Commit 60e99c38 authored by Matt Bell's avatar Matt Bell Committed by Juan Batiz-Benet

commands/cli: Transitionary commit - Generate helptext from HelpText fields if they aren't empty

parent 56c2ddad
...@@ -23,6 +23,7 @@ const ( ...@@ -23,6 +23,7 @@ const (
type helpFields struct { type helpFields struct {
Indent string Indent string
Usage string
Path string Path string
ArgUsage string ArgUsage string
Tagline string Tagline string
...@@ -32,7 +33,7 @@ type helpFields struct { ...@@ -32,7 +33,7 @@ type helpFields struct {
Description string Description string
} }
const usageFormat = "{{.Path}}{{if .ArgUsage}} {{.ArgUsage}}{{end}} - {{.Tagline}}" const usageFormat = "{{if .Usage}}{{.Usage}}{{else}}{{.Path}}{{if .ArgUsage}} {{.ArgUsage}}{{end}} - {{.Tagline}}{{end}}"
const longHelpFormat = ` const longHelpFormat = `
{{.Indent}}{{template "usage" .}} {{.Indent}}{{template "usage" .}}
...@@ -101,6 +102,7 @@ func LongHelp(rootName string, root *cmds.Command, path []string, out io.Writer) ...@@ -101,6 +102,7 @@ func LongHelp(rootName string, root *cmds.Command, path []string, out io.Writer)
pathStr += " " + strings.Join(path, " ") pathStr += " " + strings.Join(path, " ")
} }
// TODO: get the fields from the HelpText struct by default (when commands are ported to use it)
fields := helpFields{ fields := helpFields{
Indent: indentStr, Indent: indentStr,
Path: pathStr, Path: pathStr,
...@@ -112,6 +114,17 @@ func LongHelp(rootName string, root *cmds.Command, path []string, out io.Writer) ...@@ -112,6 +114,17 @@ func LongHelp(rootName string, root *cmds.Command, path []string, out io.Writer)
Description: cmd.Help, Description: cmd.Help,
} }
// TODO: don't do these checks, just use these fields by default (when commands get ported to it)
if len(cmd.Helptext.Tagline) > 0 {
fields.Tagline = cmd.Helptext.Tagline
}
if len(cmd.Helptext.ShortDescription) > 0 {
fields.Description = cmd.Helptext.ShortDescription
}
if len(cmd.Helptext.Usage) > 0 {
fields.Usage = cmd.Helptext.Subcommands
}
// autogen fields that are empty // autogen fields that are empty
if len(cmd.ArgumentHelp) == 0 { if len(cmd.ArgumentHelp) == 0 {
fields.Arguments = strings.Join(argumentText(cmd), "\n") fields.Arguments = strings.Join(argumentText(cmd), "\n")
...@@ -151,6 +164,28 @@ func ShortHelp(rootName string, root *cmds.Command, path []string, out io.Writer ...@@ -151,6 +164,28 @@ func ShortHelp(rootName string, root *cmds.Command, path []string, out io.Writer
Description: cmd.Help, Description: cmd.Help,
} }
// TODO: don't do these checks, just use these fields by default (when commands get ported to it)
if len(cmd.Helptext.Tagline) > 0 {
fields.Tagline = cmd.Helptext.Tagline
}
if len(cmd.Helptext.Arguments) > 0 {
fields.Arguments = cmd.Helptext.Arguments
}
if len(cmd.Helptext.Options) > 0 {
fields.Options = cmd.Helptext.Options
}
if len(cmd.Helptext.Subcommands) > 0 {
fields.Subcommands = cmd.Helptext.Subcommands
}
if len(cmd.Helptext.LongDescription) > 0 {
fields.Description = cmd.Helptext.LongDescription
} else if len(cmd.Helptext.ShortDescription) > 0 {
fields.Description = cmd.Helptext.ShortDescription
}
if len(cmd.Helptext.Usage) > 0 {
fields.Usage = cmd.Helptext.Subcommands
}
fields.Description = indentString(fields.Description, indentStr) fields.Description = indentString(fields.Description, indentStr)
return shortHelpTemplate.Execute(out, fields) return shortHelpTemplate.Execute(out, fields)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment