Skip to content

Commit

Permalink
Merge pull request #1258 from dearchap/visible_interface
Browse files Browse the repository at this point in the history
Fix(issue #631). Remove reflect calls for Hidden field
  • Loading branch information
rliebz committed Apr 24, 2021
2 parents 75602a4 + df595c0 commit e79ceb6
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 2 deletions.
11 changes: 9 additions & 2 deletions flag.go
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
}
}
Expand Down
5 changes: 5 additions & 0 deletions flag_bool.go
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions flag_duration.go
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions flag_float64.go
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions flag_float64_slice.go
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions flag_generic.go
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions flag_int.go
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions flag_int64.go
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions flag_int64_slice.go
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions flag_int_slice.go
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions flag_path.go
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions flag_string.go
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions flag_string_slice.go
Expand Up @@ -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 {

Expand Down
5 changes: 5 additions & 0 deletions flag_timestamp.go
Expand Up @@ -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 == "" {
Expand Down
5 changes: 5 additions & 0 deletions flag_uint.go
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions flag_uint64.go
Expand Up @@ -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 {
Expand Down

0 comments on commit e79ceb6

Please sign in to comment.