diff --git a/app.go b/app.go index 5bf20feab9..a5d3608be9 100644 --- a/app.go +++ b/app.go @@ -271,12 +271,12 @@ func (a *App) RunContext(ctx context.Context, arguments []string) (err error) { cCtx.shellComplete = shellComplete a.rootCommand = &Command{ - HelpName: a.HelpName, - Subcommands: a.Commands, - flagCategories: a.flagCategories, - Flags: a.Flags, - Name: a.Name, - //Action: a.Action, // dont set this now + HelpName: a.HelpName, + Subcommands: a.Commands, + flagCategories: a.flagCategories, + Flags: a.Flags, + Name: a.Name, + Action: a.Action, UseShortOptionHandling: a.UseShortOptionHandling, Before: a.Before, After: a.After, diff --git a/command.go b/command.go index 3d5cd4f793..32e90ee0ca 100644 --- a/command.go +++ b/command.go @@ -109,19 +109,14 @@ func (cmd *Command) Command(name string) *Command { } func (c *Command) setup(ctx *Context) { - helpCmd := helpCommand - if len(c.Subcommands) > 0 { - helpCmd = helpSubcommand - } - - if c.Command(helpCmd.Name) == nil && !c.HideHelp { + if c.Command(helpCommand.Name) == nil && !c.HideHelp { if !c.HideHelpCommand { - c.Subcommands = append(c.Subcommands, helpCmd) + c.Subcommands = append(c.Subcommands, helpCommand) } } if c.helpAction == nil { - c.helpAction = helpCmd.Action + c.helpAction = helpCommand.Action } if !c.HideHelp && HelpFlag != nil { @@ -183,7 +178,7 @@ func (c *Command) Run(cCtx *Context, arguments []string) (err error) { if c.isRoot { _ = ShowAppHelp(cCtx) } else { - _ = ShowCommandHelp(cCtx, c.Name) + _ = ShowCommandHelp(cCtx.parentContext, c.Name) } } return err diff --git a/command_test.go b/command_test.go index 1a62f1b2e7..4c319d129c 100644 --- a/command_test.go +++ b/command_test.go @@ -39,6 +39,7 @@ func TestCommandFlagParsing(t *testing.T) { Description: "testing", Action: func(_ *Context) error { return nil }, SkipFlagParsing: c.skipFlagParsing, + isRoot: true, } err := command.Run(cCtx, c.testArgs) diff --git a/help.go b/help.go index dcedfa6abb..6143de597e 100644 --- a/help.go +++ b/help.go @@ -68,6 +68,15 @@ var helpCommand = &Command{ } // Case 3, 5 + if (len(cCtx.Command.Subcommands) == 1 && !cCtx.Command.HideHelp) || + (len(cCtx.Command.Subcommands) == 0 && cCtx.Command.HideHelp) { + templ := cCtx.Command.CustomHelpTemplate + if templ == "" { + templ = CommandHelpTemplate + } + HelpPrinter(cCtx.App.Writer, templ, cCtx.Command) + return nil + } return ShowSubcommandHelp(cCtx) }, } @@ -230,8 +239,9 @@ func ShowCommandHelpAndExit(c *Context, command string, code int) { // ShowCommandHelp prints help for the given command func ShowCommandHelp(ctx *Context, command string) error { + commands := ctx.App.Commands - if ctx.Command != nil { + if ctx.Command.Subcommands != nil { commands = ctx.Command.Subcommands } for _, c := range commands {