Further simplifying default flag stringer func

This commit is contained in:
Dan Buch 2016-05-02 19:58:16 -04:00
parent 6089d723a8
commit cd92adcb75
No known key found for this signature in database
GPG Key ID: FAEF12936DD3E3EC

20
flag.go
View File

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