refactor flag names - make it more explicitly

avoid the using 'reflect' package while getting flag names
This commit is contained in:
Dmitry Kutakov 2019-12-26 17:24:58 +01:00 committed by Dmitry
parent 25e0c70969
commit 8c28ae347a
No known key found for this signature in database
GPG Key ID: E90B6246CD197433
16 changed files with 16 additions and 30 deletions

16
flag.go
View File

@ -203,12 +203,9 @@ func withEnvHint(envVars []string, str string) string {
return str + envText
}
func flagNames(f Flag) []string {
func flagNames(name string, aliases []string) []string {
var ret []string
name := flagStringField(f, "Name")
aliases := flagStringSliceField(f, "Aliases")
for _, part := range append([]string{name}, aliases...) {
// v1 -> v2 migration warning zone:
// Strip off anything after the first found comma or space, which
@ -231,17 +228,6 @@ func flagStringSliceField(f Flag, name string) []string {
return []string{}
}
func flagStringField(f Flag, name string) string {
fv := flagValue(f)
field := fv.FieldByName(name)
if field.IsValid() {
return field.String()
}
return ""
}
func withFileHint(filePath, str string) string {
fileText := ""
if filePath != "" {

View File

@ -34,7 +34,7 @@ func (f *BoolFlag) String() string {
// Names returns the names of the flag
func (f *BoolFlag) Names() []string {
return flagNames(f)
return flagNames(f.Name, f.Aliases)
}
// IsRequired returns whether or not the flag is required

View File

@ -34,7 +34,7 @@ func (f *DurationFlag) String() string {
// Names returns the names of the flag
func (f *DurationFlag) Names() []string {
return flagNames(f)
return flagNames(f.Name, f.Aliases)
}
// IsRequired returns whether or not the flag is required

View File

@ -34,7 +34,7 @@ func (f *Float64Flag) String() string {
// Names returns the names of the flag
func (f *Float64Flag) Names() []string {
return flagNames(f)
return flagNames(f.Name, f.Aliases)
}
// IsRequired returns whether or not the flag is required

View File

@ -90,7 +90,7 @@ func (f *Float64SliceFlag) String() string {
// Names returns the names of the flag
func (f *Float64SliceFlag) Names() []string {
return flagNames(f)
return flagNames(f.Name, f.Aliases)
}
// IsRequired returns whether or not the flag is required

View File

@ -39,7 +39,7 @@ func (f *GenericFlag) String() string {
// Names returns the names of the flag
func (f *GenericFlag) Names() []string {
return flagNames(f)
return flagNames(f.Name, f.Aliases)
}
// IsRequired returns whether or not the flag is required

View File

@ -34,7 +34,7 @@ func (f *IntFlag) String() string {
// Names returns the names of the flag
func (f *IntFlag) Names() []string {
return flagNames(f)
return flagNames(f.Name, f.Aliases)
}
// IsRequired returns whether or not the flag is required

View File

@ -34,7 +34,7 @@ func (f *Int64Flag) String() string {
// Names returns the names of the flag
func (f *Int64Flag) Names() []string {
return flagNames(f)
return flagNames(f.Name, f.Aliases)
}
// IsRequired returns whether or not the flag is required

View File

@ -91,7 +91,7 @@ func (f *Int64SliceFlag) String() string {
// Names returns the names of the flag
func (f *Int64SliceFlag) Names() []string {
return flagNames(f)
return flagNames(f.Name, f.Aliases)
}
// IsRequired returns whether or not the flag is required

View File

@ -102,7 +102,7 @@ func (f *IntSliceFlag) String() string {
// Names returns the names of the flag
func (f *IntSliceFlag) Names() []string {
return flagNames(f)
return flagNames(f.Name, f.Aliases)
}
// IsRequired returns whether or not the flag is required

View File

@ -30,7 +30,7 @@ func (f *PathFlag) String() string {
// Names returns the names of the flag
func (f *PathFlag) Names() []string {
return flagNames(f)
return flagNames(f.Name, f.Aliases)
}
// IsRequired returns whether or not the flag is required

View File

@ -31,7 +31,7 @@ func (f *StringFlag) String() string {
// Names returns the names of the flag
func (f *StringFlag) Names() []string {
return flagNames(f)
return flagNames(f.Name, f.Aliases)
}
// IsRequired returns whether or not the flag is required

View File

@ -86,7 +86,7 @@ func (f *StringSliceFlag) String() string {
// Names returns the names of the flag
func (f *StringSliceFlag) Names() []string {
return flagNames(f)
return flagNames(f.Name, f.Aliases)
}
// IsRequired returns whether or not the flag is required

View File

@ -86,7 +86,7 @@ func (f *TimestampFlag) String() string {
// Names returns the names of the flag
func (f *TimestampFlag) Names() []string {
return flagNames(f)
return flagNames(f.Name, f.Aliases)
}
// IsRequired returns whether or not the flag is required

View File

@ -34,7 +34,7 @@ func (f *UintFlag) String() string {
// Names returns the names of the flag
func (f *UintFlag) Names() []string {
return flagNames(f)
return flagNames(f.Name, f.Aliases)
}
// IsRequired returns whether or not the flag is required

View File

@ -34,7 +34,7 @@ func (f *Uint64Flag) String() string {
// Names returns the names of the flag
func (f *Uint64Flag) Names() []string {
return flagNames(f)
return flagNames(f.Name, f.Aliases)
}
// IsRequired returns whether or not the flag is required