From df595c0d85738225ddf21c251c9cddb9642203cb Mon Sep 17 00:00:00 2001 From: Naveen Gogineni Date: Thu, 25 Mar 2021 20:45:30 -0400 Subject: [PATCH] Fix(issue #631). Remove reflect calls for Hidden field --- flag.go | 11 +++++++++-- flag_bool.go | 5 +++++ flag_duration.go | 5 +++++ flag_float64.go | 5 +++++ flag_float64_slice.go | 5 +++++ flag_generic.go | 5 +++++ flag_int.go | 5 +++++ flag_int64.go | 5 +++++ flag_int64_slice.go | 5 +++++ flag_int_slice.go | 5 +++++ flag_path.go | 5 +++++ flag_string.go | 5 +++++ flag_string_slice.go | 5 +++++ flag_timestamp.go | 5 +++++ flag_uint.go | 5 +++++ flag_uint64.go | 5 +++++ 16 files changed, 84 insertions(+), 2 deletions(-) diff --git a/flag.go b/flag.go index aff8d5be63..45a9e9e4b5 100644 --- a/flag.go +++ b/flag.go @@ -118,6 +118,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) @@ -133,8 +141,7 @@ func flagSet(name string, flags []Flag) (*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 bc9ea35d08..fc829b0160 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 22a2e67201..6c2055e606 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 91c778c873..d5c62f9044 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 706ee6cd4b..b94c66255b 100644 --- a/flag_float64_slice.go +++ b/flag_float64_slice.go @@ -117,6 +117,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 b0c8ff44d2..769c017efc 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 ac39d4a9e4..d0adfe661b 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 e09991269b..851cc36871 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 2c9a15af3e..a4afac3b12 100644 --- a/flag_int64_slice.go +++ b/flag_int64_slice.go @@ -118,6 +118,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 a73ca6b81c..b26c42baa1 100644 --- a/flag_int_slice.go +++ b/flag_int_slice.go @@ -129,6 +129,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 8070dc4b0b..2cc6ead104 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 400bb532e7..97c4707faa 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 35497032cb..4cc7ee4d77 100644 --- a/flag_string_slice.go +++ b/flag_string_slice.go @@ -114,6 +114,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 0382a6b9dc..2198d10dad 100644 --- a/flag_timestamp.go +++ b/flag_timestamp.go @@ -113,6 +113,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 2e5e76b0ea..9081e4a8cb 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 8fc3289d82..5505e57896 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 {