More consistent implementation of recursive subcommands

This commit is contained in:
Summer Mousa
2014-04-16 11:18:00 -05:00
parent 640826c88f
commit 13f0c8c0f6
3 changed files with 140 additions and 1 deletions

46
help.go
View File

@@ -44,6 +44,24 @@ OPTIONS:
{{end}}
`
// The text template for the subcommand help topic.
// cli.go uses text/template to render templates. You can
// render custom help text by setting this variable.
var SubcommandHelpTemplate = `NAME:
{{.Name}} - {{.Usage}}
USAGE:
{{.Name}} [global options] command [command options] [arguments...]
COMMANDS:
{{range .Commands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}
{{end}}
OPTIONS:
{{range .Flags}}{{.}}
{{end}}
`
var helpCommand = Command{
Name: "help",
ShortName: "h",
@@ -58,6 +76,20 @@ var helpCommand = Command{
},
}
var helpSubcommand = Command{
Name: "help",
ShortName: "h",
Usage: "Shows a list of commands or help for one command",
Action: func(c *Context) {
args := c.Args()
if args.Present() {
ShowCommandHelp(c, args.First())
} else {
ShowSubcommandHelp(c)
}
},
}
// Prints help for the App
var HelpPrinter = printHelp
@@ -87,6 +119,11 @@ func ShowCommandHelp(c *Context, command string) {
fmt.Printf("No help topic for '%v'\n", command)
}
// Prints help for the given subcommand
func ShowSubcommandHelp(c *Context) {
HelpPrinter(SubcommandHelpTemplate, c.App)
}
// Prints the version number of the App
func ShowVersion(c *Context) {
fmt.Printf("%v version %v\n", c.App.Name, c.App.Version)
@@ -145,6 +182,15 @@ func checkCommandHelp(c *Context, name string) bool {
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.App.EnableBashCompletion {
ShowCompletions(c)