2016-05-03 23:17:16 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2016-07-22 18:15:41 +00:00
|
|
|
"os"
|
2016-05-03 23:17:16 +00:00
|
|
|
"strings"
|
|
|
|
"text/tabwriter"
|
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
2016-07-22 18:15:41 +00:00
|
|
|
// AppHelpTemplate is the text template for the Default help topic.
|
2016-05-03 23:17:16 +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-07-22 18:15:41 +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-05-03 23:17:16 +00:00
|
|
|
{{if .Version}}{{if not .HideVersion}}
|
|
|
|
VERSION:
|
|
|
|
{{.Version}}
|
|
|
|
{{end}}{{end}}{{if len .Authors}}
|
|
|
|
AUTHOR(S):
|
2016-07-22 18:15:41 +00:00
|
|
|
{{range .Authors}}{{.}}{{end}}
|
|
|
|
{{end}}{{if .VisibleCommands}}
|
|
|
|
COMMANDS:{{range .VisibleCategories}}{{if .Name}}
|
|
|
|
{{.Name}}:{{end}}{{range .VisibleCommands}}
|
|
|
|
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}
|
|
|
|
{{end}}{{end}}{{if .VisibleFlags}}
|
2016-05-03 23:17:16 +00:00
|
|
|
GLOBAL OPTIONS:
|
2016-07-22 18:15:41 +00:00
|
|
|
{{range .VisibleFlags}}{{.}}
|
|
|
|
{{end}}{{end}}{{if .Copyright}}
|
2016-05-03 23:17:16 +00:00
|
|
|
COPYRIGHT:
|
|
|
|
{{.Copyright}}
|
|
|
|
{{end}}
|
|
|
|
`
|
|
|
|
|
2016-07-22 18:15:41 +00:00
|
|
|
// CommandHelpTemplate is the text template for the command help topic.
|
2016-05-03 23:17:16 +00:00
|
|
|
// cli.go uses text/template to render templates. You can
|
|
|
|
// render custom help text by setting this variable.
|
|
|
|
var CommandHelpTemplate = `NAME:
|
|
|
|
{{.HelpName}} - {{.Usage}}
|
|
|
|
|
|
|
|
USAGE:
|
2016-07-22 18:15:41 +00:00
|
|
|
{{.HelpName}}{{if .VisibleFlags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{if .Category}}
|
2016-05-03 23:17:16 +00:00
|
|
|
|
|
|
|
CATEGORY:
|
|
|
|
{{.Category}}{{end}}{{if .Description}}
|
|
|
|
|
|
|
|
DESCRIPTION:
|
2016-07-22 18:15:41 +00:00
|
|
|
{{.Description}}{{end}}{{if .VisibleFlags}}
|
2016-05-03 23:17:16 +00:00
|
|
|
|
|
|
|
OPTIONS:
|
2016-07-22 18:15:41 +00:00
|
|
|
{{range .VisibleFlags}}{{.}}
|
|
|
|
{{end}}{{end}}
|
2016-05-03 23:17:16 +00:00
|
|
|
`
|
|
|
|
|
2016-07-22 18:15:41 +00:00
|
|
|
// SubcommandHelpTemplate is the text template for the subcommand help topic.
|
2016-05-03 23:17:16 +00:00
|
|
|
// cli.go uses text/template to render templates. You can
|
|
|
|
// render custom help text by setting this variable.
|
|
|
|
var SubcommandHelpTemplate = `NAME:
|
|
|
|
{{.HelpName}} - {{.Usage}}
|
|
|
|
|
|
|
|
USAGE:
|
2016-07-22 18:15:41 +00:00
|
|
|
{{.HelpName}} command{{if .VisibleFlags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}
|
2016-05-03 23:17:16 +00:00
|
|
|
|
2016-07-22 18:15:41 +00:00
|
|
|
COMMANDS:{{range .VisibleCategories}}{{if .Name}}
|
|
|
|
{{.Name}}:{{end}}{{range .VisibleCommands}}
|
|
|
|
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}
|
|
|
|
{{end}}{{if .VisibleFlags}}
|
2016-05-03 23:17:16 +00:00
|
|
|
OPTIONS:
|
2016-07-22 18:15:41 +00:00
|
|
|
{{range .VisibleFlags}}{{.}}
|
2016-05-03 23:17:16 +00:00
|
|
|
{{end}}{{end}}
|
|
|
|
`
|
|
|
|
|
|
|
|
var helpCommand = Command{
|
|
|
|
Name: "help",
|
|
|
|
Aliases: []string{"h"},
|
|
|
|
Usage: "Shows a list of commands or help for one command",
|
|
|
|
ArgsUsage: "[command]",
|
2016-07-22 18:15:41 +00:00
|
|
|
Action: func(c *Context) error {
|
2016-05-03 23:17:16 +00:00
|
|
|
args := c.Args()
|
|
|
|
if args.Present() {
|
2016-07-22 18:15:41 +00:00
|
|
|
return ShowCommandHelp(c, args.First())
|
2016-05-03 23:17:16 +00:00
|
|
|
}
|
2016-07-22 18:15:41 +00:00
|
|
|
|
|
|
|
ShowAppHelp(c)
|
|
|
|
return nil
|
2016-05-03 23:17:16 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var helpSubcommand = Command{
|
|
|
|
Name: "help",
|
|
|
|
Aliases: []string{"h"},
|
|
|
|
Usage: "Shows a list of commands or help for one command",
|
|
|
|
ArgsUsage: "[command]",
|
2016-07-22 18:15:41 +00:00
|
|
|
Action: func(c *Context) error {
|
2016-05-03 23:17:16 +00:00
|
|
|
args := c.Args()
|
|
|
|
if args.Present() {
|
2016-07-22 18:15:41 +00:00
|
|
|
return ShowCommandHelp(c, args.First())
|
2016-05-03 23:17:16 +00:00
|
|
|
}
|
2016-07-22 18:15:41 +00:00
|
|
|
|
|
|
|
return ShowSubcommandHelp(c)
|
2016-05-03 23:17:16 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prints help for the App or Command
|
|
|
|
type helpPrinter func(w io.Writer, templ string, data interface{})
|
|
|
|
|
2016-07-22 18:15:41 +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{})
|
2016-05-03 23:17:16 +00:00
|
|
|
var HelpPrinter helpPrinter = printHelp
|
|
|
|
|
2016-07-22 18:15:41 +00:00
|
|
|
// VersionPrinter prints the version for the App
|
2016-05-03 23:17:16 +00:00
|
|
|
var VersionPrinter = printVersion
|
|
|
|
|
2016-07-22 18:15:41 +00:00
|
|
|
// ShowAppHelp is an action that displays the help.
|
|
|
|
func ShowAppHelp(c *Context) error {
|
2016-05-03 23:17:16 +00:00
|
|
|
HelpPrinter(c.App.Writer, AppHelpTemplate, c.App)
|
2016-07-22 18:15:41 +00:00
|
|
|
return nil
|
2016-05-03 23:17:16 +00:00
|
|
|
}
|
|
|
|
|
2016-07-22 18:15:41 +00:00
|
|
|
// DefaultAppComplete prints the list of subcommands as the default app completion method
|
2016-05-03 23:17:16 +00:00
|
|
|
func DefaultAppComplete(c *Context) {
|
|
|
|
for _, command := range c.App.Commands {
|
2016-07-22 18:15:41 +00:00
|
|
|
if command.Hidden {
|
|
|
|
continue
|
|
|
|
}
|
2016-05-03 23:17:16 +00:00
|
|
|
for _, name := range command.Names() {
|
|
|
|
fmt.Fprintln(c.App.Writer, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-22 18:15:41 +00:00
|
|
|
// ShowCommandHelp prints help for the given command
|
|
|
|
func ShowCommandHelp(ctx *Context, command string) error {
|
2016-05-03 23:17:16 +00:00
|
|
|
// show the subcommand help for a command with subcommands
|
|
|
|
if command == "" {
|
|
|
|
HelpPrinter(ctx.App.Writer, SubcommandHelpTemplate, ctx.App)
|
2016-07-22 18:15:41 +00:00
|
|
|
return nil
|
2016-05-03 23:17:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range ctx.App.Commands {
|
|
|
|
if c.HasName(command) {
|
|
|
|
HelpPrinter(ctx.App.Writer, CommandHelpTemplate, c)
|
2016-07-22 18:15:41 +00:00
|
|
|
return nil
|
2016-05-03 23:17:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-22 18:15:41 +00:00
|
|
|
if ctx.App.CommandNotFound == nil {
|
|
|
|
return NewExitError(fmt.Sprintf("No help topic for '%v'", command), 3)
|
2016-05-03 23:17:16 +00:00
|
|
|
}
|
2016-07-22 18:15:41 +00:00
|
|
|
|
|
|
|
ctx.App.CommandNotFound(ctx, command)
|
|
|
|
return nil
|
2016-05-03 23:17:16 +00:00
|
|
|
}
|
|
|
|
|
2016-07-22 18:15:41 +00:00
|
|
|
// ShowSubcommandHelp prints help for the given subcommand
|
|
|
|
func ShowSubcommandHelp(c *Context) error {
|
|
|
|
return ShowCommandHelp(c, c.Command.Name)
|
2016-05-03 23:17:16 +00:00
|
|
|
}
|
|
|
|
|
2016-07-22 18:15:41 +00:00
|
|
|
// ShowVersion prints the version number of the App
|
2016-05-03 23:17:16 +00: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-07-22 18:15:41 +00:00
|
|
|
// ShowCompletions prints the lists of commands within a given context
|
2016-05-03 23:17:16 +00:00
|
|
|
func ShowCompletions(c *Context) {
|
|
|
|
a := c.App
|
|
|
|
if a != nil && a.BashComplete != nil {
|
|
|
|
a.BashComplete(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-22 18:15:41 +00:00
|
|
|
// ShowCommandCompletions prints the custom completions for a given command
|
2016-05-03 23:17:16 +00:00
|
|
|
func ShowCommandCompletions(ctx *Context, command string) {
|
|
|
|
c := ctx.App.Command(command)
|
|
|
|
if c != nil && c.BashComplete != nil {
|
|
|
|
c.BashComplete(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func printHelp(out io.Writer, templ string, data interface{}) {
|
|
|
|
funcMap := template.FuncMap{
|
|
|
|
"join": strings.Join,
|
|
|
|
}
|
|
|
|
|
2016-07-22 18:15:41 +00:00
|
|
|
w := tabwriter.NewWriter(out, 1, 8, 2, ' ', 0)
|
2016-05-03 23:17:16 +00:00
|
|
|
t := template.Must(template.New("help").Funcs(funcMap).Parse(templ))
|
|
|
|
err := t.Execute(w, data)
|
|
|
|
if err != nil {
|
|
|
|
// If the writer is closed, t.Execute will fail, and there's nothing
|
2016-07-22 18:15:41 +00:00
|
|
|
// we can do to recover.
|
|
|
|
if os.Getenv("CLI_TEMPLATE_ERROR_DEBUG") != "" {
|
|
|
|
fmt.Fprintf(ErrWriter, "CLI TEMPLATE ERROR: %#v\n", err)
|
|
|
|
}
|
2016-05-03 23:17:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkVersion(c *Context) bool {
|
|
|
|
found := false
|
|
|
|
if VersionFlag.Name != "" {
|
|
|
|
eachName(VersionFlag.Name, func(name string) {
|
|
|
|
if c.GlobalBool(name) || c.Bool(name) {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkHelp(c *Context) bool {
|
|
|
|
found := false
|
|
|
|
if HelpFlag.Name != "" {
|
|
|
|
eachName(HelpFlag.Name, func(name string) {
|
|
|
|
if c.GlobalBool(name) || c.Bool(name) {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2016-07-22 18:15:41 +00:00
|
|
|
if c.Bool("h") || c.Bool("help") {
|
2016-05-03 23:17:16 +00:00
|
|
|
ShowSubcommandHelp(c)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkCompletions(c *Context) bool {
|
|
|
|
if (c.GlobalBool(BashCompletionFlag.Name) || c.Bool(BashCompletionFlag.Name)) && c.App.EnableBashCompletion {
|
|
|
|
ShowCompletions(c)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkCommandCompletions(c *Context, name string) bool {
|
|
|
|
if c.Bool(BashCompletionFlag.Name) && c.App.EnableBashCompletion {
|
|
|
|
ShowCommandCompletions(c, name)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|