Merge pull request #1024 from vkd/refactor-flag-names
Refactor the getting flag names - make it more explicit
This commit is contained in:
commit
2beb7f00d3
16
flag.go
16
flag.go
@ -203,12 +203,9 @@ func withEnvHint(envVars []string, str string) string {
|
|||||||
return str + envText
|
return str + envText
|
||||||
}
|
}
|
||||||
|
|
||||||
func flagNames(f Flag) []string {
|
func flagNames(name string, aliases []string) []string {
|
||||||
var ret []string
|
var ret []string
|
||||||
|
|
||||||
name := flagStringField(f, "Name")
|
|
||||||
aliases := flagStringSliceField(f, "Aliases")
|
|
||||||
|
|
||||||
for _, part := range append([]string{name}, aliases...) {
|
for _, part := range append([]string{name}, aliases...) {
|
||||||
// v1 -> v2 migration warning zone:
|
// v1 -> v2 migration warning zone:
|
||||||
// Strip off anything after the first found comma or space, which
|
// Strip off anything after the first found comma or space, which
|
||||||
@ -231,17 +228,6 @@ func flagStringSliceField(f Flag, name string) []string {
|
|||||||
return []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 {
|
func withFileHint(filePath, str string) string {
|
||||||
fileText := ""
|
fileText := ""
|
||||||
if filePath != "" {
|
if filePath != "" {
|
||||||
|
@ -34,7 +34,7 @@ func (f *BoolFlag) String() string {
|
|||||||
|
|
||||||
// Names returns the names of the flag
|
// Names returns the names of the flag
|
||||||
func (f *BoolFlag) Names() []string {
|
func (f *BoolFlag) Names() []string {
|
||||||
return flagNames(f)
|
return flagNames(f.Name, f.Aliases)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsRequired returns whether or not the flag is required
|
// 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
|
// Names returns the names of the flag
|
||||||
func (f *DurationFlag) Names() []string {
|
func (f *DurationFlag) Names() []string {
|
||||||
return flagNames(f)
|
return flagNames(f.Name, f.Aliases)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsRequired returns whether or not the flag is required
|
// 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
|
// Names returns the names of the flag
|
||||||
func (f *Float64Flag) Names() []string {
|
func (f *Float64Flag) Names() []string {
|
||||||
return flagNames(f)
|
return flagNames(f.Name, f.Aliases)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsRequired returns whether or not the flag is required
|
// 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
|
// Names returns the names of the flag
|
||||||
func (f *Float64SliceFlag) Names() []string {
|
func (f *Float64SliceFlag) Names() []string {
|
||||||
return flagNames(f)
|
return flagNames(f.Name, f.Aliases)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsRequired returns whether or not the flag is required
|
// 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
|
// Names returns the names of the flag
|
||||||
func (f *GenericFlag) Names() []string {
|
func (f *GenericFlag) Names() []string {
|
||||||
return flagNames(f)
|
return flagNames(f.Name, f.Aliases)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsRequired returns whether or not the flag is required
|
// 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
|
// Names returns the names of the flag
|
||||||
func (f *IntFlag) Names() []string {
|
func (f *IntFlag) Names() []string {
|
||||||
return flagNames(f)
|
return flagNames(f.Name, f.Aliases)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsRequired returns whether or not the flag is required
|
// 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
|
// Names returns the names of the flag
|
||||||
func (f *Int64Flag) Names() []string {
|
func (f *Int64Flag) Names() []string {
|
||||||
return flagNames(f)
|
return flagNames(f.Name, f.Aliases)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsRequired returns whether or not the flag is required
|
// 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
|
// Names returns the names of the flag
|
||||||
func (f *Int64SliceFlag) Names() []string {
|
func (f *Int64SliceFlag) Names() []string {
|
||||||
return flagNames(f)
|
return flagNames(f.Name, f.Aliases)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsRequired returns whether or not the flag is required
|
// 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
|
// Names returns the names of the flag
|
||||||
func (f *IntSliceFlag) Names() []string {
|
func (f *IntSliceFlag) Names() []string {
|
||||||
return flagNames(f)
|
return flagNames(f.Name, f.Aliases)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsRequired returns whether or not the flag is required
|
// 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
|
// Names returns the names of the flag
|
||||||
func (f *PathFlag) Names() []string {
|
func (f *PathFlag) Names() []string {
|
||||||
return flagNames(f)
|
return flagNames(f.Name, f.Aliases)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsRequired returns whether or not the flag is required
|
// 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
|
// Names returns the names of the flag
|
||||||
func (f *StringFlag) Names() []string {
|
func (f *StringFlag) Names() []string {
|
||||||
return flagNames(f)
|
return flagNames(f.Name, f.Aliases)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsRequired returns whether or not the flag is required
|
// 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
|
// Names returns the names of the flag
|
||||||
func (f *StringSliceFlag) Names() []string {
|
func (f *StringSliceFlag) Names() []string {
|
||||||
return flagNames(f)
|
return flagNames(f.Name, f.Aliases)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsRequired returns whether or not the flag is required
|
// 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
|
// Names returns the names of the flag
|
||||||
func (f *TimestampFlag) Names() []string {
|
func (f *TimestampFlag) Names() []string {
|
||||||
return flagNames(f)
|
return flagNames(f.Name, f.Aliases)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsRequired returns whether or not the flag is required
|
// 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
|
// Names returns the names of the flag
|
||||||
func (f *UintFlag) Names() []string {
|
func (f *UintFlag) Names() []string {
|
||||||
return flagNames(f)
|
return flagNames(f.Name, f.Aliases)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsRequired returns whether or not the flag is required
|
// 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
|
// Names returns the names of the flag
|
||||||
func (f *Uint64Flag) Names() []string {
|
func (f *Uint64Flag) Names() []string {
|
||||||
return flagNames(f)
|
return flagNames(f.Name, f.Aliases)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsRequired returns whether or not the flag is required
|
// IsRequired returns whether or not the flag is required
|
||||||
|
Loading…
Reference in New Issue
Block a user