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:
parent
879acab1d0
commit
99431669d0
47
flag.go
47
flag.go
@ -13,6 +13,7 @@ import (
|
||||
// This flag enables bash-completion for all commands and subcommands
|
||||
var BashCompletionFlag = BoolFlag{
|
||||
Name: "generate-bash-completion",
|
||||
Hide: true,
|
||||
}
|
||||
|
||||
// This flag prints the version for the application
|
||||
@ -37,6 +38,7 @@ type Flag interface {
|
||||
// Apply Flag settings to the given flag set
|
||||
Apply(*flag.FlagSet)
|
||||
GetName() string
|
||||
isNotHidden() bool
|
||||
}
|
||||
|
||||
func flagSet(name string, flags []Flag) *flag.FlagSet {
|
||||
@ -68,6 +70,7 @@ type GenericFlag struct {
|
||||
Value Generic
|
||||
Usage string
|
||||
EnvVar string
|
||||
Hide bool
|
||||
}
|
||||
|
||||
// String returns the string representation of the generic flag to display the
|
||||
@ -112,6 +115,10 @@ func (f GenericFlag) GetName() string {
|
||||
return f.Name
|
||||
}
|
||||
|
||||
func (f GenericFlag) isNotHidden() bool {
|
||||
return !f.Hide
|
||||
}
|
||||
|
||||
// StringSlice is an opaque type for []string to satisfy flag.Value
|
||||
type StringSlice []string
|
||||
|
||||
@ -138,6 +145,7 @@ type StringSliceFlag struct {
|
||||
Value *StringSlice
|
||||
Usage string
|
||||
EnvVar string
|
||||
Hide bool
|
||||
}
|
||||
|
||||
// String returns the usage
|
||||
@ -177,6 +185,10 @@ func (f StringSliceFlag) GetName() string {
|
||||
return f.Name
|
||||
}
|
||||
|
||||
func (f StringSliceFlag) isNotHidden() bool {
|
||||
return !f.Hide
|
||||
}
|
||||
|
||||
// StringSlice is an opaque type for []int to satisfy flag.Value
|
||||
type IntSlice []int
|
||||
|
||||
@ -208,6 +220,7 @@ type IntSliceFlag struct {
|
||||
Value *IntSlice
|
||||
Usage string
|
||||
EnvVar string
|
||||
Hide bool
|
||||
}
|
||||
|
||||
// String returns the usage
|
||||
@ -250,12 +263,17 @@ func (f IntSliceFlag) GetName() string {
|
||||
return f.Name
|
||||
}
|
||||
|
||||
func (f IntSliceFlag) isNotHidden() bool {
|
||||
return !f.Hide
|
||||
}
|
||||
|
||||
// BoolFlag is a switch that defaults to false
|
||||
type BoolFlag struct {
|
||||
Name string
|
||||
Usage string
|
||||
EnvVar string
|
||||
Destination *bool
|
||||
Hide bool
|
||||
}
|
||||
|
||||
// String returns a readable representation of this value (for usage defaults)
|
||||
@ -293,6 +311,10 @@ func (f BoolFlag) GetName() string {
|
||||
return f.Name
|
||||
}
|
||||
|
||||
func (f BoolFlag) isNotHidden() bool {
|
||||
return !f.Hide
|
||||
}
|
||||
|
||||
// BoolTFlag this represents a boolean flag that is true by default, but can
|
||||
// still be set to false by --some-flag=false
|
||||
type BoolTFlag struct {
|
||||
@ -300,6 +322,7 @@ type BoolTFlag struct {
|
||||
Usage string
|
||||
EnvVar string
|
||||
Destination *bool
|
||||
Hide bool
|
||||
}
|
||||
|
||||
// String returns a readable representation of this value (for usage defaults)
|
||||
@ -337,6 +360,10 @@ func (f BoolTFlag) GetName() string {
|
||||
return f.Name
|
||||
}
|
||||
|
||||
func (f BoolTFlag) isNotHidden() bool {
|
||||
return !f.Hide
|
||||
}
|
||||
|
||||
// StringFlag represents a flag that takes as string value
|
||||
type StringFlag struct {
|
||||
Name string
|
||||
@ -344,6 +371,7 @@ type StringFlag struct {
|
||||
Usage string
|
||||
EnvVar string
|
||||
Destination *string
|
||||
Hide bool
|
||||
}
|
||||
|
||||
// String returns the usage
|
||||
@ -385,6 +413,10 @@ func (f StringFlag) GetName() string {
|
||||
return f.Name
|
||||
}
|
||||
|
||||
func (f StringFlag) isNotHidden() bool {
|
||||
return !f.Hide
|
||||
}
|
||||
|
||||
// IntFlag is a flag that takes an integer
|
||||
// Errors if the value provided cannot be parsed
|
||||
type IntFlag struct {
|
||||
@ -393,6 +425,7 @@ type IntFlag struct {
|
||||
Usage string
|
||||
EnvVar string
|
||||
Destination *int
|
||||
Hide bool
|
||||
}
|
||||
|
||||
// String returns the usage
|
||||
@ -429,6 +462,10 @@ func (f IntFlag) GetName() string {
|
||||
return f.Name
|
||||
}
|
||||
|
||||
func (f IntFlag) isNotHidden() bool {
|
||||
return !f.Hide
|
||||
}
|
||||
|
||||
// DurationFlag is a flag that takes a duration specified in Go's duration
|
||||
// format: https://golang.org/pkg/time/#ParseDuration
|
||||
type DurationFlag struct {
|
||||
@ -437,6 +474,7 @@ type DurationFlag struct {
|
||||
Usage string
|
||||
EnvVar string
|
||||
Destination *time.Duration
|
||||
Hide bool
|
||||
}
|
||||
|
||||
// String returns a readable representation of this value (for usage defaults)
|
||||
@ -473,6 +511,10 @@ func (f DurationFlag) GetName() string {
|
||||
return f.Name
|
||||
}
|
||||
|
||||
func (f DurationFlag) isNotHidden() bool {
|
||||
return !f.Hide
|
||||
}
|
||||
|
||||
// Float64Flag is a flag that takes an float value
|
||||
// Errors if the value provided cannot be parsed
|
||||
type Float64Flag struct {
|
||||
@ -481,6 +523,7 @@ type Float64Flag struct {
|
||||
Usage string
|
||||
EnvVar string
|
||||
Destination *float64
|
||||
Hide bool
|
||||
}
|
||||
|
||||
// String returns the usage
|
||||
@ -516,6 +559,10 @@ func (f Float64Flag) GetName() string {
|
||||
return f.Name
|
||||
}
|
||||
|
||||
func (f Float64Flag) isNotHidden() bool {
|
||||
return !f.Hide
|
||||
}
|
||||
|
||||
func prefixFor(name string) (prefix string) {
|
||||
if len(name) == 1 {
|
||||
prefix = "-"
|
||||
|
30
help.go
30
help.go
@ -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
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user