help.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. package cli
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "strings"
  7. "text/tabwriter"
  8. "text/template"
  9. )
  10. // AppHelpTemplate is the text template for the Default help topic.
  11. // cli.go uses text/template to render templates. You can
  12. // render custom help text by setting this variable.
  13. var AppHelpTemplate = `NAME:
  14. {{.Name}} - {{.Usage}}
  15. USAGE:
  16. {{if .UsageText}}{{.UsageText}}{{else}}{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}
  17. {{if .Version}}{{if not .HideVersion}}
  18. VERSION:
  19. {{.Version}}
  20. {{end}}{{end}}{{if len .Authors}}
  21. AUTHOR(S):
  22. {{range .Authors}}{{.}}{{end}}
  23. {{end}}{{if .VisibleCommands}}
  24. COMMANDS:{{range .VisibleCategories}}{{if .Name}}
  25. {{.Name}}:{{end}}{{range .VisibleCommands}}
  26. {{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}
  27. {{end}}{{end}}{{if .VisibleFlags}}
  28. GLOBAL OPTIONS:
  29. {{range .VisibleFlags}}{{.}}
  30. {{end}}{{end}}{{if .Copyright}}
  31. COPYRIGHT:
  32. {{.Copyright}}
  33. {{end}}
  34. `
  35. // CommandHelpTemplate is the text template for the command help topic.
  36. // cli.go uses text/template to render templates. You can
  37. // render custom help text by setting this variable.
  38. var CommandHelpTemplate = `NAME:
  39. {{.HelpName}} - {{.Usage}}
  40. USAGE:
  41. {{.HelpName}}{{if .VisibleFlags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{if .Category}}
  42. CATEGORY:
  43. {{.Category}}{{end}}{{if .Description}}
  44. DESCRIPTION:
  45. {{.Description}}{{end}}{{if .VisibleFlags}}
  46. OPTIONS:
  47. {{range .VisibleFlags}}{{.}}
  48. {{end}}{{end}}
  49. `
  50. // SubcommandHelpTemplate is the text template for the subcommand help topic.
  51. // cli.go uses text/template to render templates. You can
  52. // render custom help text by setting this variable.
  53. var SubcommandHelpTemplate = `NAME:
  54. {{.HelpName}} - {{.Usage}}
  55. USAGE:
  56. {{.HelpName}} command{{if .VisibleFlags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}
  57. COMMANDS:{{range .VisibleCategories}}{{if .Name}}
  58. {{.Name}}:{{end}}{{range .VisibleCommands}}
  59. {{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}
  60. {{end}}{{if .VisibleFlags}}
  61. OPTIONS:
  62. {{range .VisibleFlags}}{{.}}
  63. {{end}}{{end}}
  64. `
  65. var helpCommand = Command{
  66. Name: "help",
  67. Aliases: []string{"h"},
  68. Usage: "Shows a list of commands or help for one command",
  69. ArgsUsage: "[command]",
  70. Action: func(c *Context) error {
  71. args := c.Args()
  72. if args.Present() {
  73. return ShowCommandHelp(c, args.First())
  74. }
  75. ShowAppHelp(c)
  76. return nil
  77. },
  78. }
  79. var helpSubcommand = Command{
  80. Name: "help",
  81. Aliases: []string{"h"},
  82. Usage: "Shows a list of commands or help for one command",
  83. ArgsUsage: "[command]",
  84. Action: func(c *Context) error {
  85. args := c.Args()
  86. if args.Present() {
  87. return ShowCommandHelp(c, args.First())
  88. }
  89. return ShowSubcommandHelp(c)
  90. },
  91. }
  92. // Prints help for the App or Command
  93. type helpPrinter func(w io.Writer, templ string, data interface{})
  94. // HelpPrinter is a function that writes the help output. If not set a default
  95. // is used. The function signature is:
  96. // func(w io.Writer, templ string, data interface{})
  97. var HelpPrinter helpPrinter = printHelp
  98. // VersionPrinter prints the version for the App
  99. var VersionPrinter = printVersion
  100. // ShowAppHelp is an action that displays the help.
  101. func ShowAppHelp(c *Context) {
  102. HelpPrinter(c.App.Writer, AppHelpTemplate, c.App)
  103. }
  104. // DefaultAppComplete prints the list of subcommands as the default app completion method
  105. func DefaultAppComplete(c *Context) {
  106. for _, command := range c.App.Commands {
  107. if command.Hidden {
  108. continue
  109. }
  110. for _, name := range command.Names() {
  111. fmt.Fprintln(c.App.Writer, name)
  112. }
  113. }
  114. }
  115. // ShowCommandHelp prints help for the given command
  116. func ShowCommandHelp(ctx *Context, command string) error {
  117. // show the subcommand help for a command with subcommands
  118. if command == "" {
  119. HelpPrinter(ctx.App.Writer, SubcommandHelpTemplate, ctx.App)
  120. return nil
  121. }
  122. for _, c := range ctx.App.Commands {
  123. if c.HasName(command) {
  124. HelpPrinter(ctx.App.Writer, CommandHelpTemplate, c)
  125. return nil
  126. }
  127. }
  128. if ctx.App.CommandNotFound == nil {
  129. return NewExitError(fmt.Sprintf("No help topic for '%v'", command), 3)
  130. }
  131. ctx.App.CommandNotFound(ctx, command)
  132. return nil
  133. }
  134. // ShowSubcommandHelp prints help for the given subcommand
  135. func ShowSubcommandHelp(c *Context) error {
  136. return ShowCommandHelp(c, c.Command.Name)
  137. }
  138. // ShowVersion prints the version number of the App
  139. func ShowVersion(c *Context) {
  140. VersionPrinter(c)
  141. }
  142. func printVersion(c *Context) {
  143. fmt.Fprintf(c.App.Writer, "%v version %v\n", c.App.Name, c.App.Version)
  144. }
  145. // ShowCompletions prints the lists of commands within a given context
  146. func ShowCompletions(c *Context) {
  147. a := c.App
  148. if a != nil && a.BashComplete != nil {
  149. a.BashComplete(c)
  150. }
  151. }
  152. // ShowCommandCompletions prints the custom completions for a given command
  153. func ShowCommandCompletions(ctx *Context, command string) {
  154. c := ctx.App.Command(command)
  155. if c != nil && c.BashComplete != nil {
  156. c.BashComplete(ctx)
  157. }
  158. }
  159. func printHelp(out io.Writer, templ string, data interface{}) {
  160. funcMap := template.FuncMap{
  161. "join": strings.Join,
  162. }
  163. w := tabwriter.NewWriter(out, 0, 8, 1, '\t', 0)
  164. t := template.Must(template.New("help").Funcs(funcMap).Parse(templ))
  165. err := t.Execute(w, data)
  166. if err != nil {
  167. // If the writer is closed, t.Execute will fail, and there's nothing
  168. // we can do to recover.
  169. if os.Getenv("CLI_TEMPLATE_ERROR_DEBUG") != "" {
  170. fmt.Fprintf(ErrWriter, "CLI TEMPLATE ERROR: %#v\n", err)
  171. }
  172. return
  173. }
  174. w.Flush()
  175. }
  176. func checkVersion(c *Context) bool {
  177. found := false
  178. if VersionFlag.Name != "" {
  179. eachName(VersionFlag.Name, func(name string) {
  180. if c.GlobalBool(name) || c.Bool(name) {
  181. found = true
  182. }
  183. })
  184. }
  185. return found
  186. }
  187. func checkHelp(c *Context) bool {
  188. found := false
  189. if HelpFlag.Name != "" {
  190. eachName(HelpFlag.Name, func(name string) {
  191. if c.GlobalBool(name) || c.Bool(name) {
  192. found = true
  193. }
  194. })
  195. }
  196. return found
  197. }
  198. func checkCommandHelp(c *Context, name string) bool {
  199. if c.Bool("h") || c.Bool("help") {
  200. ShowCommandHelp(c, name)
  201. return true
  202. }
  203. return false
  204. }
  205. func checkSubcommandHelp(c *Context) bool {
  206. if c.GlobalBool("h") || c.GlobalBool("help") {
  207. ShowSubcommandHelp(c)
  208. return true
  209. }
  210. return false
  211. }
  212. func checkCompletions(c *Context) bool {
  213. if (c.GlobalBool(BashCompletionFlag.Name) || c.Bool(BashCompletionFlag.Name)) && c.App.EnableBashCompletion {
  214. ShowCompletions(c)
  215. return true
  216. }
  217. return false
  218. }
  219. func checkCommandCompletions(c *Context, name string) bool {
  220. if c.Bool(BashCompletionFlag.Name) && c.App.EnableBashCompletion {
  221. ShowCommandCompletions(c, name)
  222. return true
  223. }
  224. return false
  225. }