Fix that default should not override the placeholder.

Fixes #243.
This commit is contained in:
Mitar
2021-12-02 12:35:12 +01:00
committed by Alec Thomas
parent d564bd286f
commit c3703cda7e
3 changed files with 11 additions and 7 deletions
+6 -3
View File
@@ -400,21 +400,24 @@ func (f *Flag) FormatPlaceHolder() string {
if f.Value.IsSlice() && f.Value.Tag.Sep != -1 {
tail += string(f.Value.Tag.Sep) + "..."
}
if f.PlaceHolder != "" {
return f.PlaceHolder + tail
}
if f.Default != "" {
if f.Value.Target.Kind() == reflect.String {
return strconv.Quote(f.Default) + tail
}
return f.Default + tail
}
if f.PlaceHolder != "" {
return f.PlaceHolder + tail
}
if f.Value.IsMap() {
if f.Value.Tag.MapSep != -1 {
tail = string(f.Value.Tag.MapSep) + "..."
}
return "KEY=VALUE" + tail
}
if f.Tag != nil && f.Tag.TypeName != "" {
return strings.ToUpper(dashedString(f.Tag.TypeName)) + tail
}
return strings.ToUpper(f.Name) + tail
}
+3 -1
View File
@@ -32,6 +32,7 @@ func TestFlagString(t *testing.T) {
DefaultInt int `default:"42"`
DefaultStr string `default:"hello"`
Placeholder string `placeholder:"world"`
DefaultPlaceholder string `default:"hello" placeholder:"world"`
SliceSep []string
SliceNoSep []string `sep:"none"`
SliceDefault []string `default:"hello"`
@@ -49,11 +50,12 @@ func TestFlagString(t *testing.T) {
"default-int": "--default-int=42",
"default-str": `--default-str="hello"`,
"placeholder": "--placeholder=world",
"default-placeholder": "--default-placeholder=world",
"slice-sep": "--slice-sep=SLICE-SEP,...",
"slice-no-sep": "--slice-no-sep=SLICE-NO-SEP",
"slice-default": "--slice-default=hello,...",
"slice-placeholder": "--slice-placeholder=world,...",
"slice-default-placeholder": "--slice-default-placeholder=hello,...",
"slice-default-placeholder": "--slice-default-placeholder=world,...",
"map-sep": "--map-sep=KEY=VALUE;...",
"map-no-sep": "--map-no-sep=KEY=VALUE",
"map-default": "--map-default=hello",
+2 -3
View File
@@ -19,6 +19,7 @@ type Tag struct {
Name string
Help string
Type string
TypeName string
Default string
Format string
PlaceHolder string
@@ -183,6 +184,7 @@ func hydrateTag(t *Tag, typeName string, isBool bool) error {
t.Name = t.Get("name")
t.Help = t.Get("help")
t.Type = t.Get("type")
t.TypeName = typeName
t.Env = t.Get("env")
t.Short, err = t.GetRune("short")
if err != nil && t.Get("short") != "" {
@@ -217,9 +219,6 @@ func hydrateTag(t *Tag, typeName string, isBool bool) error {
t.Vars[parts[0]] = parts[1]
}
t.PlaceHolder = t.Get("placeholder")
if t.PlaceHolder == "" {
t.PlaceHolder = strings.ToUpper(dashedString(typeName))
}
t.Enum = t.Get("enum")
if t.Enum != "" && !(t.Required || t.Default != "") {
return fmt.Errorf("enum value is only valid if it is either required or has a valid default value")