Use the 'example' section of cobra commands

This makes examples their own section in the docs, if they are defined.
This commit is contained in:
Eric Paris 2015-02-20 16:27:56 -05:00
parent c2c86b0a6d
commit 781ca91a59
2 changed files with 15 additions and 4 deletions

View File

@ -31,9 +31,6 @@ import (
func printOptions(out *bytes.Buffer, command *cobra.Command, name string) {
flags := command.NonInheritedFlags()
flags.SetOutput(out)
if command.Runnable() {
fmt.Fprintf(out, "%s\n\n", command.UseLine())
}
if flags.HasFlags() {
fmt.Fprintf(out, "### Options\n\n```\n")
flags.PrintDefaults()
@ -68,7 +65,16 @@ func genMarkdown(command *cobra.Command, parent, docsDir string) {
fmt.Fprintf(out, "## %s\n\n", name)
fmt.Fprintf(out, "%s\n\n", short)
fmt.Fprintf(out, "### Synopsis\n\n")
fmt.Fprintf(out, "%s\n\n", long)
fmt.Fprintf(out, "```\n%s\n```\n\n", long)
if command.Runnable() {
fmt.Fprintf(out, "%s\n\n", command.UseLine())
}
if len(command.Example) > 0 {
fmt.Fprintf(out, "### Examples\n\n")
fmt.Fprintf(out, "```\n%s\n```\n\n", command.Example)
}
printOptions(out, command, name)

View File

@ -133,6 +133,11 @@ func genMarkdown(command *cobra.Command, parent, docsDir string) {
preamble(out, name, short, long)
printOptions(out, command)
if len(command.Example) > 0 {
fmt.Fprintf(out, "# EXAMPLE\n")
fmt.Fprintf(out, "```\n%s\n```\n", command.Example)
}
if len(command.Commands()) > 0 || len(parent) > 0 {
fmt.Fprintf(out, "# SEE ALSO\n")
if len(parent) > 0 {