diff --git a/flag.go b/flag.go index a693386524..125c6cecce 100644 --- a/flag.go +++ b/flag.go @@ -119,6 +119,14 @@ type DocGenerationFlag interface { GetValue() string } +// VisibleFlag is an interface that allows to check if a flag is visible +type VisibleFlag interface { + Flag + + // IsVisible returns true if the flag is not hidden, otherwise false + IsVisible() bool +} + func flagSet(name string, flags []Flag) (*flag.FlagSet, error) { set := flag.NewFlagSet(name, flag.ContinueOnError) @@ -176,8 +184,7 @@ func normalizeFlags(flags []Flag, set *flag.FlagSet) error { func visibleFlags(fl []Flag) []Flag { var visible []Flag for _, f := range fl { - field := flagValue(f).FieldByName("Hidden") - if !field.IsValid() || !field.Bool() { + if vf, ok := f.(VisibleFlag); ok && vf.IsVisible() { visible = append(visible, f) } } diff --git a/flag_bool.go b/flag_bool.go index 85270e44ba..8bd582094f 100644 --- a/flag_bool.go +++ b/flag_bool.go @@ -58,6 +58,11 @@ func (f *BoolFlag) GetValue() string { return "" } +// IsVisible returns true if the flag is not hidden, otherwise false +func (f *BoolFlag) IsVisible() bool { + return !f.Hidden +} + // Apply populates the flag given the flag set and environment func (f *BoolFlag) Apply(set *flag.FlagSet) error { if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok { diff --git a/flag_duration.go b/flag_duration.go index 7b59a38651..28f3978051 100644 --- a/flag_duration.go +++ b/flag_duration.go @@ -58,6 +58,11 @@ func (f *DurationFlag) GetValue() string { return f.Value.String() } +// IsVisible returns true if the flag is not hidden, otherwise false +func (f *DurationFlag) IsVisible() bool { + return !f.Hidden +} + // Apply populates the flag given the flag set and environment func (f *DurationFlag) Apply(set *flag.FlagSet) error { if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok { diff --git a/flag_float64.go b/flag_float64.go index d2a6458657..10fb8dfc1c 100644 --- a/flag_float64.go +++ b/flag_float64.go @@ -58,6 +58,11 @@ func (f *Float64Flag) GetValue() string { return fmt.Sprintf("%f", f.Value) } +// IsVisible returns true if the flag is not hidden, otherwise false +func (f *Float64Flag) IsVisible() bool { + return !f.Hidden +} + // Apply populates the flag given the flag set and environment func (f *Float64Flag) Apply(set *flag.FlagSet) error { if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok { diff --git a/flag_float64_slice.go b/flag_float64_slice.go index d4e4029584..f752ad7534 100644 --- a/flag_float64_slice.go +++ b/flag_float64_slice.go @@ -127,6 +127,11 @@ func (f *Float64SliceFlag) GetValue() string { return "" } +// IsVisible returns true if the flag is not hidden, otherwise false +func (f *Float64SliceFlag) IsVisible() bool { + return !f.Hidden +} + // Apply populates the flag given the flag set and environment func (f *Float64SliceFlag) Apply(set *flag.FlagSet) error { if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok { diff --git a/flag_generic.go b/flag_generic.go index d6800a858d..fdf586d127 100644 --- a/flag_generic.go +++ b/flag_generic.go @@ -66,6 +66,11 @@ func (f *GenericFlag) GetValue() string { return "" } +// IsVisible returns true if the flag is not hidden, otherwise false +func (f *GenericFlag) IsVisible() bool { + return !f.Hidden +} + // Apply takes the flagset and calls Set on the generic flag with the value // provided by the user for parsing by the flag func (f GenericFlag) Apply(set *flag.FlagSet) error { diff --git a/flag_int.go b/flag_int.go index e9da3fa47c..1ebe3569c1 100644 --- a/flag_int.go +++ b/flag_int.go @@ -58,6 +58,11 @@ func (f *IntFlag) GetValue() string { return fmt.Sprintf("%d", f.Value) } +// IsVisible returns true if the flag is not hidden, otherwise false +func (f *IntFlag) IsVisible() bool { + return !f.Hidden +} + // Apply populates the flag given the flag set and environment func (f *IntFlag) Apply(set *flag.FlagSet) error { if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok { diff --git a/flag_int64.go b/flag_int64.go index 6c5545883c..ecf0e9ef6f 100644 --- a/flag_int64.go +++ b/flag_int64.go @@ -58,6 +58,11 @@ func (f *Int64Flag) GetValue() string { return fmt.Sprintf("%d", f.Value) } +// IsVisible returns true if the flag is not hidden, otherwise false +func (f *Int64Flag) IsVisible() bool { + return !f.Hidden +} + // Apply populates the flag given the flag set and environment func (f *Int64Flag) Apply(set *flag.FlagSet) error { if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok { diff --git a/flag_int64_slice.go b/flag_int64_slice.go index 6f9fd6b885..b4c8bc1496 100644 --- a/flag_int64_slice.go +++ b/flag_int64_slice.go @@ -128,6 +128,11 @@ func (f *Int64SliceFlag) GetValue() string { return "" } +// IsVisible returns true if the flag is not hidden, otherwise false +func (f *Int64SliceFlag) IsVisible() bool { + return !f.Hidden +} + // Apply populates the flag given the flag set and environment func (f *Int64SliceFlag) Apply(set *flag.FlagSet) error { if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok { diff --git a/flag_int_slice.go b/flag_int_slice.go index ec44b70610..d4889b382c 100644 --- a/flag_int_slice.go +++ b/flag_int_slice.go @@ -139,6 +139,11 @@ func (f *IntSliceFlag) GetValue() string { return "" } +// IsVisible returns true if the flag is not hidden, otherwise false +func (f *IntSliceFlag) IsVisible() bool { + return !f.Hidden +} + // Apply populates the flag given the flag set and environment func (f *IntSliceFlag) Apply(set *flag.FlagSet) error { if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok { diff --git a/flag_path.go b/flag_path.go index 37c6f27d35..b3aa9191d3 100644 --- a/flag_path.go +++ b/flag_path.go @@ -54,6 +54,11 @@ func (f *PathFlag) GetValue() string { return f.Value } +// IsVisible returns true if the flag is not hidden, otherwise false +func (f *PathFlag) IsVisible() bool { + return !f.Hidden +} + // Apply populates the flag given the flag set and environment func (f *PathFlag) Apply(set *flag.FlagSet) error { if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok { diff --git a/flag_string.go b/flag_string.go index a43f7c20cb..e7bd412299 100644 --- a/flag_string.go +++ b/flag_string.go @@ -55,6 +55,11 @@ func (f *StringFlag) GetValue() string { return f.Value } +// IsVisible returns true if the flag is not hidden, otherwise false +func (f *StringFlag) IsVisible() bool { + return !f.Hidden +} + // Apply populates the flag given the flag set and environment func (f *StringFlag) Apply(set *flag.FlagSet) error { if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok { diff --git a/flag_string_slice.go b/flag_string_slice.go index e9e27bbacd..5269643105 100644 --- a/flag_string_slice.go +++ b/flag_string_slice.go @@ -124,6 +124,11 @@ func (f *StringSliceFlag) GetValue() string { return "" } +// IsVisible returns true if the flag is not hidden, otherwise false +func (f *StringSliceFlag) IsVisible() bool { + return !f.Hidden +} + // Apply populates the flag given the flag set and environment func (f *StringSliceFlag) Apply(set *flag.FlagSet) error { diff --git a/flag_timestamp.go b/flag_timestamp.go index 8266e23f5a..7458a79b6a 100644 --- a/flag_timestamp.go +++ b/flag_timestamp.go @@ -114,6 +114,11 @@ func (f *TimestampFlag) GetValue() string { return "" } +// IsVisible returns true if the flag is not hidden, otherwise false +func (f *TimestampFlag) IsVisible() bool { + return !f.Hidden +} + // Apply populates the flag given the flag set and environment func (f *TimestampFlag) Apply(set *flag.FlagSet) error { if f.Layout == "" { diff --git a/flag_uint.go b/flag_uint.go index 6730e696ff..23a70a7f90 100644 --- a/flag_uint.go +++ b/flag_uint.go @@ -52,6 +52,11 @@ func (f *UintFlag) GetUsage() string { return f.Usage } +// IsVisible returns true if the flag is not hidden, otherwise false +func (f *UintFlag) IsVisible() bool { + return !f.Hidden +} + // Apply populates the flag given the flag set and environment func (f *UintFlag) Apply(set *flag.FlagSet) error { if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok { diff --git a/flag_uint64.go b/flag_uint64.go index 4af65fa9e4..a2df024e18 100644 --- a/flag_uint64.go +++ b/flag_uint64.go @@ -52,6 +52,11 @@ func (f *Uint64Flag) GetUsage() string { return f.Usage } +// IsVisible returns true if the flag is not hidden, otherwise false +func (f *Uint64Flag) IsVisible() bool { + return !f.Hidden +} + // Apply populates the flag given the flag set and environment func (f *Uint64Flag) Apply(set *flag.FlagSet) error { if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok {