Add support for command aliases (#130)

This commit is contained in:
Joe Schmitt
2021-01-10 15:36:13 -05:00
committed by GitHub
parent d78d607800
commit 2479d83cc0
6 changed files with 75 additions and 0 deletions
+46
View File
@@ -149,3 +149,49 @@ func TestTagSetOnFlag(t *testing.T) {
require.NoError(t, err)
require.Contains(t, buf.String(), `A key from somewhere.`)
}
func TestTagAliases(t *testing.T) {
type Command struct {
Arg string `arg help:"Some arg"`
}
var cli struct {
Cmd Command `cmd aliases:"alias1, alias2"`
}
p := mustNew(t, &cli)
_, err := p.Parse([]string{"alias1", "arg"})
require.NoError(t, err)
require.Equal(t, "arg", cli.Cmd.Arg)
_, err = p.Parse([]string{"alias2", "arg"})
require.NoError(t, err)
require.Equal(t, "arg", cli.Cmd.Arg)
}
func TestTagAliasesConflict(t *testing.T) {
type Command struct {
Arg string `arg help:"Some arg"`
}
var cli struct {
Cmd Command `cmd hidden aliases:"other-cmd"`
OtherCmd Command `cmd`
}
p := mustNew(t, &cli)
_, err := p.Parse([]string{"other-cmd", "arg"})
require.NoError(t, err)
require.Equal(t, "arg", cli.OtherCmd.Arg)
}
func TestTagAliasesSub(t *testing.T) {
type SubCommand struct {
Arg string `arg help:"Some arg"`
}
type Command struct {
SubCmd SubCommand `cmd aliases:"other-sub-cmd"`
}
var cli struct {
Cmd Command `cmd hidden`
}
p := mustNew(t, &cli)
_, err := p.Parse([]string{"cmd", "other-sub-cmd", "arg"})
require.NoError(t, err)
require.Equal(t, "arg", cli.Cmd.SubCmd.Arg)
}