Skip to content

Commit

Permalink
Aliases can be duplicated between different sub commands
Browse files Browse the repository at this point in the history
Make sure we remove all the aliases from the seenFlags tracker just like we do with other flags/short flags
  • Loading branch information
lespea committed Mar 14, 2024
1 parent 3f756c1 commit 75b7255
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions build.go
Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions kong_test.go
Expand Up @@ -1340,6 +1340,20 @@ func TestDuplicateAliases(t *testing.T) {
assert.EqualError(t, err, "<anonymous struct>.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 ``
Expand Down

0 comments on commit 75b7255

Please sign in to comment.