Add test case for inverting the negation
Not sure why anyone would do this, but it's covered now.
This commit is contained in:
@@ -440,7 +440,7 @@ Tag | Description
|
|||||||
`required` | If present, flag/arg is required.
|
`required` | If present, flag/arg is required.
|
||||||
`optional` | If present, flag/arg is optional.
|
`optional` | If present, flag/arg is optional.
|
||||||
`hidden` | If present, command or flag is hidden.
|
`hidden` | If present, command or flag is hidden.
|
||||||
`negatable` | If present on a `bool` field, supports prefixing a flag with `--no-` to set flag to `false`
|
`negatable` | If present on a `bool` field, supports prefixing a flag with `--no-` to invert the default value
|
||||||
`format:"X"` | Format for parsing input, if supported.
|
`format:"X"` | Format for parsing input, if supported.
|
||||||
`sep:"X"` | Separator for sequences (defaults to ","). May be `none` to disable splitting.
|
`sep:"X"` | Separator for sequences (defaults to ","). May be `none` to disable splitting.
|
||||||
`mapsep:"X"` | Separator for maps (defaults to ";"). May be `none` to disable splitting.
|
`mapsep:"X"` | Separator for maps (defaults to ";"). May be `none` to disable splitting.
|
||||||
|
|||||||
+6
-4
@@ -579,9 +579,6 @@ func (c *Context) getValue(value *Value) reflect.Value {
|
|||||||
}
|
}
|
||||||
c.values[value] = v
|
c.values[value] = v
|
||||||
}
|
}
|
||||||
if value.Flag != nil && value.Flag.Negated {
|
|
||||||
v.SetBool(false)
|
|
||||||
}
|
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -629,7 +626,12 @@ func (c *Context) Apply() (string, error) {
|
|||||||
panic("unsupported path ?!")
|
panic("unsupported path ?!")
|
||||||
}
|
}
|
||||||
if value != nil {
|
if value != nil {
|
||||||
value.Apply(c.getValue(value))
|
v := c.getValue(value)
|
||||||
|
if value.Flag != nil && value.Flag.Negated {
|
||||||
|
v.SetBool(!v.Bool())
|
||||||
|
}
|
||||||
|
|
||||||
|
value.Apply(v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -369,6 +369,19 @@ func TestNegatedBooleanFlag(t *testing.T) {
|
|||||||
require.Equal(t, false, cli.Cmd.Flag)
|
require.Equal(t, false, cli.Cmd.Flag)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestInvertedNegatedBooleanFlag(t *testing.T) {
|
||||||
|
var cli struct {
|
||||||
|
Cmd struct {
|
||||||
|
Flag bool `kong:"default='true',negatable"`
|
||||||
|
} `kong:"cmd"`
|
||||||
|
}
|
||||||
|
|
||||||
|
p := mustNew(t, &cli)
|
||||||
|
_, err := p.Parse([]string{"cmd", "--no-flag=false"})
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, true, cli.Cmd.Flag)
|
||||||
|
}
|
||||||
|
|
||||||
func TestInvalidNegatedNonBool(t *testing.T) {
|
func TestInvalidNegatedNonBool(t *testing.T) {
|
||||||
var cli struct {
|
var cli struct {
|
||||||
Cmd struct {
|
Cmd struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user