mirror of
https://github.com/spf13/cobra
synced 2025-05-06 13:27:26 +00:00
Generating additional help topics when generating markdown
This commit is contained in:
parent
b80588d523
commit
ace5fe16e9
1 changed files with 20 additions and 8 deletions
|
@ -51,11 +51,21 @@ func GenMarkdown(cmd *cobra.Command, w io.Writer) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenMarkdownCustom creates custom markdown output.
|
// GenMarkdownCustom creates custom markdown output.
|
||||||
func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error {
|
func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) (err error) {
|
||||||
cmd.InitDefaultHelpCmd()
|
cmd.InitDefaultHelpCmd()
|
||||||
cmd.InitDefaultHelpFlag()
|
cmd.InitDefaultHelpFlag()
|
||||||
|
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
|
defer func() {
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !cmd.DisableAutoGenTag {
|
||||||
|
buf.WriteString("###### Auto generated by spf13/cobra on " + time.Now().Format("2-Jan-2006") + "\n")
|
||||||
|
}
|
||||||
|
_, err = buf.WriteTo(w)
|
||||||
|
}()
|
||||||
|
|
||||||
name := cmd.CommandPath()
|
name := cmd.CommandPath()
|
||||||
|
|
||||||
short := cmd.Short
|
short := cmd.Short
|
||||||
|
@ -64,6 +74,11 @@ func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string)
|
||||||
long = short
|
long = short
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cmd.IsAdditionalHelpTopicCommand() {
|
||||||
|
buf.WriteString(long + "\n\n")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
buf.WriteString("## " + name + "\n\n")
|
buf.WriteString("## " + name + "\n\n")
|
||||||
buf.WriteString(short + "\n\n")
|
buf.WriteString(short + "\n\n")
|
||||||
buf.WriteString("### Synopsis\n\n")
|
buf.WriteString("### Synopsis\n\n")
|
||||||
|
@ -100,7 +115,7 @@ func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string)
|
||||||
sort.Sort(byName(children))
|
sort.Sort(byName(children))
|
||||||
|
|
||||||
for _, child := range children {
|
for _, child := range children {
|
||||||
if !child.IsAvailableCommand() || child.IsAdditionalHelpTopicCommand() {
|
if !child.IsAvailableCommand() && !child.IsAdditionalHelpTopicCommand() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
cname := name + " " + child.Name()
|
cname := name + " " + child.Name()
|
||||||
|
@ -110,11 +125,8 @@ func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string)
|
||||||
}
|
}
|
||||||
buf.WriteString("\n")
|
buf.WriteString("\n")
|
||||||
}
|
}
|
||||||
if !cmd.DisableAutoGenTag {
|
|
||||||
buf.WriteString("###### Auto generated by spf13/cobra on " + time.Now().Format("2-Jan-2006") + "\n")
|
return nil
|
||||||
}
|
|
||||||
_, err := buf.WriteTo(w)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenMarkdownTree will generate a markdown page for this command and all
|
// GenMarkdownTree will generate a markdown page for this command and all
|
||||||
|
@ -133,7 +145,7 @@ func GenMarkdownTree(cmd *cobra.Command, dir string) error {
|
||||||
// with custom filePrepender and linkHandler.
|
// with custom filePrepender and linkHandler.
|
||||||
func GenMarkdownTreeCustom(cmd *cobra.Command, dir string, filePrepender, linkHandler func(string) string) error {
|
func GenMarkdownTreeCustom(cmd *cobra.Command, dir string, filePrepender, linkHandler func(string) string) error {
|
||||||
for _, c := range cmd.Commands() {
|
for _, c := range cmd.Commands() {
|
||||||
if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
|
if !c.IsAvailableCommand() && !c.IsAdditionalHelpTopicCommand() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if err := GenMarkdownTreeCustom(c, dir, filePrepender, linkHandler); err != nil {
|
if err := GenMarkdownTreeCustom(c, dir, filePrepender, linkHandler); err != nil {
|
||||||
|
|
Loading…
Add table
Reference in a new issue