help.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. {{.HelpName}} {{if .Flags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}
  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. {{.HelpName}} - {{.Usage}}
  38. USAGE:
  39. {{.HelpName}}{{if .Flags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{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. {{.HelpName}} - {{.Usage}}
  51. USAGE:
  52. {{.HelpName}} command{{if .Flags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}
  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. ArgsUsage: "[command]",
  65. Action: func(c *Context) {
  66. args := c.Args()
  67. if args.Present() {
  68. ShowCommandHelp(c, args.First())
  69. } else {
  70. ShowAppHelp(c)
  71. }
  72. },
  73. }
  74. var helpSubcommand = Command{
  75. Name: "help",
  76. Aliases: []string{"h"},
  77. Usage: "Shows a list of commands or help for one command",
  78. ArgsUsage: "[command]",
  79. Action: func(c *Context) {
  80. args := c.Args()
  81. if args.Present() {
  82. ShowCommandHelp(c, args.First())
  83. } else {
  84. ShowSubcommandHelp(c)
  85. }
  86. },
  87. }
  88. // Prints help for the App or Command
  89. type helpPrinter func(w io.Writer, templ string, data interface{})
  90. var HelpPrinter helpPrinter = printHelp
  91. // Prints version for the App
  92. var VersionPrinter = printVersion
  93. func ShowAppHelp(c *Context) {
  94. HelpPrinter(c.App.Writer, AppHelpTemplate, c.App)
  95. }
  96. // Prints the list of subcommands as the default app completion method
  97. func DefaultAppComplete(c *Context) {
  98. for _, command := range c.App.Commands {
  99. for _, name := range command.Names() {
  100. fmt.Fprintln(c.App.Writer, name)
  101. }
  102. }
  103. }
  104. // Prints help for the given command
  105. func ShowCommandHelp(ctx *Context, command string) {
  106. // show the subcommand help for a command with subcommands
  107. if command == "" {
  108. HelpPrinter(ctx.App.Writer, SubcommandHelpTemplate, ctx.App)
  109. return
  110. }
  111. for _, c := range ctx.App.Commands {
  112. if c.HasName(command) {
  113. HelpPrinter(ctx.App.Writer, CommandHelpTemplate, c)
  114. return
  115. }
  116. }
  117. if ctx.App.CommandNotFound != nil {
  118. ctx.App.CommandNotFound(ctx, command)
  119. } else {
  120. fmt.Fprintf(ctx.App.Writer, "No help topic for '%v'\n", command)
  121. }
  122. }
  123. // Prints help for the given subcommand
  124. func ShowSubcommandHelp(c *Context) {
  125. ShowCommandHelp(c, c.Command.Name)
  126. }
  127. // Prints the version number of the App
  128. func ShowVersion(c *Context) {
  129. VersionPrinter(c)
  130. }
  131. func printVersion(c *Context) {
  132. fmt.Fprintf(c.App.Writer, "%v version %v\n", c.App.Name, c.App.Version)
  133. }
  134. // Prints the lists of commands within a given context
  135. func ShowCompletions(c *Context) {
  136. a := c.App
  137. if a != nil && a.BashComplete != nil {
  138. a.BashComplete(c)
  139. }
  140. }
  141. // Prints the custom completions for a given command
  142. func ShowCommandCompletions(ctx *Context, command string) {
  143. c := ctx.App.Command(command)
  144. if c != nil && c.BashComplete != nil {
  145. c.BashComplete(ctx)
  146. }
  147. }
  148. func printHelp(out io.Writer, templ string, data interface{}) {
  149. funcMap := template.FuncMap{
  150. "join": strings.Join,
  151. }
  152. w := tabwriter.NewWriter(out, 0, 8, 1, '\t', 0)
  153. t := template.Must(template.New("help").Funcs(funcMap).Parse(templ))
  154. err := t.Execute(w, data)
  155. if err != nil {
  156. panic(err)
  157. }
  158. w.Flush()
  159. }
  160. func checkVersion(c *Context) bool {
  161. found := false
  162. if VersionFlag.Name != "" {
  163. eachName(VersionFlag.Name, func(name string) {
  164. if c.GlobalBool(name) || c.Bool(name) {
  165. found = true
  166. }
  167. })
  168. }
  169. return found
  170. }
  171. func checkHelp(c *Context) bool {
  172. found := false
  173. if HelpFlag.Name != "" {
  174. eachName(HelpFlag.Name, func(name string) {
  175. if c.GlobalBool(name) || c.Bool(name) {
  176. found = true
  177. }
  178. })
  179. }
  180. return found
  181. }
  182. func checkCommandHelp(c *Context, name string) bool {
  183. if c.Bool("h") || c.Bool("help") {
  184. ShowCommandHelp(c, name)
  185. return true
  186. }
  187. return false
  188. }
  189. func checkSubcommandHelp(c *Context) bool {
  190. if c.GlobalBool("h") || c.GlobalBool("help") {
  191. ShowSubcommandHelp(c)
  192. return true
  193. }
  194. return false
  195. }
  196. func checkCompletions(c *Context) bool {
  197. if (c.GlobalBool(BashCompletionFlag.Name) || c.Bool(BashCompletionFlag.Name)) && c.App.EnableBashCompletion {
  198. ShowCompletions(c)
  199. return true
  200. }
  201. return false
  202. }
  203. func checkCommandCompletions(c *Context, name string) bool {
  204. if c.Bool(BashCompletionFlag.Name) && c.App.EnableBashCompletion {
  205. ShowCommandCompletions(c, name)
  206. return true
  207. }
  208. return false
  209. }