Skip to content

Commit 958a344

Browse files
authored
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
1 parent 3f756c1 commit 958a344

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

build.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ MAIN:
170170
if flag.Short != 0 {
171171
delete(seenFlags, "-"+string(flag.Short))
172172
}
173+
for _, aflag := range flag.Aliases {
174+
delete(seenFlags, "--"+aflag)
175+
}
173176
}
174177

175178
if err := validatePositionalArguments(node); err != nil {

kong_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,20 @@ func TestDuplicateAliases(t *testing.T) {
13401340
assert.EqualError(t, err, "<anonymous struct>.Flag2: duplicate flag --flag")
13411341
}
13421342

1343+
func TestSubCommandAliases(t *testing.T) {
1344+
type SubC struct {
1345+
Flag1 string `aliases:"flag"`
1346+
}
1347+
1348+
cli1 := struct {
1349+
Sub1 SubC `cmd:"sub1"`
1350+
Sub2 SubC `cmd:"sub2"`
1351+
}{}
1352+
1353+
_, err := kong.New(&cli1)
1354+
assert.NoError(t, err, "dupe aliases shouldn't error if they're in separate sub commands")
1355+
}
1356+
13431357
func TestDuplicateAliasLong(t *testing.T) {
13441358
cli2 := struct {
13451359
Flag string ``

0 commit comments

Comments
 (0)