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