Allow commands to be hidden from help and autocomplete

This commit is contained in:
Peter Smit 2015-02-06 10:46:32 +02:00
parent 6086d7927e
commit 5308b4cd0f
2 changed files with 7 additions and 2 deletions

View File

@ -31,6 +31,8 @@ type Command struct {
SkipFlagParsing bool SkipFlagParsing bool
// Boolean to hide built-in help command // Boolean to hide built-in help command
HideHelp bool HideHelp bool
// Boolean to hide this command from help or completion
Hidden bool
} }
// Invokes the command given the context, parses ctx.Args() to generate command-specific flags // Invokes the command given the context, parses ctx.Args() to generate command-specific flags

View File

@ -19,7 +19,7 @@ AUTHOR:{{if .Author}}
{{.Email}}{{end}}{{end}} {{.Email}}{{end}}{{end}}
COMMANDS: COMMANDS:
{{range .Commands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}} {{range .Commands}}{{if not .Hidden}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}{{end}}
{{end}}{{if .Flags}} {{end}}{{if .Flags}}
GLOBAL OPTIONS: GLOBAL OPTIONS:
{{range .Flags}}{{.}} {{range .Flags}}{{.}}
@ -53,7 +53,7 @@ USAGE:
{{.Name}} command{{if .Flags}} [command options]{{end}} [arguments...] {{.Name}} command{{if .Flags}} [command options]{{end}} [arguments...]
COMMANDS: COMMANDS:
{{range .Commands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}} {{range .Commands}}{{if not .Hidden}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}{{end}}
{{end}}{{if .Flags}} {{end}}{{if .Flags}}
OPTIONS: OPTIONS:
{{range .Flags}}{{.}} {{range .Flags}}{{.}}
@ -103,6 +103,9 @@ func ShowAppHelp(c *Context) {
// Prints the list of subcommands as the default app completion method // Prints the list of subcommands as the default app completion method
func DefaultAppComplete(c *Context) { func DefaultAppComplete(c *Context) {
for _, command := range c.App.Commands { for _, command := range c.App.Commands {
if command.Hidden {
continue
}
fmt.Fprintln(c.App.Writer, command.Name) fmt.Fprintln(c.App.Writer, command.Name)
if command.ShortName != "" { if command.ShortName != "" {
fmt.Fprintln(c.App.Writer, command.ShortName) fmt.Fprintln(c.App.Writer, command.ShortName)