diff --git a/model.go b/model.go index cb0534a..f21dece 100644 --- a/model.go +++ b/model.go @@ -305,6 +305,11 @@ func (v *Value) IsBool() 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. func (v *Value) Parse(scan *Scanner, target reflect.Value) (err error) { defer func() { @@ -384,7 +389,7 @@ func (f *Flag) String() string { if f.Short != 0 { out = fmt.Sprintf("-%c, %s", f.Short, out) } - if !f.IsBool() { + if !f.IsBool() && !f.IsCounter() { out += "=" + f.FormatPlaceHolder() } return out diff --git a/model_test.go b/model_test.go index 9910704..0edd1b3 100644 --- a/model_test.go +++ b/model_test.go @@ -26,7 +26,7 @@ func TestModelApplicationCommands(t *testing.T) { require.Equal(t, []string{"one two", "one three "}, actual) } -func TestFormatPlaceHolder(t *testing.T) { +func TestFlagString(t *testing.T) { var cli struct { String string DefaultInt int `default:"42"` @@ -41,28 +41,30 @@ func TestFormatPlaceHolder(t *testing.T) { MapNoSep map[string]string `mapsep:"none"` MapDefault map[string]string `mapsep:"none" default:"hello"` MapPlaceholder map[string]string `mapsep:"none" placeholder:"world"` + Counter int `type:"counter"` } tests := map[string]string{ - "help": "HELP", - "string": "STRING", - "default-int": "42", - "default-str": `"hello"`, - "placeholder": "world", - "slice-sep": "SLICE-SEP,...", - "slice-no-sep": "SLICE-NO-SEP", - "slice-default": "hello,...", - "slice-placeholder": "world,...", - "slice-default-placeholder": "hello,...", - "map-sep": "KEY=VALUE;...", - "map-no-sep": "KEY=VALUE", - "map-default": "hello", - "map-placeholder": "world", + "help": "-h, --help", + "string": "--string=STRING", + "default-int": "--default-int=42", + "default-str": `--default-str="hello"`, + "placeholder": "--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,...", + "map-sep": "--map-sep=KEY=VALUE;...", + "map-no-sep": "--map-no-sep=KEY=VALUE", + "map-default": "--map-default=hello", + "map-placeholder": "--map-placeholder=world", + "counter": "--counter", } p := mustNew(t, &cli) for _, flag := range p.Model.Flags { want, ok := tests[flag.Name] require.Truef(t, ok, "unknown flag name: %s", flag.Name) - require.Equal(t, want, flag.FormatPlaceHolder()) + require.Equal(t, want, flag.String()) } }