1 //Copyright 2015 Red Hat Inc. All rights reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
26 "github.com/spf13/cobra"
29 func printOptionsReST(buf *bytes.Buffer, cmd *cobra.Command, name string) error {
30 flags := cmd.NonInheritedFlags()
33 buf.WriteString("Options\n")
34 buf.WriteString("~~~~~~~\n\n::\n\n")
39 parentFlags := cmd.InheritedFlags()
40 parentFlags.SetOutput(buf)
41 if parentFlags.HasFlags() {
42 buf.WriteString("Options inherited from parent commands\n")
43 buf.WriteString("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n::\n\n")
44 parentFlags.PrintDefaults()
50 // linkHandler for default ReST hyperlink markup
51 func defaultLinkHandler(name, ref string) string {
52 return fmt.Sprintf("`%s <%s.rst>`_", name, ref)
55 // GenReST creates reStructured Text output.
56 func GenReST(cmd *cobra.Command, w io.Writer) error {
57 return GenReSTCustom(cmd, w, defaultLinkHandler)
60 // GenReSTCustom creates custom reStructured Text output.
61 func GenReSTCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string, string) string) error {
62 cmd.InitDefaultHelpCmd()
63 cmd.InitDefaultHelpFlag()
65 buf := new(bytes.Buffer)
66 name := cmd.CommandPath()
73 ref := strings.Replace(name, " ", "_", -1)
75 buf.WriteString(".. _" + ref + ":\n\n")
76 buf.WriteString(name + "\n")
77 buf.WriteString(strings.Repeat("-", len(name)) + "\n\n")
78 buf.WriteString(short + "\n\n")
79 buf.WriteString("Synopsis\n")
80 buf.WriteString("~~~~~~~~\n\n")
81 buf.WriteString("\n" + long + "\n\n")
84 buf.WriteString(fmt.Sprintf("::\n\n %s\n\n", cmd.UseLine()))
87 if len(cmd.Example) > 0 {
88 buf.WriteString("Examples\n")
89 buf.WriteString("~~~~~~~~\n\n")
90 buf.WriteString(fmt.Sprintf("::\n\n%s\n\n", indentString(cmd.Example, " ")))
93 if err := printOptionsReST(buf, cmd, name); err != nil {
97 buf.WriteString("SEE ALSO\n")
98 buf.WriteString("~~~~~~~~\n\n")
100 parent := cmd.Parent()
101 pname := parent.CommandPath()
102 ref = strings.Replace(pname, " ", "_", -1)
103 buf.WriteString(fmt.Sprintf("* %s \t - %s\n", linkHandler(pname, ref), parent.Short))
104 cmd.VisitParents(func(c *cobra.Command) {
105 if c.DisableAutoGenTag {
106 cmd.DisableAutoGenTag = c.DisableAutoGenTag
111 children := cmd.Commands()
112 sort.Sort(byName(children))
114 for _, child := range children {
115 if !child.IsAvailableCommand() || child.IsAdditionalHelpTopicCommand() {
118 cname := name + " " + child.Name()
119 ref = strings.Replace(cname, " ", "_", -1)
120 buf.WriteString(fmt.Sprintf("* %s \t - %s\n", linkHandler(cname, ref), child.Short))
122 buf.WriteString("\n")
124 if !cmd.DisableAutoGenTag {
125 buf.WriteString("*Auto generated by spf13/cobra on " + time.Now().Format("2-Jan-2006") + "*\n")
127 _, err := buf.WriteTo(w)
131 // GenReSTTree will generate a ReST page for this command and all
132 // descendants in the directory given.
133 // This function may not work correctly if your command names have `-` in them.
134 // If you have `cmd` with two subcmds, `sub` and `sub-third`,
135 // and `sub` has a subcommand called `third`, it is undefined which
136 // help output will be in the file `cmd-sub-third.1`.
137 func GenReSTTree(cmd *cobra.Command, dir string) error {
138 emptyStr := func(s string) string { return "" }
139 return GenReSTTreeCustom(cmd, dir, emptyStr, defaultLinkHandler)
142 // GenReSTTreeCustom is the the same as GenReSTTree, but
143 // with custom filePrepender and linkHandler.
144 func GenReSTTreeCustom(cmd *cobra.Command, dir string, filePrepender func(string) string, linkHandler func(string, string) string) error {
145 for _, c := range cmd.Commands() {
146 if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
149 if err := GenReSTTreeCustom(c, dir, filePrepender, linkHandler); err != nil {
154 basename := strings.Replace(cmd.CommandPath(), " ", "_", -1) + ".rst"
155 filename := filepath.Join(dir, basename)
156 f, err := os.Create(filename)
162 if _, err := io.WriteString(f, filePrepender(filename)); err != nil {
165 if err := GenReSTCustom(cmd, f, linkHandler); err != nil {
171 // adapted from: https://github.com/kr/text/blob/main/indent.go
172 func indentString(s, p string) string {
177 for _, c := range b {
178 if bol && c != '\n' {
179 res = append(res, prefix...)