Support aliases for flags (#409)

Aliases are currently only supported for sub-commands, but they're
useful for flags as well. E.g., when migrating from an old flag name
to a new flag name, while still supporting the old value.
This commit is contained in:
Prashant Varanasi
2024-02-26 16:10:15 -08:00
committed by GitHub
parent 30e84613fe
commit fa9b636997
5 changed files with 50 additions and 4 deletions
+28
View File
@@ -518,6 +518,16 @@ func TestShort(t *testing.T) {
assert.Equal(t, "hello", cli.String)
}
func TestAlias(t *testing.T) {
var cli struct {
String string `aliases:"str"`
}
app := mustNew(t, &cli)
_, err := app.Parse([]string{"--str", "hello"})
assert.NoError(t, err)
assert.Equal(t, "hello", cli.String)
}
func TestDuplicateFlagChoosesLast(t *testing.T) {
var cli struct {
Flag int
@@ -1321,6 +1331,24 @@ func TestDuplicateShortflags(t *testing.T) {
assert.EqualError(t, err, "<anonymous struct>.Flag2: duplicate short flag -t")
}
func TestDuplicateAliases(t *testing.T) {
cli1 := struct {
Flag1 string `aliases:"flag"`
Flag2 string `aliases:"flag"`
}{}
_, err := kong.New(&cli1)
assert.EqualError(t, err, "<anonymous struct>.Flag2: duplicate flag --flag")
}
func TestDuplicateAliasLong(t *testing.T) {
cli2 := struct {
Flag string ``
Flag2 string `aliases:"flag"` // duplicates Flag
}{}
_, err := kong.New(&cli2)
assert.EqualError(t, err, "<anonymous struct>.Flag2: duplicate flag --flag")
}
func TestDuplicateNestedShortFlags(t *testing.T) {
cli := struct {
Flag1 bool `short:"t"`