help.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. {{if .UsageText}}{{.UsageText}}{{else}}{{.HelpName}}{{if .VisibleFlags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{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}} - {{if .Description}}{{.Description}}{{else}}{{.Usage}}{{end}}
  53. USAGE:
  54. {{if .UsageText}}{{.UsageText}}{{else}}{{.HelpName}} command{{if .VisibleFlags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{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. // Prints help for the App or Command with custom template function.
  93. type helpPrinterCustom func(w io.Writer, templ string, data interface{}, customFunc map[string]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. // HelpPrinterCustom is same as HelpPrinter but
  99. // takes a custom function for template function map.
  100. var HelpPrinterCustom helpPrinterCustom = printHelpCustom
  101. // VersionPrinter prints the version for the App
  102. var VersionPrinter = printVersion
  103. // ShowAppHelpAndExit - Prints the list of subcommands for the app and exits with exit code.
  104. func ShowAppHelpAndExit(c *Context, exitCode int) {
  105. ShowAppHelp(c)
  106. os.Exit(exitCode)
  107. }
  108. // ShowAppHelp is an action that displays the help.
  109. func ShowAppHelp(c *Context) (err error) {
  110. if c.App.CustomAppHelpTemplate == "" {
  111. HelpPrinter(c.App.Writer, AppHelpTemplate, c.App)
  112. return
  113. }
  114. customAppData := func() map[string]interface{} {
  115. if c.App.ExtraInfo == nil {
  116. return nil
  117. }
  118. return map[string]interface{}{
  119. "ExtraInfo": c.App.ExtraInfo,
  120. }
  121. }
  122. HelpPrinterCustom(c.App.Writer, c.App.CustomAppHelpTemplate, c.App, customAppData())
  123. return nil
  124. }
  125. // DefaultAppComplete prints the list of subcommands as the default app completion method
  126. func DefaultAppComplete(c *Context) {
  127. for _, command := range c.App.Commands {
  128. if command.Hidden {
  129. continue
  130. }
  131. for _, name := range command.Names() {
  132. fmt.Fprintln(c.App.Writer, name)
  133. }
  134. }
  135. }
  136. // ShowCommandHelpAndExit - exits with code after showing help
  137. func ShowCommandHelpAndExit(c *Context, command string, code int) {
  138. ShowCommandHelp(c, command)
  139. os.Exit(code)
  140. }
  141. // ShowCommandHelp prints help for the given command
  142. func ShowCommandHelp(ctx *Context, command string) error {
  143. // show the subcommand help for a command with subcommands
  144. if command == "" {
  145. HelpPrinter(ctx.App.Writer, SubcommandHelpTemplate, ctx.App)
  146. return nil
  147. }
  148. for _, c := range ctx.App.Commands {
  149. if c.HasName(command) {
  150. if c.CustomHelpTemplate != "" {
  151. HelpPrinterCustom(ctx.App.Writer, c.CustomHelpTemplate, c, nil)
  152. } else {
  153. HelpPrinter(ctx.App.Writer, CommandHelpTemplate, c)
  154. }
  155. return nil
  156. }
  157. }
  158. if ctx.App.CommandNotFound == nil {
  159. return NewExitError(fmt.Sprintf("No help topic for '%v'", command), 3)
  160. }
  161. ctx.App.CommandNotFound(ctx, command)
  162. return nil
  163. }
  164. // ShowSubcommandHelp prints help for the given subcommand
  165. func ShowSubcommandHelp(c *Context) error {
  166. return ShowCommandHelp(c, c.Command.Name)
  167. }
  168. // ShowVersion prints the version number of the App
  169. func ShowVersion(c *Context) {
  170. VersionPrinter(c)
  171. }
  172. func printVersion(c *Context) {
  173. fmt.Fprintf(c.App.Writer, "%v version %v\n", c.App.Name, c.App.Version)
  174. }
  175. // ShowCompletions prints the lists of commands within a given context
  176. func ShowCompletions(c *Context) {
  177. a := c.App
  178. if a != nil && a.BashComplete != nil {
  179. a.BashComplete(c)
  180. }
  181. }
  182. // ShowCommandCompletions prints the custom completions for a given command
  183. func ShowCommandCompletions(ctx *Context, command string) {
  184. c := ctx.App.Command(command)
  185. if c != nil && c.BashComplete != nil {
  186. c.BashComplete(ctx)
  187. }
  188. }
  189. func printHelpCustom(out io.Writer, templ string, data interface{}, customFunc map[string]interface{}) {
  190. funcMap := template.FuncMap{
  191. "join": strings.Join,
  192. }
  193. if customFunc != nil {
  194. for key, value := range customFunc {
  195. funcMap[key] = value
  196. }
  197. }
  198. w := tabwriter.NewWriter(out, 1, 8, 2, ' ', 0)
  199. t := template.Must(template.New("help").Funcs(funcMap).Parse(templ))
  200. err := t.Execute(w, data)
  201. if err != nil {
  202. // If the writer is closed, t.Execute will fail, and there's nothing
  203. // we can do to recover.
  204. if os.Getenv("CLI_TEMPLATE_ERROR_DEBUG") != "" {
  205. fmt.Fprintf(ErrWriter, "CLI TEMPLATE ERROR: %#v\n", err)
  206. }
  207. return
  208. }
  209. w.Flush()
  210. }
  211. func printHelp(out io.Writer, templ string, data interface{}) {
  212. printHelpCustom(out, templ, data, nil)
  213. }
  214. func checkVersion(c *Context) bool {
  215. found := false
  216. if VersionFlag.GetName() != "" {
  217. eachName(VersionFlag.GetName(), func(name string) {
  218. if c.GlobalBool(name) || c.Bool(name) {
  219. found = true
  220. }
  221. })
  222. }
  223. return found
  224. }
  225. func checkHelp(c *Context) bool {
  226. found := false
  227. if HelpFlag.GetName() != "" {
  228. eachName(HelpFlag.GetName(), func(name string) {
  229. if c.GlobalBool(name) || c.Bool(name) {
  230. found = true
  231. }
  232. })
  233. }
  234. return found
  235. }
  236. func checkCommandHelp(c *Context, name string) bool {
  237. if c.Bool("h") || c.Bool("help") {
  238. ShowCommandHelp(c, name)
  239. return true
  240. }
  241. return false
  242. }
  243. func checkSubcommandHelp(c *Context) bool {
  244. if c.Bool("h") || c.Bool("help") {
  245. ShowSubcommandHelp(c)
  246. return true
  247. }
  248. return false
  249. }
  250. func checkShellCompleteFlag(a *App, arguments []string) (bool, []string) {
  251. if !a.EnableBashCompletion {
  252. return false, arguments
  253. }
  254. pos := len(arguments) - 1
  255. lastArg := arguments[pos]
  256. if lastArg != "--"+BashCompletionFlag.GetName() {
  257. return false, arguments
  258. }
  259. return true, arguments[:pos]
  260. }
  261. func checkCompletions(c *Context) bool {
  262. if !c.shellComplete {
  263. return false
  264. }
  265. if args := c.Args(); args.Present() {
  266. name := args.First()
  267. if cmd := c.App.Command(name); cmd != nil {
  268. // let the command handle the completion
  269. return false
  270. }
  271. }
  272. ShowCompletions(c)
  273. return true
  274. }
  275. func checkCommandCompletions(c *Context, name string) bool {
  276. if !c.shellComplete {
  277. return false
  278. }
  279. ShowCommandCompletions(c, name)
  280. return true
  281. }