Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(issue #631). Remove reflect calls for Hidden field #1258

Merged
merged 1 commit into from Apr 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions flag.go
Expand Up @@ -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)

Expand All @@ -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)
}
}
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 @@ -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 {
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 @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions flag_int_slice.go
Expand Up @@ -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 {
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 @@ -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 {

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