man_docs.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // Copyright 2015 Red Hat Inc. All rights reserved.
  2. //
  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
  7. //
  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.
  13. package cobra
  14. import (
  15. "bytes"
  16. "fmt"
  17. "os"
  18. "sort"
  19. "strings"
  20. "time"
  21. mangen "github.com/cpuguy83/go-md2man/md2man"
  22. "github.com/spf13/pflag"
  23. )
  24. // GenManTree will call cmd.GenManTree(header, dir)
  25. func GenManTree(cmd *Command, header *GenManHeader, dir string) {
  26. cmd.GenManTree(header, dir)
  27. }
  28. // GenManTree will generate a man page for this command and all decendants
  29. // in the directory given. The header may be nil. This function may not work
  30. // correctly if your command names have - in them. If you have `cmd` with two
  31. // subcmds, `sub` and `sub-third`. And `sub` has a subcommand called `third`
  32. // it is undefined which help output will be in the file `cmd-sub-third.1`.
  33. func (cmd *Command) GenManTree(header *GenManHeader, dir string) {
  34. if header == nil {
  35. header = &GenManHeader{}
  36. }
  37. for _, c := range cmd.Commands() {
  38. if !c.IsAvailableCommand() || c == cmd.helpCommand {
  39. continue
  40. }
  41. GenManTree(c, header, dir)
  42. }
  43. out := new(bytes.Buffer)
  44. needToResetTitle := header.Title == ""
  45. cmd.GenMan(header, out)
  46. if needToResetTitle {
  47. header.Title = ""
  48. }
  49. filename := cmd.CommandPath()
  50. filename = dir + strings.Replace(filename, " ", "-", -1) + ".1"
  51. outFile, err := os.Create(filename)
  52. if err != nil {
  53. fmt.Println(err)
  54. os.Exit(1)
  55. }
  56. defer outFile.Close()
  57. _, err = outFile.Write(out.Bytes())
  58. if err != nil {
  59. fmt.Println(err)
  60. os.Exit(1)
  61. }
  62. }
  63. // GenManHeader is a lot like the .TH header at the start of man pages. These
  64. // include the title, section, date, source, and manual. We will use the
  65. // current time if Date if unset and will use "Auto generated by spf13/cobra"
  66. // if the Source is unset.
  67. type GenManHeader struct {
  68. Title string
  69. Section string
  70. Date *time.Time
  71. date string
  72. Source string
  73. Manual string
  74. }
  75. // GenMan will call cmd.GenMan(header, out)
  76. func GenMan(cmd *Command, header *GenManHeader, out *bytes.Buffer) {
  77. cmd.GenMan(header, out)
  78. }
  79. // GenMan will generate a man page for the given command in the out buffer.
  80. // The header argument may be nil, however obviously out may not.
  81. func (cmd *Command) GenMan(header *GenManHeader, out *bytes.Buffer) {
  82. if header == nil {
  83. header = &GenManHeader{}
  84. }
  85. buf := genMarkdown(cmd, header)
  86. final := mangen.Render(buf)
  87. out.Write(final)
  88. }
  89. func fillHeader(header *GenManHeader, name string) {
  90. if header.Title == "" {
  91. header.Title = strings.ToUpper(strings.Replace(name, " ", "\\-", -1))
  92. }
  93. if header.Section == "" {
  94. header.Section = "1"
  95. }
  96. if header.Date == nil {
  97. now := time.Now()
  98. header.Date = &now
  99. }
  100. header.date = (*header.Date).Format("Jan 2006")
  101. if header.Source == "" {
  102. header.Source = "Auto generated by spf13/cobra"
  103. }
  104. }
  105. func manPreamble(out *bytes.Buffer, header *GenManHeader, name, short, long string) {
  106. dashName := strings.Replace(name, " ", "-", -1)
  107. fmt.Fprintf(out, `%% %s(%s)%s
  108. %% %s
  109. %% %s
  110. # NAME
  111. `, header.Title, header.Section, header.date, header.Source, header.Manual)
  112. fmt.Fprintf(out, "%s \\- %s\n\n", dashName, short)
  113. fmt.Fprintf(out, "# SYNOPSIS\n")
  114. fmt.Fprintf(out, "**%s** [OPTIONS]\n\n", name)
  115. fmt.Fprintf(out, "# DESCRIPTION\n")
  116. fmt.Fprintf(out, "%s\n\n", long)
  117. }
  118. func manPrintFlags(out *bytes.Buffer, flags *pflag.FlagSet) {
  119. flags.VisitAll(func(flag *pflag.Flag) {
  120. if len(flag.Deprecated) > 0 || flag.Hidden {
  121. return
  122. }
  123. format := ""
  124. if len(flag.Shorthand) > 0 {
  125. format = "**-%s**, **--%s**"
  126. } else {
  127. format = "%s**--%s**"
  128. }
  129. if len(flag.NoOptDefVal) > 0 {
  130. format = format + "["
  131. }
  132. if flag.Value.Type() == "string" {
  133. // put quotes on the value
  134. format = format + "=%q"
  135. } else {
  136. format = format + "=%s"
  137. }
  138. if len(flag.NoOptDefVal) > 0 {
  139. format = format + "]"
  140. }
  141. format = format + "\n\t%s\n\n"
  142. fmt.Fprintf(out, format, flag.Shorthand, flag.Name, flag.DefValue, flag.Usage)
  143. })
  144. }
  145. func manPrintOptions(out *bytes.Buffer, command *Command) {
  146. flags := command.NonInheritedFlags()
  147. if flags.HasFlags() {
  148. fmt.Fprintf(out, "# OPTIONS\n")
  149. manPrintFlags(out, flags)
  150. fmt.Fprintf(out, "\n")
  151. }
  152. flags = command.InheritedFlags()
  153. if flags.HasFlags() {
  154. fmt.Fprintf(out, "# OPTIONS INHERITED FROM PARENT COMMANDS\n")
  155. manPrintFlags(out, flags)
  156. fmt.Fprintf(out, "\n")
  157. }
  158. }
  159. func genMarkdown(cmd *Command, header *GenManHeader) []byte {
  160. // something like `rootcmd subcmd1 subcmd2`
  161. commandName := cmd.CommandPath()
  162. // something like `rootcmd-subcmd1-subcmd2`
  163. dashCommandName := strings.Replace(commandName, " ", "-", -1)
  164. fillHeader(header, commandName)
  165. buf := new(bytes.Buffer)
  166. short := cmd.Short
  167. long := cmd.Long
  168. if len(long) == 0 {
  169. long = short
  170. }
  171. manPreamble(buf, header, commandName, short, long)
  172. manPrintOptions(buf, cmd)
  173. if len(cmd.Example) > 0 {
  174. fmt.Fprintf(buf, "# EXAMPLE\n")
  175. fmt.Fprintf(buf, "```\n%s\n```\n", cmd.Example)
  176. }
  177. if cmd.hasSeeAlso() {
  178. fmt.Fprintf(buf, "# SEE ALSO\n")
  179. if cmd.HasParent() {
  180. parentPath := cmd.Parent().CommandPath()
  181. dashParentPath := strings.Replace(parentPath, " ", "-", -1)
  182. fmt.Fprintf(buf, "**%s(%s)**", dashParentPath, header.Section)
  183. cmd.VisitParents(func(c *Command) {
  184. if c.DisableAutoGenTag {
  185. cmd.DisableAutoGenTag = c.DisableAutoGenTag
  186. }
  187. })
  188. }
  189. children := cmd.Commands()
  190. sort.Sort(byName(children))
  191. for i, c := range children {
  192. if !c.IsAvailableCommand() || c == cmd.helpCommand {
  193. continue
  194. }
  195. if cmd.HasParent() || i > 0 {
  196. fmt.Fprintf(buf, ", ")
  197. }
  198. fmt.Fprintf(buf, "**%s-%s(%s)**", dashCommandName, c.Name(), header.Section)
  199. }
  200. fmt.Fprintf(buf, "\n")
  201. }
  202. if !cmd.DisableAutoGenTag {
  203. fmt.Fprintf(buf, "# HISTORY\n%s Auto generated by spf13/cobra\n", header.Date.Format("2-Jan-2006"))
  204. }
  205. return buf.Bytes()
  206. }