Skip to content

Commit 64fc57c

Browse files
committed
Fix handling of aliases
See gohugoio/hugo#11090
1 parent 90e1e10 commit 64fc57c

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

simplecobra.go

+5
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ func checkArgs(cmd *cobra.Command, args []string) error {
233233
return nil
234234
}
235235

236+
// Also check the aliases.
237+
if cmd.HasAlias(commandName) {
238+
return nil
239+
}
240+
236241
return fmt.Errorf("unknown command %q for %q%s", args[1], cmd.CommandPath(), findSuggestions(cmd, commandName))
237242
}
238243

simplecobra_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,26 @@ func TestSimpleCobra(t *testing.T) {
8383

8484
}
8585

86+
func TestAliases(t *testing.T) {
87+
c := qt.New(t)
88+
89+
rootCmd := &rootCommand{name: "root",
90+
commands: []simplecobra.Commander{
91+
&lvl1Command{name: "foo", aliases: []string{"f"},
92+
commands: []simplecobra.Commander{
93+
&lvl2Command{name: "bar"},
94+
},
95+
},
96+
},
97+
}
98+
99+
x, err := simplecobra.New(rootCmd)
100+
c.Assert(err, qt.IsNil)
101+
args := []string{"f"}
102+
_, err = x.Execute(context.Background(), args)
103+
c.Assert(err, qt.IsNil)
104+
}
105+
86106
func TestInitAncestorsOnly(t *testing.T) {
87107
c := qt.New(t)
88108

@@ -305,6 +325,8 @@ type lvl1Command struct {
305325
name string
306326
isInit bool
307327

328+
aliases []string
329+
308330
localFlagName string
309331
localFlagNameC string
310332

@@ -348,6 +370,7 @@ func (c *lvl1Command) Init(cd *simplecobra.Commandeer) error {
348370
}
349371
cmd := cd.CobraCommand
350372
cmd.DisableSuggestions = c.disableSuggestions
373+
cmd.Aliases = c.aliases
351374
localFlags := cmd.Flags()
352375
localFlags.StringVar(&c.localFlagName, "localFlagName", "", "set localFlagName for lvl1Command")
353376
return nil

0 commit comments

Comments
 (0)