New `Hide` variable for all Flags

This is a way to provide hidden flags for app, command and subcommands

For example:

  --generate-bash-completion global flag shouldn't be printed along
  with other flags as it might generally confuse people into thinking
  that 'generate' in-fact would generate a bash completion file for them
  to be used along with their app.

Also in general one would want to hide some flags for their apps.
This commit is contained in:
Harshavardhana
2015-03-15 03:11:52 -07:00
committed by Dan Buch
parent 879acab1d0
commit 99431669d0
2 changed files with 74 additions and 3 deletions

30
help.go
View File

@@ -114,7 +114,15 @@ var HelpPrinter helpPrinter = printHelp
var VersionPrinter = printVersion
func ShowAppHelp(c *Context) {
HelpPrinter(c.App.Writer, AppHelpTemplate, c.App)
// Make a copy of c.App context
app := *c.App
app.Flags = make([]Flag, 0)
for _, flag := range c.App.Flags {
if flag.isNotHidden() {
app.Flags = append(app.Flags, flag)
}
}
HelpPrinter(c.App.Writer, AppHelpTemplate, app)
}
// Prints the list of subcommands as the default app completion method
@@ -130,13 +138,29 @@ func DefaultAppComplete(c *Context) {
func ShowCommandHelp(ctx *Context, command string) {
// show the subcommand help for a command with subcommands
if command == "" {
HelpPrinter(ctx.App.Writer, SubcommandHelpTemplate, ctx.App)
// Make a copy of c.App context
app := *c.App
app.Flags = make([]Flag, 0)
for _, flag := range c.App.Flags {
if flag.isNotHidden() {
app.Flags = append(app.Flags, flag)
}
}
HelpPrinter(ctx.App.Writer, SubcommandHelpTemplate, app)
return
}
for _, c := range ctx.App.Commands {
if c.HasName(command) {
HelpPrinter(ctx.App.Writer, CommandHelpTemplate, c)
// Make a copy of command context
c0 := c
c0.Flags = make([]Flag, 0)
for _, flag := range c.Flags {
if flag.isNotHidden() {
c0.Flags = append(c0.Flags, flag)
}
}
HelpPrinter(ctx.App.Writer, CommandHelpTemplate, c0)
return
}
}