refactor flag names - make it more explicitly

avoid the using 'reflect' package while getting flag names
main
Dmitry Kutakov 4 years ago committed by Dmitry
parent 25e0c70969
commit 8c28ae347a
No known key found for this signature in database
GPG Key ID: E90B6246CD197433

@ -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 != "" {

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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

Loading…
Cancel
Save