help.go 5.3 KB

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