help.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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) error {
  102. HelpPrinter(c.App.Writer, AppHelpTemplate, c.App)
  103. return nil
  104. }
  105. // DefaultAppComplete prints the list of subcommands as the default app completion method
  106. func DefaultAppComplete(c *Context) {
  107. for _, command := range c.App.Commands {
  108. if command.Hidden {
  109. continue
  110. }
  111. for _, name := range command.Names() {
  112. fmt.Fprintln(c.App.Writer, name)
  113. }
  114. }
  115. }
  116. // ShowCommandHelp prints help for the given command
  117. func ShowCommandHelp(ctx *Context, command string) error {
  118. // show the subcommand help for a command with subcommands
  119. if command == "" {
  120. HelpPrinter(ctx.App.Writer, SubcommandHelpTemplate, ctx.App)
  121. return nil
  122. }
  123. for _, c := range ctx.App.Commands {
  124. if c.HasName(command) {
  125. HelpPrinter(ctx.App.Writer, CommandHelpTemplate, c)
  126. return nil
  127. }
  128. }
  129. if ctx.App.CommandNotFound == nil {
  130. return NewExitError(fmt.Sprintf("No help topic for '%v'", command), 3)
  131. }
  132. ctx.App.CommandNotFound(ctx, command)
  133. return nil
  134. }
  135. // ShowSubcommandHelp prints help for the given subcommand
  136. func ShowSubcommandHelp(c *Context) error {
  137. return ShowCommandHelp(c, c.Command.Name)
  138. }
  139. // ShowVersion prints the version number of the App
  140. func ShowVersion(c *Context) {
  141. VersionPrinter(c)
  142. }
  143. func printVersion(c *Context) {
  144. fmt.Fprintf(c.App.Writer, "%v version %v\n", c.App.Name, c.App.Version)
  145. }
  146. // ShowCompletions prints the lists of commands within a given context
  147. func ShowCompletions(c *Context) {
  148. a := c.App
  149. if a != nil && a.BashComplete != nil {
  150. a.BashComplete(c)
  151. }
  152. }
  153. // ShowCommandCompletions prints the custom completions for a given command
  154. func ShowCommandCompletions(ctx *Context, command string) {
  155. c := ctx.App.Command(command)
  156. if c != nil && c.BashComplete != nil {
  157. c.BashComplete(ctx)
  158. }
  159. }
  160. func printHelp(out io.Writer, templ string, data interface{}) {
  161. funcMap := template.FuncMap{
  162. "join": strings.Join,
  163. }
  164. w := tabwriter.NewWriter(out, 1, 8, 2, ' ', 0)
  165. t := template.Must(template.New("help").Funcs(funcMap).Parse(templ))
  166. err := t.Execute(w, data)
  167. if err != nil {
  168. // If the writer is closed, t.Execute will fail, and there's nothing
  169. // we can do to recover.
  170. if os.Getenv("CLI_TEMPLATE_ERROR_DEBUG") != "" {
  171. fmt.Fprintf(ErrWriter, "CLI TEMPLATE ERROR: %#v\n", err)
  172. }
  173. return
  174. }
  175. w.Flush()
  176. }
  177. func checkVersion(c *Context) bool {
  178. found := false
  179. if VersionFlag.Name != "" {
  180. eachName(VersionFlag.Name, func(name string) {
  181. if c.GlobalBool(name) || c.Bool(name) {
  182. found = true
  183. }
  184. })
  185. }
  186. return found
  187. }
  188. func checkHelp(c *Context) bool {
  189. found := false
  190. if HelpFlag.Name != "" {
  191. eachName(HelpFlag.Name, func(name string) {
  192. if c.GlobalBool(name) || c.Bool(name) {
  193. found = true
  194. }
  195. })
  196. }
  197. return found
  198. }
  199. func checkCommandHelp(c *Context, name string) bool {
  200. if c.Bool("h") || c.Bool("help") {
  201. ShowCommandHelp(c, name)
  202. return true
  203. }
  204. return false
  205. }
  206. func checkSubcommandHelp(c *Context) bool {
  207. if c.GlobalBool("h") || c.GlobalBool("help") {
  208. ShowSubcommandHelp(c)
  209. return true
  210. }
  211. return false
  212. }
  213. func checkCompletions(c *Context) bool {
  214. if (c.GlobalBool(BashCompletionFlag.Name) || c.Bool(BashCompletionFlag.Name)) && c.App.EnableBashCompletion {
  215. ShowCompletions(c)
  216. return true
  217. }
  218. return false
  219. }
  220. func checkCommandCompletions(c *Context, name string) bool {
  221. if c.Bool(BashCompletionFlag.Name) && c.App.EnableBashCompletion {
  222. ShowCommandCompletions(c, name)
  223. return true
  224. }
  225. return false
  226. }