help.go 7.2 KB

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