help.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package cli
  2. import (
  3. "fmt"
  4. "os"
  5. "text/tabwriter"
  6. "text/template"
  7. )
  8. // The text template for the Default help topic.
  9. // cli.go uses text/template to render templates. You can
  10. // render custom help text by setting this variable.
  11. var AppHelpTemplate = `NAME:
  12. {{.Name}} - {{.Usage}}
  13. USAGE:
  14. {{.Name}} {{if .Flags}}[global options] {{end}}command{{if .Flags}} [command options]{{end}} [arguments...]
  15. VERSION:
  16. {{.Version}}{{if or .Author .Email}}
  17. AUTHOR:{{if .Author}}
  18. {{.Author}}{{if .Email}} - <{{.Email}}>{{end}}{{else}}
  19. {{.Email}}{{end}}{{end}}
  20. COMMANDS:
  21. {{range .Commands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}
  22. {{end}}{{if .Flags}}
  23. GLOBAL OPTIONS:
  24. {{range .Flags}}{{.}}
  25. {{end}}{{end}}
  26. `
  27. // The text template for the command help topic.
  28. // cli.go uses text/template to render templates. You can
  29. // render custom help text by setting this variable.
  30. var CommandHelpTemplate = `NAME:
  31. {{.Name}} - {{.Usage}}
  32. USAGE:
  33. command {{.Name}}{{if .Flags}} [command options]{{end}} [arguments...]{{if .Description}}
  34. DESCRIPTION:
  35. {{.Description}}{{end}}{{if .Flags}}
  36. OPTIONS:
  37. {{range .Flags}}{{.}}
  38. {{end}}{{ end }}
  39. `
  40. // The text template for the subcommand help topic.
  41. // cli.go uses text/template to render templates. You can
  42. // render custom help text by setting this variable.
  43. var SubcommandHelpTemplate = `NAME:
  44. {{.Name}} - {{.Usage}}
  45. USAGE:
  46. {{.Name}} command{{if .Flags}} [command options]{{end}} [arguments...]
  47. COMMANDS:
  48. {{range .Commands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}
  49. {{end}}{{if .Flags}}
  50. OPTIONS:
  51. {{range .Flags}}{{.}}
  52. {{end}}{{end}}
  53. `
  54. var helpCommand = Command{
  55. Name: "help",
  56. ShortName: "h",
  57. Usage: "Shows a list of commands or help for one command",
  58. Action: func(c *Context) {
  59. args := c.Args()
  60. if args.Present() {
  61. ShowCommandHelp(c, args.First())
  62. } else {
  63. ShowAppHelp(c)
  64. }
  65. },
  66. }
  67. var helpSubcommand = Command{
  68. Name: "help",
  69. ShortName: "h",
  70. Usage: "Shows a list of commands or help for one command",
  71. Action: func(c *Context) {
  72. args := c.Args()
  73. if args.Present() {
  74. ShowCommandHelp(c, args.First())
  75. } else {
  76. ShowSubcommandHelp(c)
  77. }
  78. },
  79. }
  80. // Prints help for the App
  81. var HelpPrinter = printHelp
  82. func ShowAppHelp(c *Context) {
  83. HelpPrinter(AppHelpTemplate, c.App)
  84. }
  85. // Prints the list of subcommands as the default app completion method
  86. func DefaultAppComplete(c *Context) {
  87. for _, command := range c.App.Commands {
  88. fmt.Println(command.Name)
  89. if command.ShortName != "" {
  90. fmt.Println(command.ShortName)
  91. }
  92. }
  93. }
  94. // Prints help for the given command
  95. func ShowCommandHelp(c *Context, command string) {
  96. for _, c := range c.App.Commands {
  97. if c.HasName(command) {
  98. HelpPrinter(CommandHelpTemplate, c)
  99. return
  100. }
  101. }
  102. if c.App.CommandNotFound != nil {
  103. c.App.CommandNotFound(c, command)
  104. } else {
  105. fmt.Printf("No help topic for '%v'\n", command)
  106. }
  107. }
  108. // Prints help for the given subcommand
  109. func ShowSubcommandHelp(c *Context) {
  110. HelpPrinter(SubcommandHelpTemplate, c.App)
  111. }
  112. // Prints the version number of the App
  113. func ShowVersion(c *Context) {
  114. fmt.Printf("%v version %v\n", c.App.Name, c.App.Version)
  115. }
  116. // Prints the lists of commands within a given context
  117. func ShowCompletions(c *Context) {
  118. a := c.App
  119. if a != nil && a.BashComplete != nil {
  120. a.BashComplete(c)
  121. }
  122. }
  123. // Prints the custom completions for a given command
  124. func ShowCommandCompletions(ctx *Context, command string) {
  125. c := ctx.App.Command(command)
  126. if c != nil && c.BashComplete != nil {
  127. c.BashComplete(ctx)
  128. }
  129. }
  130. func printHelp(templ string, data interface{}) {
  131. w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
  132. t := template.Must(template.New("help").Parse(templ))
  133. err := t.Execute(w, data)
  134. if err != nil {
  135. panic(err)
  136. }
  137. w.Flush()
  138. }
  139. func checkVersion(c *Context) bool {
  140. if c.GlobalBool("version") {
  141. ShowVersion(c)
  142. return true
  143. }
  144. return false
  145. }
  146. func checkHelp(c *Context) bool {
  147. if c.GlobalBool("h") || c.GlobalBool("help") {
  148. ShowAppHelp(c)
  149. return true
  150. }
  151. return false
  152. }
  153. func checkCommandHelp(c *Context, name string) bool {
  154. if c.Bool("h") || c.Bool("help") {
  155. ShowCommandHelp(c, name)
  156. return true
  157. }
  158. return false
  159. }
  160. func checkSubcommandHelp(c *Context) bool {
  161. if c.GlobalBool("h") || c.GlobalBool("help") {
  162. ShowSubcommandHelp(c)
  163. return true
  164. }
  165. return false
  166. }
  167. func checkCompletions(c *Context) bool {
  168. if c.GlobalBool(BashCompletionFlag.Name) && c.App.EnableBashCompletion {
  169. ShowCompletions(c)
  170. return true
  171. }
  172. return false
  173. }
  174. func checkCommandCompletions(c *Context, name string) bool {
  175. if c.Bool(BashCompletionFlag.Name) && c.App.EnableBashCompletion {
  176. ShowCommandCompletions(c, name)
  177. return true
  178. }
  179. return false
  180. }