Skip to content

Commit

Permalink
Fix that default should not override the placeholder.
Browse files Browse the repository at this point in the history
Fixes #243.
  • Loading branch information
mitar authored and alecthomas committed Dec 2, 2021
1 parent d564bd2 commit c3703cd
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
9 changes: 6 additions & 3 deletions model.go
Expand Up @@ -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
}

Expand Down
4 changes: 3 additions & 1 deletion model_test.go
Expand Up @@ -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"`
Expand All @@ -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",
Expand Down
5 changes: 2 additions & 3 deletions tag.go
Expand Up @@ -19,6 +19,7 @@ type Tag struct {
Name string
Help string
Type string
TypeName string
Default string
Format string
PlaceHolder string
Expand Down Expand Up @@ -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") != "" {
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit c3703cd

Please sign in to comment.