MCPcopy
hub / github.com/spf13/cobra / GenMarkdownCustom

Function GenMarkdownCustom

doc/md_docs.go:57–117  ·  view source on GitHub ↗

GenMarkdownCustom creates custom markdown output.

(cmd *cobra.Command, w io.Writer, linkHandler func(string) string)

Source from the content-addressed store, hash-verified

55
56// GenMarkdownCustom creates custom markdown output.
57func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error {
58 cmd.InitDefaultHelpCmd()
59 cmd.InitDefaultHelpFlag()
60
61 buf := new(bytes.Buffer)
62 name := cmd.CommandPath()
63
64 buf.WriteString("## " + name + "\n\n")
65 buf.WriteString(cmd.Short + "\n\n")
66 if len(cmd.Long) > 0 {
67 buf.WriteString("### Synopsis\n\n")
68 buf.WriteString(cmd.Long + "\n\n")
69 }
70
71 if cmd.Runnable() {
72 fmt.Fprintf(buf, "```\n%s\n```\n\n", cmd.UseLine())
73 }
74
75 if len(cmd.Example) > 0 {
76 buf.WriteString("### Examples\n\n")
77 fmt.Fprintf(buf, "```\n%s\n```\n\n", cmd.Example)
78 }
79
80 if err := printOptions(buf, cmd, name); err != nil {
81 return err
82 }
83 if hasSeeAlso(cmd) {
84 buf.WriteString("### SEE ALSO\n\n")
85 if cmd.HasParent() {
86 parent := cmd.Parent()
87 pname := parent.CommandPath()
88 link := pname + markdownExtension
89 link = strings.ReplaceAll(link, " ", "_")
90 fmt.Fprintf(buf, "* [%s](%s)\t - %s\n", pname, linkHandler(link), parent.Short)
91 cmd.VisitParents(func(c *cobra.Command) {
92 if c.DisableAutoGenTag {
93 cmd.DisableAutoGenTag = c.DisableAutoGenTag
94 }
95 })
96 }
97
98 children := cmd.Commands()
99 sort.Sort(byName(children))
100
101 for _, child := range children {
102 if !child.IsAvailableCommand() || child.IsAdditionalHelpTopicCommand() {
103 continue
104 }
105 cname := name + " " + child.Name()
106 link := cname + markdownExtension
107 link = strings.ReplaceAll(link, " ", "_")
108 fmt.Fprintf(buf, "* [%s](%s)\t - %s\n", cname, linkHandler(link), child.Short)
109 }
110 buf.WriteString("\n")
111 }
112 if !cmd.DisableAutoGenTag {
113 buf.WriteString("###### Auto generated by spf13/cobra on " + time.Now().Format("2-Jan-2006") + "\n")
114 }

Callers 2

GenMarkdownFunction · 0.85
GenMarkdownTreeCustomFunction · 0.85

Calls 15

printOptionsFunction · 0.85
hasSeeAlsoFunction · 0.85
byNameTypeAlias · 0.85
InitDefaultHelpCmdMethod · 0.80
InitDefaultHelpFlagMethod · 0.80
CommandPathMethod · 0.80
RunnableMethod · 0.80
UseLineMethod · 0.80
HasParentMethod · 0.80
ParentMethod · 0.80
VisitParentsMethod · 0.80
CommandsMethod · 0.80

Tested by

no test coverage detected