You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

338 lines
8.4 KiB

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