2013-07-14 23:30:58 +00:00
|
|
|
package cli
|
|
|
|
|
2015-05-04 00:43:52 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2016-05-03 10:54:05 +00:00
|
|
|
"os"
|
2015-05-04 00:43:52 +00:00
|
|
|
"strings"
|
|
|
|
"text/tabwriter"
|
|
|
|
"text/template"
|
|
|
|
)
|
2013-07-15 00:37:43 +00:00
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// AppHelpTemplate is the text template for the Default help topic.
|
2013-07-20 17:46:47 +00:00
|
|
|
// cli.go uses text/template to render templates. You can
|
|
|
|
// render custom help text by setting this variable.
|
|
|
|
var AppHelpTemplate = `NAME:
|
|
|
|
{{.Name}} - {{.Usage}}
|
|
|
|
|
|
|
|
USAGE:
|
2016-05-01 12:36:17 +00:00
|
|
|
{{if .UsageText}}{{.UsageText}}{{else}}{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}
|
2016-03-11 16:25:30 +00:00
|
|
|
{{if .Version}}{{if not .HideVersion}}
|
2013-07-20 17:46:47 +00:00
|
|
|
VERSION:
|
2015-06-25 08:53:32 +00:00
|
|
|
{{.Version}}
|
2016-03-11 16:25:30 +00:00
|
|
|
{{end}}{{end}}{{if len .Authors}}
|
2015-06-20 23:59:54 +00:00
|
|
|
AUTHOR(S):
|
2016-05-03 10:54:05 +00:00
|
|
|
{{range .Authors}}{{.}}{{end}}
|
|
|
|
{{end}}{{if .VisibleCommands}}
|
|
|
|
COMMANDS:{{range .VisibleCategories}}{{if .Name}}
|
|
|
|
{{.Name}}:{{end}}{{range .VisibleCommands}}
|
2016-05-10 17:41:43 +00:00
|
|
|
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}
|
2016-05-01 12:36:17 +00:00
|
|
|
{{end}}{{end}}{{if .VisibleFlags}}
|
2013-07-20 17:46:47 +00:00
|
|
|
GLOBAL OPTIONS:
|
2016-05-01 12:36:17 +00:00
|
|
|
{{range .VisibleFlags}}{{.}}
|
2016-05-03 10:54:05 +00:00
|
|
|
{{end}}{{end}}{{if .Copyright}}
|
2015-06-09 22:35:50 +00:00
|
|
|
COPYRIGHT:
|
2015-06-25 08:53:32 +00:00
|
|
|
{{.Copyright}}
|
|
|
|
{{end}}
|
2013-07-20 17:46:47 +00:00
|
|
|
`
|
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// CommandHelpTemplate is the text template for the command help topic.
|
2013-07-20 17:46:47 +00:00
|
|
|
// cli.go uses text/template to render templates. You can
|
|
|
|
// render custom help text by setting this variable.
|
|
|
|
var CommandHelpTemplate = `NAME:
|
2015-08-13 04:43:14 +00:00
|
|
|
{{.HelpName}} - {{.Usage}}
|
2013-07-20 17:46:47 +00:00
|
|
|
|
|
|
|
USAGE:
|
2016-05-01 12:36:17 +00:00
|
|
|
{{.HelpName}}{{if .VisibleFlags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{if .Category}}
|
2013-07-20 17:46:47 +00:00
|
|
|
|
2016-03-20 19:17:13 +00:00
|
|
|
CATEGORY:
|
|
|
|
{{.Category}}{{end}}{{if .Description}}
|
2013-07-20 17:46:47 +00:00
|
|
|
|
|
|
|
DESCRIPTION:
|
2016-05-01 12:36:17 +00:00
|
|
|
{{.Description}}{{end}}{{if .VisibleFlags}}
|
2013-07-20 17:46:47 +00:00
|
|
|
|
|
|
|
OPTIONS:
|
2016-05-01 12:36:17 +00:00
|
|
|
{{range .VisibleFlags}}{{.}}
|
2016-05-03 10:54:05 +00:00
|
|
|
{{end}}{{end}}
|
2013-07-20 17:46:47 +00:00
|
|
|
`
|
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// SubcommandHelpTemplate is the text template for the subcommand help topic.
|
2014-04-16 16:18:00 +00:00
|
|
|
// cli.go uses text/template to render templates. You can
|
|
|
|
// render custom help text by setting this variable.
|
|
|
|
var SubcommandHelpTemplate = `NAME:
|
2015-08-13 04:43:14 +00:00
|
|
|
{{.HelpName}} - {{.Usage}}
|
2014-04-16 16:18:00 +00:00
|
|
|
|
|
|
|
USAGE:
|
2016-05-01 12:36:17 +00:00
|
|
|
{{.HelpName}} command{{if .VisibleFlags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}
|
2014-04-16 16:18:00 +00:00
|
|
|
|
2016-05-03 10:54:05 +00:00
|
|
|
COMMANDS:{{range .VisibleCategories}}{{if .Name}}
|
|
|
|
{{.Name}}:{{end}}{{range .VisibleCommands}}
|
2016-05-10 17:41:43 +00:00
|
|
|
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}
|
2016-05-01 12:36:17 +00:00
|
|
|
{{end}}{{if .VisibleFlags}}
|
2014-04-16 16:18:00 +00:00
|
|
|
OPTIONS:
|
2016-05-01 12:36:17 +00:00
|
|
|
{{range .VisibleFlags}}{{.}}
|
2014-07-26 20:54:50 +00:00
|
|
|
{{end}}{{end}}
|
2014-04-16 16:18:00 +00:00
|
|
|
`
|
|
|
|
|
2016-05-22 19:20:52 +00:00
|
|
|
var helpCommand = &Command{
|
2015-08-03 23:51:11 +00:00
|
|
|
Name: "help",
|
|
|
|
Aliases: []string{"h"},
|
|
|
|
Usage: "Shows a list of commands or help for one command",
|
|
|
|
ArgsUsage: "[command]",
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(c *Context) error {
|
2013-07-20 17:20:46 +00:00
|
|
|
args := c.Args()
|
2013-11-24 13:40:21 +00:00
|
|
|
if args.Present() {
|
2016-05-08 00:22:09 +00:00
|
|
|
return ShowCommandHelp(c, args.First())
|
2013-07-20 17:20:46 +00:00
|
|
|
}
|
2016-05-08 00:22:09 +00:00
|
|
|
|
|
|
|
ShowAppHelp(c)
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2013-07-20 17:20:46 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-05-22 19:20:52 +00:00
|
|
|
var helpSubcommand = &Command{
|
2015-08-03 23:51:11 +00:00
|
|
|
Name: "help",
|
|
|
|
Aliases: []string{"h"},
|
|
|
|
Usage: "Shows a list of commands or help for one command",
|
|
|
|
ArgsUsage: "[command]",
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(c *Context) error {
|
2014-04-16 16:18:00 +00:00
|
|
|
args := c.Args()
|
|
|
|
if args.Present() {
|
2016-05-08 00:22:09 +00:00
|
|
|
return ShowCommandHelp(c, args.First())
|
2014-04-16 16:18:00 +00:00
|
|
|
}
|
2016-05-08 00:22:09 +00:00
|
|
|
|
|
|
|
return ShowSubcommandHelp(c)
|
2014-04-16 16:18:00 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-05-04 00:43:52 +00:00
|
|
|
// Prints help for the App or Command
|
|
|
|
type helpPrinter func(w io.Writer, templ string, data interface{})
|
2014-06-12 07:39:13 +00:00
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// HelpPrinter is a function that writes the help output. If not set a default
|
|
|
|
// is used. The function signature is:
|
|
|
|
// func(w io.Writer, templ string, data interface{})
|
2015-05-04 00:43:52 +00:00
|
|
|
var HelpPrinter helpPrinter = printHelp
|
2014-04-12 13:32:53 +00:00
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// VersionPrinter prints the version for the App
|
2014-06-18 04:37:55 +00:00
|
|
|
var VersionPrinter = printVersion
|
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// ShowAppHelp is an action that displays the help.
|
2013-07-24 14:35:45 +00:00
|
|
|
func ShowAppHelp(c *Context) {
|
2016-05-01 12:36:17 +00:00
|
|
|
HelpPrinter(c.App.Writer, AppHelpTemplate, c.App)
|
2013-07-20 17:46:47 +00:00
|
|
|
}
|
2013-07-15 00:37:43 +00:00
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// DefaultAppComplete prints the list of subcommands as the default app completion method
|
2014-04-12 13:32:53 +00:00
|
|
|
func DefaultAppComplete(c *Context) {
|
|
|
|
for _, command := range c.App.Commands {
|
2015-02-06 08:46:32 +00:00
|
|
|
if command.Hidden {
|
|
|
|
continue
|
|
|
|
}
|
2015-03-10 04:24:57 +00:00
|
|
|
for _, name := range command.Names() {
|
|
|
|
fmt.Fprintln(c.App.Writer, name)
|
2014-04-12 13:32:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// ShowCommandHelp prints help for the given command
|
2016-05-08 00:22:09 +00:00
|
|
|
func ShowCommandHelp(ctx *Context, command string) error {
|
2015-02-23 13:01:17 +00:00
|
|
|
// show the subcommand help for a command with subcommands
|
|
|
|
if command == "" {
|
2016-05-01 12:36:17 +00:00
|
|
|
HelpPrinter(ctx.App.Writer, SubcommandHelpTemplate, ctx.App)
|
2016-05-08 00:22:09 +00:00
|
|
|
return nil
|
2015-02-23 13:01:17 +00:00
|
|
|
}
|
|
|
|
|
2015-05-04 00:43:52 +00:00
|
|
|
for _, c := range ctx.App.Commands {
|
2013-07-24 14:35:45 +00:00
|
|
|
if c.HasName(command) {
|
2016-05-01 12:36:17 +00:00
|
|
|
HelpPrinter(ctx.App.Writer, CommandHelpTemplate, c)
|
2016-05-08 00:22:09 +00:00
|
|
|
return nil
|
2013-07-20 17:46:47 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-15 00:37:43 +00:00
|
|
|
|
2016-05-08 00:22:09 +00:00
|
|
|
if ctx.App.CommandNotFound == nil {
|
2016-05-25 16:05:14 +00:00
|
|
|
return Exit(fmt.Sprintf("No help topic for '%v'", command), 3)
|
2014-03-31 03:40:46 +00:00
|
|
|
}
|
2016-05-08 00:22:09 +00:00
|
|
|
|
|
|
|
ctx.App.CommandNotFound(ctx, command)
|
|
|
|
return nil
|
2013-07-20 17:46:47 +00:00
|
|
|
}
|
2013-07-15 00:37:43 +00:00
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// ShowSubcommandHelp prints help for the given subcommand
|
2016-05-08 00:22:09 +00:00
|
|
|
func ShowSubcommandHelp(c *Context) error {
|
2016-05-22 19:20:52 +00:00
|
|
|
if c == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Command != nil {
|
|
|
|
return ShowCommandHelp(c, c.Command.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ShowCommandHelp(c, "")
|
2014-04-16 16:18:00 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// ShowVersion prints the version number of the App
|
2013-07-24 14:35:45 +00:00
|
|
|
func ShowVersion(c *Context) {
|
2014-06-18 04:37:55 +00:00
|
|
|
VersionPrinter(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func printVersion(c *Context) {
|
2014-12-02 04:57:35 +00:00
|
|
|
fmt.Fprintf(c.App.Writer, "%v version %v\n", c.App.Name, c.App.Version)
|
2013-07-24 14:35:45 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// ShowCompletions prints the lists of commands within a given context
|
2014-04-10 17:14:13 +00:00
|
|
|
func ShowCompletions(c *Context) {
|
2014-04-12 13:32:53 +00:00
|
|
|
a := c.App
|
|
|
|
if a != nil && a.BashComplete != nil {
|
|
|
|
a.BashComplete(c)
|
2014-04-10 17:14:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// ShowCommandCompletions prints the custom completions for a given command
|
2014-04-10 17:14:13 +00:00
|
|
|
func ShowCommandCompletions(ctx *Context, command string) {
|
|
|
|
c := ctx.App.Command(command)
|
|
|
|
if c != nil && c.BashComplete != nil {
|
|
|
|
c.BashComplete(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-04 00:43:52 +00:00
|
|
|
func printHelp(out io.Writer, templ string, data interface{}) {
|
|
|
|
funcMap := template.FuncMap{
|
|
|
|
"join": strings.Join,
|
|
|
|
}
|
|
|
|
|
|
|
|
w := tabwriter.NewWriter(out, 0, 8, 1, '\t', 0)
|
|
|
|
t := template.Must(template.New("help").Funcs(funcMap).Parse(templ))
|
2016-05-25 16:05:14 +00:00
|
|
|
|
|
|
|
errDebug := os.Getenv("CLI_TEMPLATE_ERROR_DEBUG") != ""
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
if errDebug {
|
|
|
|
fmt.Fprintf(ErrWriter, "CLI TEMPLATE PANIC: %#v\n", r)
|
|
|
|
}
|
|
|
|
if os.Getenv("CLI_TEMPLATE_REPANIC") != "" {
|
|
|
|
panic(r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-05-04 00:43:52 +00:00
|
|
|
err := t.Execute(w, data)
|
|
|
|
if err != nil {
|
2016-05-25 16:05:14 +00:00
|
|
|
if errDebug {
|
2016-05-08 22:44:38 +00:00
|
|
|
fmt.Fprintf(ErrWriter, "CLI TEMPLATE ERROR: %#v\n", err)
|
2016-05-03 10:54:05 +00:00
|
|
|
}
|
2016-01-20 21:51:55 +00:00
|
|
|
return
|
2015-05-04 00:43:52 +00:00
|
|
|
}
|
2016-05-25 16:05:14 +00:00
|
|
|
|
2015-05-04 00:43:52 +00:00
|
|
|
w.Flush()
|
|
|
|
}
|
|
|
|
|
2013-11-01 13:45:19 +00:00
|
|
|
func checkVersion(c *Context) bool {
|
2015-10-13 22:03:47 +00:00
|
|
|
found := false
|
|
|
|
if VersionFlag.Name != "" {
|
2016-05-22 01:29:45 +00:00
|
|
|
for _, name := range VersionFlag.Names() {
|
2016-05-16 14:18:15 +00:00
|
|
|
if c.Bool(name) {
|
2015-10-13 22:03:47 +00:00
|
|
|
found = true
|
|
|
|
}
|
2016-05-22 01:29:45 +00:00
|
|
|
}
|
2013-07-24 14:35:45 +00:00
|
|
|
}
|
2015-10-13 22:03:47 +00:00
|
|
|
return found
|
2013-07-24 14:35:45 +00:00
|
|
|
}
|
|
|
|
|
2013-11-01 13:45:19 +00:00
|
|
|
func checkHelp(c *Context) bool {
|
2015-10-13 22:03:47 +00:00
|
|
|
found := false
|
|
|
|
if HelpFlag.Name != "" {
|
2016-05-22 01:29:45 +00:00
|
|
|
for _, name := range HelpFlag.Names() {
|
2016-05-16 14:18:15 +00:00
|
|
|
if c.Bool(name) {
|
2015-10-13 22:03:47 +00:00
|
|
|
found = true
|
|
|
|
}
|
2016-05-22 01:29:45 +00:00
|
|
|
}
|
2013-07-24 14:35:45 +00:00
|
|
|
}
|
2015-10-13 22:03:47 +00:00
|
|
|
return found
|
2013-07-20 15:44:09 +00:00
|
|
|
}
|
2013-08-14 04:40:39 +00:00
|
|
|
|
2013-11-01 13:45:19 +00:00
|
|
|
func checkCommandHelp(c *Context, name string) bool {
|
2013-08-14 04:40:39 +00:00
|
|
|
if c.Bool("h") || c.Bool("help") {
|
|
|
|
ShowCommandHelp(c, name)
|
2013-11-01 13:45:19 +00:00
|
|
|
return true
|
2013-08-14 04:40:39 +00:00
|
|
|
}
|
2013-11-01 13:45:19 +00:00
|
|
|
|
|
|
|
return false
|
2013-08-14 04:40:39 +00:00
|
|
|
}
|
2014-04-10 17:14:13 +00:00
|
|
|
|
2014-04-16 16:18:00 +00:00
|
|
|
func checkSubcommandHelp(c *Context) bool {
|
2016-05-16 14:18:15 +00:00
|
|
|
if c.Bool("h") || c.Bool("help") {
|
2014-04-16 16:18:00 +00:00
|
|
|
ShowSubcommandHelp(c)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-04-10 17:14:13 +00:00
|
|
|
func checkCompletions(c *Context) bool {
|
2016-05-16 14:18:15 +00:00
|
|
|
if c.Bool(BashCompletionFlag.Name) && c.App.EnableBashCompletion {
|
2014-04-10 17:14:13 +00:00
|
|
|
ShowCompletions(c)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkCommandCompletions(c *Context, name string) bool {
|
2014-04-12 13:32:53 +00:00
|
|
|
if c.Bool(BashCompletionFlag.Name) && c.App.EnableBashCompletion {
|
2014-04-10 17:14:13 +00:00
|
|
|
ShowCommandCompletions(c, name)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|