2015-03-06 04:00:41 +02:00
|
|
|
package cli
|
|
|
|
|
2015-10-29 20:53:24 +03:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2016-06-09 12:44:42 +03:00
|
|
|
"os"
|
2015-10-29 20:53:24 +03:00
|
|
|
"strings"
|
|
|
|
"text/tabwriter"
|
|
|
|
"text/template"
|
|
|
|
)
|
2015-03-06 04:00:41 +02:00
|
|
|
|
2016-06-09 12:44:42 +03:00
|
|
|
// AppHelpTemplate is the text template for the Default help topic.
|
2015-03-06 04:00:41 +02:00
|
|
|
// cli.go uses text/template to render templates. You can
|
|
|
|
// render custom help text by setting this variable.
|
|
|
|
var AppHelpTemplate = `NAME:
|
2017-01-10 21:33:17 +03:00
|
|
|
{{.Name}}{{if .Usage}} - {{.Usage}}{{end}}
|
2015-03-06 04:00:41 +02:00
|
|
|
|
|
|
|
USAGE:
|
2017-01-10 21:33:17 +03:00
|
|
|
{{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}}
|
|
|
|
|
2015-03-06 04:00:41 +02:00
|
|
|
VERSION:
|
2017-01-10 21:33:17 +03:00
|
|
|
{{.Version}}{{end}}{{end}}{{if .Description}}
|
|
|
|
|
|
|
|
DESCRIPTION:
|
|
|
|
{{.Description}}{{end}}{{if len .Authors}}
|
|
|
|
|
|
|
|
AUTHOR{{with $length := len .Authors}}{{if ne 1 $length}}S{{end}}{{end}}:
|
|
|
|
{{range $index, $author := .Authors}}{{if $index}}
|
|
|
|
{{end}}{{$author}}{{end}}{{end}}{{if .VisibleCommands}}
|
|
|
|
|
2016-06-09 12:44:42 +03:00
|
|
|
COMMANDS:{{range .VisibleCategories}}{{if .Name}}
|
|
|
|
{{.Name}}:{{end}}{{range .VisibleCommands}}
|
2017-01-10 21:33:17 +03:00
|
|
|
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{end}}{{end}}{{if .VisibleFlags}}
|
|
|
|
|
2015-03-06 04:00:41 +02:00
|
|
|
GLOBAL OPTIONS:
|
2017-01-10 21:33:17 +03:00
|
|
|
{{range $index, $option := .VisibleFlags}}{{if $index}}
|
|
|
|
{{end}}{{$option}}{{end}}{{end}}{{if .Copyright}}
|
|
|
|
|
2015-10-29 20:53:24 +03:00
|
|
|
COPYRIGHT:
|
2017-01-10 21:33:17 +03:00
|
|
|
{{.Copyright}}{{end}}
|
2015-03-06 04:00:41 +02:00
|
|
|
`
|
|
|
|
|
2016-06-09 12:44:42 +03:00
|
|
|
// CommandHelpTemplate is the text template for the command help topic.
|
2015-03-06 04:00:41 +02:00
|
|
|
// cli.go uses text/template to render templates. You can
|
|
|
|
// render custom help text by setting this variable.
|
|
|
|
var CommandHelpTemplate = `NAME:
|
2015-10-29 20:53:24 +03:00
|
|
|
{{.HelpName}} - {{.Usage}}
|
2015-03-06 04:00:41 +02:00
|
|
|
|
|
|
|
USAGE:
|
2017-08-11 14:29:05 +03:00
|
|
|
{{if .UsageText}}{{.UsageText}}{{else}}{{.HelpName}}{{if .VisibleFlags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}{{if .Category}}
|
2016-06-09 12:44:42 +03:00
|
|
|
|
|
|
|
CATEGORY:
|
|
|
|
{{.Category}}{{end}}{{if .Description}}
|
2015-03-06 04:00:41 +02:00
|
|
|
|
|
|
|
DESCRIPTION:
|
2016-06-09 12:44:42 +03:00
|
|
|
{{.Description}}{{end}}{{if .VisibleFlags}}
|
2015-03-06 04:00:41 +02:00
|
|
|
|
|
|
|
OPTIONS:
|
2016-06-09 12:44:42 +03:00
|
|
|
{{range .VisibleFlags}}{{.}}
|
|
|
|
{{end}}{{end}}
|
2015-03-06 04:00:41 +02:00
|
|
|
`
|
|
|
|
|
2016-06-09 12:44:42 +03:00
|
|
|
// SubcommandHelpTemplate is the text template for the subcommand help topic.
|
2015-03-06 04:00:41 +02:00
|
|
|
// cli.go uses text/template to render templates. You can
|
|
|
|
// render custom help text by setting this variable.
|
|
|
|
var SubcommandHelpTemplate = `NAME:
|
2017-08-11 14:29:05 +03:00
|
|
|
{{.HelpName}} - {{if .Description}}{{.Description}}{{else}}{{.Usage}}{{end}}
|
2015-03-06 04:00:41 +02:00
|
|
|
|
|
|
|
USAGE:
|
2017-08-11 14:29:05 +03:00
|
|
|
{{if .UsageText}}{{.UsageText}}{{else}}{{.HelpName}} command{{if .VisibleFlags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}
|
2015-03-06 04:00:41 +02:00
|
|
|
|
2016-06-09 12:44:42 +03:00
|
|
|
COMMANDS:{{range .VisibleCategories}}{{if .Name}}
|
|
|
|
{{.Name}}:{{end}}{{range .VisibleCommands}}
|
2016-10-28 20:05:01 +03:00
|
|
|
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}
|
2016-06-09 12:44:42 +03:00
|
|
|
{{end}}{{if .VisibleFlags}}
|
2015-03-06 04:00:41 +02:00
|
|
|
OPTIONS:
|
2016-06-09 12:44:42 +03:00
|
|
|
{{range .VisibleFlags}}{{.}}
|
2015-03-06 04:00:41 +02:00
|
|
|
{{end}}{{end}}
|
|
|
|
`
|
|
|
|
|
|
|
|
var helpCommand = Command{
|
2015-10-29 20:53:24 +03:00
|
|
|
Name: "help",
|
|
|
|
Aliases: []string{"h"},
|
|
|
|
Usage: "Shows a list of commands or help for one command",
|
|
|
|
ArgsUsage: "[command]",
|
2016-06-09 12:44:42 +03:00
|
|
|
Action: func(c *Context) error {
|
2015-03-06 04:00:41 +02:00
|
|
|
args := c.Args()
|
|
|
|
if args.Present() {
|
2016-06-09 12:44:42 +03:00
|
|
|
return ShowCommandHelp(c, args.First())
|
2015-03-06 04:00:41 +02:00
|
|
|
}
|
2016-06-09 12:44:42 +03:00
|
|
|
|
|
|
|
ShowAppHelp(c)
|
|
|
|
return nil
|
2015-03-06 04:00:41 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var helpSubcommand = Command{
|
2015-10-29 20:53:24 +03:00
|
|
|
Name: "help",
|
|
|
|
Aliases: []string{"h"},
|
|
|
|
Usage: "Shows a list of commands or help for one command",
|
|
|
|
ArgsUsage: "[command]",
|
2016-06-09 12:44:42 +03:00
|
|
|
Action: func(c *Context) error {
|
2015-03-06 04:00:41 +02:00
|
|
|
args := c.Args()
|
|
|
|
if args.Present() {
|
2016-06-09 12:44:42 +03:00
|
|
|
return ShowCommandHelp(c, args.First())
|
2015-03-06 04:00:41 +02:00
|
|
|
}
|
2016-06-09 12:44:42 +03:00
|
|
|
|
|
|
|
return ShowSubcommandHelp(c)
|
2015-03-06 04:00:41 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-10-29 20:53:24 +03:00
|
|
|
// Prints help for the App or Command
|
|
|
|
type helpPrinter func(w io.Writer, templ string, data interface{})
|
2015-03-06 04:00:41 +02:00
|
|
|
|
2017-08-11 14:29:05 +03:00
|
|
|
// Prints help for the App or Command with custom template function.
|
|
|
|
type helpPrinterCustom func(w io.Writer, templ string, data interface{}, customFunc map[string]interface{})
|
|
|
|
|
2016-06-09 12:44:42 +03: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-10-29 20:53:24 +03:00
|
|
|
var HelpPrinter helpPrinter = printHelp
|
2015-03-06 04:00:41 +02:00
|
|
|
|
2017-08-11 14:29:05 +03:00
|
|
|
// HelpPrinterCustom is same as HelpPrinter but
|
|
|
|
// takes a custom function for template function map.
|
|
|
|
var HelpPrinterCustom helpPrinterCustom = printHelpCustom
|
|
|
|
|
2016-06-09 12:44:42 +03:00
|
|
|
// VersionPrinter prints the version for the App
|
2015-03-06 04:00:41 +02:00
|
|
|
var VersionPrinter = printVersion
|
|
|
|
|
2017-08-11 14:29:05 +03:00
|
|
|
// ShowAppHelpAndExit - Prints the list of subcommands for the app and exits with exit code.
|
|
|
|
func ShowAppHelpAndExit(c *Context, exitCode int) {
|
|
|
|
ShowAppHelp(c)
|
|
|
|
os.Exit(exitCode)
|
|
|
|
}
|
|
|
|
|
2016-06-09 12:44:42 +03:00
|
|
|
// ShowAppHelp is an action that displays the help.
|
2017-08-11 14:29:05 +03:00
|
|
|
func ShowAppHelp(c *Context) (err error) {
|
|
|
|
if c.App.CustomAppHelpTemplate == "" {
|
|
|
|
HelpPrinter(c.App.Writer, AppHelpTemplate, c.App)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
customAppData := func() map[string]interface{} {
|
|
|
|
if c.App.ExtraInfo == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return map[string]interface{}{
|
|
|
|
"ExtraInfo": c.App.ExtraInfo,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
HelpPrinterCustom(c.App.Writer, c.App.CustomAppHelpTemplate, c.App, customAppData())
|
2016-10-28 20:05:01 +03:00
|
|
|
return nil
|
2015-03-06 04:00:41 +02:00
|
|
|
}
|
|
|
|
|
2016-06-09 12:44:42 +03:00
|
|
|
// DefaultAppComplete prints the list of subcommands as the default app completion method
|
2015-03-06 04:00:41 +02:00
|
|
|
func DefaultAppComplete(c *Context) {
|
|
|
|
for _, command := range c.App.Commands {
|
2016-06-09 12:44:42 +03:00
|
|
|
if command.Hidden {
|
|
|
|
continue
|
|
|
|
}
|
2015-03-24 22:25:11 +02:00
|
|
|
for _, name := range command.Names() {
|
|
|
|
fmt.Fprintln(c.App.Writer, name)
|
2015-03-06 04:00:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-11 14:29:05 +03:00
|
|
|
// ShowCommandHelpAndExit - exits with code after showing help
|
|
|
|
func ShowCommandHelpAndExit(c *Context, command string, code int) {
|
|
|
|
ShowCommandHelp(c, command)
|
|
|
|
os.Exit(code)
|
|
|
|
}
|
|
|
|
|
2016-06-09 12:44:42 +03:00
|
|
|
// ShowCommandHelp prints help for the given command
|
|
|
|
func ShowCommandHelp(ctx *Context, command string) error {
|
2015-03-10 17:41:33 +02:00
|
|
|
// show the subcommand help for a command with subcommands
|
|
|
|
if command == "" {
|
2015-10-29 20:53:24 +03:00
|
|
|
HelpPrinter(ctx.App.Writer, SubcommandHelpTemplate, ctx.App)
|
2016-06-09 12:44:42 +03:00
|
|
|
return nil
|
2015-03-10 17:41:33 +02:00
|
|
|
}
|
|
|
|
|
2015-10-29 20:53:24 +03:00
|
|
|
for _, c := range ctx.App.Commands {
|
2015-03-06 04:00:41 +02:00
|
|
|
if c.HasName(command) {
|
2017-08-11 14:29:05 +03:00
|
|
|
if c.CustomHelpTemplate != "" {
|
|
|
|
HelpPrinterCustom(ctx.App.Writer, c.CustomHelpTemplate, c, nil)
|
|
|
|
} else {
|
|
|
|
HelpPrinter(ctx.App.Writer, CommandHelpTemplate, c)
|
|
|
|
}
|
2016-06-09 12:44:42 +03:00
|
|
|
return nil
|
2015-03-06 04:00:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-09 12:44:42 +03:00
|
|
|
if ctx.App.CommandNotFound == nil {
|
|
|
|
return NewExitError(fmt.Sprintf("No help topic for '%v'", command), 3)
|
2015-03-06 04:00:41 +02:00
|
|
|
}
|
2016-06-09 12:44:42 +03:00
|
|
|
|
|
|
|
ctx.App.CommandNotFound(ctx, command)
|
|
|
|
return nil
|
2015-03-06 04:00:41 +02:00
|
|
|
}
|
|
|
|
|
2016-06-09 12:44:42 +03:00
|
|
|
// ShowSubcommandHelp prints help for the given subcommand
|
|
|
|
func ShowSubcommandHelp(c *Context) error {
|
|
|
|
return ShowCommandHelp(c, c.Command.Name)
|
2015-03-06 04:00:41 +02:00
|
|
|
}
|
|
|
|
|
2016-06-09 12:44:42 +03:00
|
|
|
// ShowVersion prints the version number of the App
|
2015-03-06 04:00:41 +02:00
|
|
|
func ShowVersion(c *Context) {
|
|
|
|
VersionPrinter(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func printVersion(c *Context) {
|
|
|
|
fmt.Fprintf(c.App.Writer, "%v version %v\n", c.App.Name, c.App.Version)
|
|
|
|
}
|
|
|
|
|
2016-06-09 12:44:42 +03:00
|
|
|
// ShowCompletions prints the lists of commands within a given context
|
2015-03-06 04:00:41 +02:00
|
|
|
func ShowCompletions(c *Context) {
|
|
|
|
a := c.App
|
|
|
|
if a != nil && a.BashComplete != nil {
|
|
|
|
a.BashComplete(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-09 12:44:42 +03:00
|
|
|
// ShowCommandCompletions prints the custom completions for a given command
|
2015-03-06 04:00:41 +02:00
|
|
|
func ShowCommandCompletions(ctx *Context, command string) {
|
|
|
|
c := ctx.App.Command(command)
|
|
|
|
if c != nil && c.BashComplete != nil {
|
|
|
|
c.BashComplete(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-11 14:29:05 +03:00
|
|
|
func printHelpCustom(out io.Writer, templ string, data interface{}, customFunc map[string]interface{}) {
|
2015-10-29 20:53:24 +03:00
|
|
|
funcMap := template.FuncMap{
|
|
|
|
"join": strings.Join,
|
2015-03-06 04:00:41 +02:00
|
|
|
}
|
2017-08-11 14:29:05 +03:00
|
|
|
if customFunc != nil {
|
|
|
|
for key, value := range customFunc {
|
|
|
|
funcMap[key] = value
|
|
|
|
}
|
|
|
|
}
|
2015-03-06 04:00:41 +02:00
|
|
|
|
2016-10-28 20:05:01 +03:00
|
|
|
w := tabwriter.NewWriter(out, 1, 8, 2, ' ', 0)
|
2015-10-29 20:53:24 +03:00
|
|
|
t := template.Must(template.New("help").Funcs(funcMap).Parse(templ))
|
|
|
|
err := t.Execute(w, data)
|
|
|
|
if err != nil {
|
2016-02-11 16:16:52 +02:00
|
|
|
// If the writer is closed, t.Execute will fail, and there's nothing
|
2016-06-09 12:44:42 +03:00
|
|
|
// we can do to recover.
|
|
|
|
if os.Getenv("CLI_TEMPLATE_ERROR_DEBUG") != "" {
|
|
|
|
fmt.Fprintf(ErrWriter, "CLI TEMPLATE ERROR: %#v\n", err)
|
|
|
|
}
|
2016-02-11 16:16:52 +02:00
|
|
|
return
|
2015-10-29 20:53:24 +03:00
|
|
|
}
|
|
|
|
w.Flush()
|
2015-03-06 04:00:41 +02:00
|
|
|
}
|
|
|
|
|
2017-08-11 14:29:05 +03:00
|
|
|
func printHelp(out io.Writer, templ string, data interface{}) {
|
|
|
|
printHelpCustom(out, templ, data, nil)
|
|
|
|
}
|
|
|
|
|
2015-10-29 20:53:24 +03:00
|
|
|
func checkVersion(c *Context) bool {
|
|
|
|
found := false
|
2017-08-11 14:29:05 +03:00
|
|
|
if VersionFlag.GetName() != "" {
|
|
|
|
eachName(VersionFlag.GetName(), func(name string) {
|
2015-10-29 20:53:24 +03:00
|
|
|
if c.GlobalBool(name) || c.Bool(name) {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
})
|
2015-03-06 04:00:41 +02:00
|
|
|
}
|
2015-10-29 20:53:24 +03:00
|
|
|
return found
|
|
|
|
}
|
2015-03-06 04:00:41 +02:00
|
|
|
|
2015-10-29 20:53:24 +03:00
|
|
|
func checkHelp(c *Context) bool {
|
|
|
|
found := false
|
2017-08-11 14:29:05 +03:00
|
|
|
if HelpFlag.GetName() != "" {
|
|
|
|
eachName(HelpFlag.GetName(), func(name string) {
|
2015-10-29 20:53:24 +03:00
|
|
|
if c.GlobalBool(name) || c.Bool(name) {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return found
|
2015-03-06 04:00:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkCommandHelp(c *Context, name string) bool {
|
|
|
|
if c.Bool("h") || c.Bool("help") {
|
|
|
|
ShowCommandHelp(c, name)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkSubcommandHelp(c *Context) bool {
|
2017-01-10 21:33:17 +03:00
|
|
|
if c.Bool("h") || c.Bool("help") {
|
2015-03-06 04:00:41 +02:00
|
|
|
ShowSubcommandHelp(c)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-01-10 21:33:17 +03:00
|
|
|
func checkShellCompleteFlag(a *App, arguments []string) (bool, []string) {
|
|
|
|
if !a.EnableBashCompletion {
|
|
|
|
return false, arguments
|
|
|
|
}
|
|
|
|
|
|
|
|
pos := len(arguments) - 1
|
|
|
|
lastArg := arguments[pos]
|
|
|
|
|
2017-08-11 14:29:05 +03:00
|
|
|
if lastArg != "--"+BashCompletionFlag.GetName() {
|
2017-01-10 21:33:17 +03:00
|
|
|
return false, arguments
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, arguments[:pos]
|
|
|
|
}
|
|
|
|
|
2015-03-06 04:00:41 +02:00
|
|
|
func checkCompletions(c *Context) bool {
|
2017-01-10 21:33:17 +03:00
|
|
|
if !c.shellComplete {
|
|
|
|
return false
|
2015-03-06 04:00:41 +02:00
|
|
|
}
|
|
|
|
|
2017-01-10 21:33:17 +03:00
|
|
|
if args := c.Args(); args.Present() {
|
|
|
|
name := args.First()
|
|
|
|
if cmd := c.App.Command(name); cmd != nil {
|
|
|
|
// let the command handle the completion
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ShowCompletions(c)
|
|
|
|
return true
|
2015-03-06 04:00:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkCommandCompletions(c *Context, name string) bool {
|
2017-01-10 21:33:17 +03:00
|
|
|
if !c.shellComplete {
|
|
|
|
return false
|
2015-03-06 04:00:41 +02:00
|
|
|
}
|
|
|
|
|
2017-01-10 21:33:17 +03:00
|
|
|
ShowCommandCompletions(c, name)
|
|
|
|
return true
|
2015-03-06 04:00:41 +02:00
|
|
|
}
|