Fix issue with negatable flag being negated twice (#171)
This commit is contained in:
+6
-6
@@ -635,12 +635,7 @@ func (c *Context) Apply() (string, error) {
|
|||||||
panic("unsupported path ?!")
|
panic("unsupported path ?!")
|
||||||
}
|
}
|
||||||
if value != nil {
|
if value != nil {
|
||||||
v := c.getValue(value)
|
value.Apply(c.getValue(value))
|
||||||
if value.Flag != nil && value.Flag.Negated {
|
|
||||||
v.SetBool(!v.Bool())
|
|
||||||
}
|
|
||||||
|
|
||||||
value.Apply(v)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -673,6 +668,11 @@ func (c *Context) parseFlag(flags []*Flag, match string) (err error) {
|
|||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if flag.Negated {
|
||||||
|
value := c.getValue(flag.Value)
|
||||||
|
value.SetBool(!value.Bool())
|
||||||
|
flag.Value.Apply(value)
|
||||||
|
}
|
||||||
c.Path = append(c.Path, &Path{Flag: flag})
|
c.Path = append(c.Path, &Path{Flag: flag})
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
+57
-21
@@ -356,30 +356,66 @@ func TestTraceErrorPartiallySucceeds(t *testing.T) {
|
|||||||
require.Equal(t, "one", ctx.Command())
|
require.Equal(t, "one", ctx.Command())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNegatedBooleanFlag(t *testing.T) {
|
type commandWithNegatableFlag struct {
|
||||||
var cli struct {
|
Flag bool `kong:"default='true',negatable"`
|
||||||
Cmd struct {
|
ran bool
|
||||||
Flag bool `kong:"default='true',negatable"`
|
|
||||||
} `kong:"cmd"`
|
|
||||||
}
|
|
||||||
|
|
||||||
p := mustNew(t, &cli)
|
|
||||||
_, err := p.Parse([]string{"cmd", "--no-flag"})
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, false, cli.Cmd.Flag)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInvertedNegatedBooleanFlag(t *testing.T) {
|
func (c *commandWithNegatableFlag) Run() error {
|
||||||
var cli struct {
|
c.ran = true
|
||||||
Cmd struct {
|
return nil
|
||||||
Flag bool `kong:"default='true',negatable"`
|
}
|
||||||
} `kong:"cmd"`
|
|
||||||
}
|
|
||||||
|
|
||||||
p := mustNew(t, &cli)
|
func TestNegatableFlag(t *testing.T) {
|
||||||
_, err := p.Parse([]string{"cmd", "--no-flag=false"})
|
tests := []struct {
|
||||||
require.NoError(t, err)
|
name string
|
||||||
require.Equal(t, true, cli.Cmd.Flag)
|
args []string
|
||||||
|
expected bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "no flag",
|
||||||
|
args: []string{"cmd"},
|
||||||
|
expected: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "boolean flag",
|
||||||
|
args: []string{"cmd", "--flag"},
|
||||||
|
expected: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "inverted boolean flag",
|
||||||
|
args: []string{"cmd", "--flag=false"},
|
||||||
|
expected: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "negated boolean flag",
|
||||||
|
args: []string{"cmd", "--no-flag"},
|
||||||
|
expected: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "inverted negated boolean flag",
|
||||||
|
args: []string{"cmd", "--no-flag=false"},
|
||||||
|
expected: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
tt := tt
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
var cli struct {
|
||||||
|
Cmd commandWithNegatableFlag `kong:"cmd"`
|
||||||
|
}
|
||||||
|
|
||||||
|
p := mustNew(t, &cli)
|
||||||
|
kctx, err := p.Parse(tt.args)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, tt.expected, cli.Cmd.Flag)
|
||||||
|
|
||||||
|
err = kctx.Run()
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, tt.expected, cli.Cmd.Flag)
|
||||||
|
require.True(t, cli.Cmd.ran)
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExistingNoFlag(t *testing.T) {
|
func TestExistingNoFlag(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user