From a9b72f5fdd2ec5447dbffecf409f83c9b747c1df Mon Sep 17 00:00:00 2001 From: Antonis Kalipetis Date: Tue, 27 Aug 2024 13:42:36 +0300 Subject: [PATCH] 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 ``` --- help.go | 2 +- help_test.go | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/help.go b/help.go index 9ec14d4..743a768 100644 --- a/help.go +++ b/help.go @@ -148,7 +148,7 @@ func ShowAppHelp(c *Context) error { // ShowCommandHelp prints help for the given command func ShowCommandHelp(ctx *Context, command string) error { - if c := ctx.App.Command(command); c != nil { + if c := ctx.App.BestCommand(command); c != nil { if c.DescriptionFunc != nil { c.Description = c.DescriptionFunc(c, ctx.App) } diff --git a/help_test.go b/help_test.go index 9876b93..8cc866c 100644 --- a/help_test.go +++ b/help_test.go @@ -237,6 +237,29 @@ func TestShowCommandHelp_CommandAliases(t *testing.T) { } } +func TestShowCommandHelp_CommandShortcut(t *testing.T) { + app := &Application{ + Commands: []*Command{ + { + Name: "bar", + Category: "foo", + Aliases: []*Alias{{Name: "fb"}}, + Action: func(ctx *Context) error { + return nil + }, + }, + }, + } + + output := &bytes.Buffer{} + app.Writer = output + app.Run([]string{"foo", "help", "f:b"}) + + if !strings.Contains(output.String(), "foo:bar") { + t.Errorf("expected output to include command name; got: %q", output.String()) + } +} + func TestShowCommandHelp_DescriptionFunc(t *testing.T) { app := &Application{ Commands: []*Command{