From e36f81afb0d6dfa9eb85d48d013fe74a3681df5f Mon Sep 17 00:00:00 2001 From: Eric Paris Date: Thu, 12 Feb 2015 18:12:07 -0500 Subject: [PATCH] New "example" section of commands We have a long and short description. This adds an "Example" section. Which can used to create better docs than putting it all in Long. --- command.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/command.go b/command.go index bf6f3c79..f7ddfe25 100644 --- a/command.go +++ b/command.go @@ -40,6 +40,8 @@ type Command struct { Short string // The long message shown in the 'help ' output. Long string + // Examples of how to use the command + Example string // Full set of flags flags *flag.FlagSet // Set of flags childrens of this command will inherit @@ -195,8 +197,11 @@ Usage: {{if .Runnable}} {{ .CommandPath}} [command]{{end}}{{if gt .Aliases 0}} Aliases: - {{.NameAndAliases}}{{end}} -{{ if .HasSubCommands}} + {{.NameAndAliases}} +{{end}}{{if .HasExample}} +Examples: +{{ .Example }} +{{end}}{{ if .HasSubCommands}} Available Commands: {{range .Commands}}{{if .Runnable}} {{rpad .Use .UsagePadding }} {{.Short}}{{end}}{{end}} {{end}} @@ -669,6 +674,10 @@ func (c *Command) NameAndAliases() string { return strings.Join(append([]string{c.Name()}, c.Aliases...), ", ") } +func (c *Command) HasExample() bool { + return len(c.Example) > 0 +} + // Determine if the command is itself runnable func (c *Command) Runnable() bool { return c.Run != nil