Further simplifying default flag stringer func
This commit is contained in:
parent
6089d723a8
commit
cd92adcb75
20
flag.go
20
flag.go
@ -581,35 +581,29 @@ func stringifyFlag(f Flag) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
placeholder, usage := unquoteUsage(fv.FieldByName("Usage").String())
|
placeholder, usage := unquoteUsage(fv.FieldByName("Usage").String())
|
||||||
needsPlaceholder := false
|
|
||||||
|
|
||||||
|
needsPlaceholder := false
|
||||||
defaultValueString := ""
|
defaultValueString := ""
|
||||||
val := fv.FieldByName("Value")
|
val := fv.FieldByName("Value")
|
||||||
|
|
||||||
valString := ""
|
|
||||||
if val.IsValid() {
|
if val.IsValid() {
|
||||||
needsPlaceholder = true
|
needsPlaceholder = true
|
||||||
|
defaultValueString = fmt.Sprintf(" (default: %v)", val.Interface())
|
||||||
|
|
||||||
if val.Kind() == reflect.String && val.String() != "" {
|
if val.Kind() == reflect.String && val.String() != "" {
|
||||||
valString = fmt.Sprintf("%q", val.String())
|
defaultValueString = fmt.Sprintf(" (default: %q)", val.String())
|
||||||
} else {
|
|
||||||
valString = fmt.Sprintf("%v", val.Interface())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if valString != "" {
|
if defaultValueString == " (default: )" {
|
||||||
defaultValueString = valString
|
defaultValueString = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
if needsPlaceholder && placeholder == "" {
|
if needsPlaceholder && placeholder == "" {
|
||||||
placeholder = defaultPlaceholder
|
placeholder = defaultPlaceholder
|
||||||
}
|
}
|
||||||
|
|
||||||
formattedDefaultValueString := ""
|
usageWithDefault := strings.TrimSpace(fmt.Sprintf("%s%s", usage, defaultValueString))
|
||||||
if defaultValueString != "" {
|
|
||||||
formattedDefaultValueString = fmt.Sprintf(" (default: %v)", defaultValueString)
|
|
||||||
}
|
|
||||||
|
|
||||||
usageWithDefault := strings.TrimSpace(fmt.Sprintf("%s%s", usage, formattedDefaultValueString))
|
|
||||||
|
|
||||||
return withEnvHint(fv.FieldByName("EnvVar").String(),
|
return withEnvHint(fv.FieldByName("EnvVar").String(),
|
||||||
fmt.Sprintf("%s\t%s", prefixedNames(fv.FieldByName("Name").String(), placeholder), usageWithDefault))
|
fmt.Sprintf("%s\t%s", prefixedNames(fv.FieldByName("Name").String(), placeholder), usageWithDefault))
|
||||||
|
Loading…
Reference in New Issue
Block a user