From dab5f013125e54e52e3b603670c5a6c3a27dfabf Mon Sep 17 00:00:00 2001 From: Naveen Gogineni Date: Sun, 25 Sep 2022 14:53:53 -0400 Subject: [PATCH] Cleanup --- app.go | 24 +----------------------- command.go | 37 ++++++++++++++++++++++++++----------- 2 files changed, 27 insertions(+), 34 deletions(-) diff --git a/app.go b/app.go index a5d3608be9..ec297e8426 100644 --- a/app.go +++ b/app.go @@ -270,29 +270,7 @@ func (a *App) RunContext(ctx context.Context, arguments []string) (err error) { cCtx := NewContext(a, nil, &Context{Context: ctx}) cCtx.shellComplete = shellComplete - a.rootCommand = &Command{ - 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, - HideHelp: a.HideHelp, - HideHelpCommand: a.HideHelpCommand, - OnUsageError: a.OnUsageError, - CustomHelpTemplate: a.CustomAppHelpTemplate, - Usage: a.Usage, - UsageText: a.UsageText, - Description: a.Description, - ArgsUsage: a.ArgsUsage, - BashComplete: a.BashComplete, - categories: a.categories, - helpAction: helpCommand.Action, - isRoot: true, - } + a.rootCommand = newRootCommand(a) cCtx.Command = a.rootCommand return a.rootCommand.Run(cCtx, arguments) diff --git a/command.go b/command.go index 32e90ee0ca..d909beef24 100644 --- a/command.go +++ b/command.go @@ -69,8 +69,6 @@ type Command struct { // if this is a root "special" command isRoot bool - - helpAction ActionFunc } type Commands []*Command @@ -108,6 +106,31 @@ func (cmd *Command) Command(name string) *Command { return nil } +func newRootCommand(a *App) *Command { + return &Command{ + 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, + HideHelp: a.HideHelp, + HideHelpCommand: a.HideHelpCommand, + OnUsageError: a.OnUsageError, + CustomHelpTemplate: a.CustomAppHelpTemplate, + Usage: a.Usage, + UsageText: a.UsageText, + Description: a.Description, + ArgsUsage: a.ArgsUsage, + BashComplete: a.BashComplete, + categories: a.categories, + isRoot: true, + } +} + func (c *Command) setup(ctx *Context) { if c.Command(helpCommand.Name) == nil && !c.HideHelp { if !c.HideHelpCommand { @@ -115,10 +138,6 @@ func (c *Command) setup(ctx *Context) { } } - if c.helpAction == nil { - c.helpAction = helpCommand.Action - } - if !c.HideHelp && HelpFlag != nil { // append help to flags c.appendFlag(HelpFlag) @@ -185,11 +204,7 @@ func (c *Command) Run(cCtx *Context, arguments []string) (err error) { } if checkHelp(cCtx) { - return c.helpAction(cCtx) - } - - if cCtx.Args().First() == "help" && c.Action == nil { - return c.helpAction(cCtx) + return helpCommand.Action(cCtx) } if c.isRoot && !cCtx.App.HideVersion && checkVersion(cCtx) {