help.go 8.8 KB

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