Don't display placeholder for counters.

Fixes #164.
This commit is contained in:
Alec Thomas
2021-04-18 10:05:26 +10:00
parent 8d859b918f
commit 2e611623a7
2 changed files with 24 additions and 17 deletions
+6 -1
View File
@@ -305,6 +305,11 @@ func (v *Value) IsBool() bool {
return v.Target.Kind() == reflect.Bool return v.Target.Kind() == reflect.Bool
} }
// IsCounter returns true if the value is a counter.
func (v *Value) IsCounter() bool {
return v.Tag.Type == "counter"
}
// Parse tokens into value, parse, and validate, but do not write to the field. // Parse tokens into value, parse, and validate, but do not write to the field.
func (v *Value) Parse(scan *Scanner, target reflect.Value) (err error) { func (v *Value) Parse(scan *Scanner, target reflect.Value) (err error) {
defer func() { defer func() {
@@ -384,7 +389,7 @@ func (f *Flag) String() string {
if f.Short != 0 { if f.Short != 0 {
out = fmt.Sprintf("-%c, %s", f.Short, out) out = fmt.Sprintf("-%c, %s", f.Short, out)
} }
if !f.IsBool() { if !f.IsBool() && !f.IsCounter() {
out += "=" + f.FormatPlaceHolder() out += "=" + f.FormatPlaceHolder()
} }
return out return out
+18 -16
View File
@@ -26,7 +26,7 @@ func TestModelApplicationCommands(t *testing.T) {
require.Equal(t, []string{"one two", "one three <four>"}, actual) require.Equal(t, []string{"one two", "one three <four>"}, actual)
} }
func TestFormatPlaceHolder(t *testing.T) { func TestFlagString(t *testing.T) {
var cli struct { var cli struct {
String string String string
DefaultInt int `default:"42"` DefaultInt int `default:"42"`
@@ -41,28 +41,30 @@ func TestFormatPlaceHolder(t *testing.T) {
MapNoSep map[string]string `mapsep:"none"` MapNoSep map[string]string `mapsep:"none"`
MapDefault map[string]string `mapsep:"none" default:"hello"` MapDefault map[string]string `mapsep:"none" default:"hello"`
MapPlaceholder map[string]string `mapsep:"none" placeholder:"world"` MapPlaceholder map[string]string `mapsep:"none" placeholder:"world"`
Counter int `type:"counter"`
} }
tests := map[string]string{ tests := map[string]string{
"help": "HELP", "help": "-h, --help",
"string": "STRING", "string": "--string=STRING",
"default-int": "42", "default-int": "--default-int=42",
"default-str": `"hello"`, "default-str": `--default-str="hello"`,
"placeholder": "world", "placeholder": "--placeholder=world",
"slice-sep": "SLICE-SEP,...", "slice-sep": "--slice-sep=SLICE-SEP,...",
"slice-no-sep": "SLICE-NO-SEP", "slice-no-sep": "--slice-no-sep=SLICE-NO-SEP",
"slice-default": "hello,...", "slice-default": "--slice-default=hello,...",
"slice-placeholder": "world,...", "slice-placeholder": "--slice-placeholder=world,...",
"slice-default-placeholder": "hello,...", "slice-default-placeholder": "--slice-default-placeholder=hello,...",
"map-sep": "KEY=VALUE;...", "map-sep": "--map-sep=KEY=VALUE;...",
"map-no-sep": "KEY=VALUE", "map-no-sep": "--map-no-sep=KEY=VALUE",
"map-default": "hello", "map-default": "--map-default=hello",
"map-placeholder": "world", "map-placeholder": "--map-placeholder=world",
"counter": "--counter",
} }
p := mustNew(t, &cli) p := mustNew(t, &cli)
for _, flag := range p.Model.Flags { for _, flag := range p.Model.Flags {
want, ok := tests[flag.Name] want, ok := tests[flag.Name]
require.Truef(t, ok, "unknown flag name: %s", flag.Name) require.Truef(t, ok, "unknown flag name: %s", flag.Name)
require.Equal(t, want, flag.FormatPlaceHolder()) require.Equal(t, want, flag.String())
} }
} }