mirror of
https://github.com/spf13/cobra
synced 2024-11-16 18:57:08 +00:00
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.
This commit is contained in:
parent
07a9dc0024
commit
e36f81afb0
1 changed files with 11 additions and 2 deletions
13
command.go
13
command.go
|
@ -40,6 +40,8 @@ type Command struct {
|
||||||
Short string
|
Short string
|
||||||
// The long message shown in the 'help <this-command>' output.
|
// The long message shown in the 'help <this-command>' output.
|
||||||
Long string
|
Long string
|
||||||
|
// Examples of how to use the command
|
||||||
|
Example string
|
||||||
// Full set of flags
|
// Full set of flags
|
||||||
flags *flag.FlagSet
|
flags *flag.FlagSet
|
||||||
// Set of flags childrens of this command will inherit
|
// Set of flags childrens of this command will inherit
|
||||||
|
@ -195,8 +197,11 @@ Usage: {{if .Runnable}}
|
||||||
{{ .CommandPath}} [command]{{end}}{{if gt .Aliases 0}}
|
{{ .CommandPath}} [command]{{end}}{{if gt .Aliases 0}}
|
||||||
|
|
||||||
Aliases:
|
Aliases:
|
||||||
{{.NameAndAliases}}{{end}}
|
{{.NameAndAliases}}
|
||||||
{{ if .HasSubCommands}}
|
{{end}}{{if .HasExample}}
|
||||||
|
Examples:
|
||||||
|
{{ .Example }}
|
||||||
|
{{end}}{{ if .HasSubCommands}}
|
||||||
Available Commands: {{range .Commands}}{{if .Runnable}}
|
Available Commands: {{range .Commands}}{{if .Runnable}}
|
||||||
{{rpad .Use .UsagePadding }} {{.Short}}{{end}}{{end}}
|
{{rpad .Use .UsagePadding }} {{.Short}}{{end}}{{end}}
|
||||||
{{end}}
|
{{end}}
|
||||||
|
@ -669,6 +674,10 @@ func (c *Command) NameAndAliases() string {
|
||||||
return strings.Join(append([]string{c.Name()}, c.Aliases...), ", ")
|
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
|
// Determine if the command is itself runnable
|
||||||
func (c *Command) Runnable() bool {
|
func (c *Command) Runnable() bool {
|
||||||
return c.Run != nil
|
return c.Run != nil
|
||||||
|
|
Loading…
Reference in a new issue