Format help placeholders with slice/map separators

Use the specified separators for a flag in the placeholders printed in
the help message for slices and maps. Do not print the separator and
ellipsis if the separator is disabled (`"none"`).

Previously the default separator was printed with an ellipsis, ignoring
the `sep` and `mapsep` tags.

Add tests for `Flag.FormatPlaceHolder()`.
This commit is contained in:
Cam Hutchison
2020-06-02 08:17:42 +10:00
committed by Alec Thomas
parent b89354adb0
commit f19f1031c0
2 changed files with 47 additions and 3 deletions
+6 -3
View File
@@ -372,8 +372,8 @@ func (f *Flag) String() string {
// FormatPlaceHolder formats the placeholder string for a Flag.
func (f *Flag) FormatPlaceHolder() string {
tail := ""
if f.Value.IsSlice() {
tail += ",..."
if f.Value.IsSlice() && f.Value.Tag.Sep != -1 {
tail += string(f.Value.Tag.Sep) + "..."
}
if f.Default != "" {
if f.Value.Target.Kind() == reflect.String {
@@ -385,7 +385,10 @@ func (f *Flag) FormatPlaceHolder() string {
return f.PlaceHolder + tail
}
if f.Value.IsMap() {
return "KEY=VALUE;..."
if f.Value.Tag.MapSep != -1 {
tail = string(f.Value.Tag.MapSep) + "..."
}
return "KEY=VALUE" + tail
}
return strings.ToUpper(f.Name) + tail
}