From 26c1c9ad052ae46eb77fda47fea6b2b7cfb6a605 Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Thu, 12 Sep 2024 12:05:09 +1000 Subject: [PATCH] fix: allow duplicate custom negated flags Fixes #456 --- build.go | 3 +++ kong_test.go | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/build.go b/build.go index 7791c62..287beeb 100644 --- a/build.go +++ b/build.go @@ -173,6 +173,9 @@ MAIN: if flag.Short != 0 { delete(seenFlags, "-"+string(flag.Short)) } + if negFlag := negatableFlagName(flag.Name, flag.Tag.Negatable); negFlag != "" { + delete(seenFlags, negFlag) + } for _, aflag := range flag.Aliases { delete(seenFlags, "--"+aflag) } diff --git a/kong_test.go b/kong_test.go index f05fc18..4785dd0 100644 --- a/kong_test.go +++ b/kong_test.go @@ -467,6 +467,19 @@ func TestDuplicateNegatableLong(t *testing.T) { assert.EqualError(t, err, ".Two: duplicate negation flag --one") } +func TestDuplicateNegatableFlagsInSubcommands(t *testing.T) { + cli2 := struct { + Sub struct { + Negated bool `negatable:"nope-"` + } `cmd:""` + Sub2 struct { + Negated bool `negatable:"nope-"` + } `cmd:""` + }{} + _, err := kong.New(&cli2) + assert.NoError(t, err) +} + func TestExistingNoFlag(t *testing.T) { var cli struct { Cmd struct {