From 958a344920f05d78ce87d50fcf23269a11d15640 Mon Sep 17 00:00:00 2001 From: Adam Lesperance Date: Thu, 14 Mar 2024 14:17:27 -0500 Subject: [PATCH] Aliases can be duplicated between different sub commands (#419) Make sure we remove all the aliases from the seenFlags tracker just like we do with other flags/short flags --- build.go | 3 +++ kong_test.go | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/build.go b/build.go index a52d90f..1cfe1c3 100644 --- a/build.go +++ b/build.go @@ -170,6 +170,9 @@ MAIN: if flag.Short != 0 { delete(seenFlags, "-"+string(flag.Short)) } + for _, aflag := range flag.Aliases { + delete(seenFlags, "--"+aflag) + } } if err := validatePositionalArguments(node); err != nil { diff --git a/kong_test.go b/kong_test.go index bd1ecfa..0e9fa9a 100644 --- a/kong_test.go +++ b/kong_test.go @@ -1340,6 +1340,20 @@ func TestDuplicateAliases(t *testing.T) { assert.EqualError(t, err, ".Flag2: duplicate flag --flag") } +func TestSubCommandAliases(t *testing.T) { + type SubC struct { + Flag1 string `aliases:"flag"` + } + + cli1 := struct { + Sub1 SubC `cmd:"sub1"` + Sub2 SubC `cmd:"sub2"` + }{} + + _, err := kong.New(&cli1) + assert.NoError(t, err, "dupe aliases shouldn't error if they're in separate sub commands") +} + func TestDuplicateAliasLong(t *testing.T) { cli2 := struct { Flag string ``