Skip to content

Commit a9b72f5

Browse files
committed
Add support for best command in help
Allows for getting help using the shortcut command as well, ie: ``` # Full command foo help foo:bar # Shortcut command (provided that it does not clash with another command) foo help f:b ```
1 parent 7c74490 commit a9b72f5

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

help.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func ShowAppHelp(c *Context) error {
148148

149149
// ShowCommandHelp prints help for the given command
150150
func ShowCommandHelp(ctx *Context, command string) error {
151-
if c := ctx.App.Command(command); c != nil {
151+
if c := ctx.App.BestCommand(command); c != nil {
152152
if c.DescriptionFunc != nil {
153153
c.Description = c.DescriptionFunc(c, ctx.App)
154154
}

help_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,29 @@ func TestShowCommandHelp_CommandAliases(t *testing.T) {
237237
}
238238
}
239239

240+
func TestShowCommandHelp_CommandShortcut(t *testing.T) {
241+
app := &Application{
242+
Commands: []*Command{
243+
{
244+
Name: "bar",
245+
Category: "foo",
246+
Aliases: []*Alias{{Name: "fb"}},
247+
Action: func(ctx *Context) error {
248+
return nil
249+
},
250+
},
251+
},
252+
}
253+
254+
output := &bytes.Buffer{}
255+
app.Writer = output
256+
app.Run([]string{"foo", "help", "f:b"})
257+
258+
if !strings.Contains(output.String(), "foo:bar") {
259+
t.Errorf("expected output to include command name; got: %q", output.String())
260+
}
261+
}
262+
240263
func TestShowCommandHelp_DescriptionFunc(t *testing.T) {
241264
app := &Application{
242265
Commands: []*Command{

0 commit comments

Comments
 (0)