Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dearchap committed Sep 25, 2022
1 parent 6febf55 commit dab5f01
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 34 deletions.
24 changes: 1 addition & 23 deletions app.go
Expand Up @@ -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)
Expand Down
37 changes: 26 additions & 11 deletions command.go
Expand Up @@ -69,8 +69,6 @@ type Command struct {

// if this is a root "special" command
isRoot bool

helpAction ActionFunc
}

type Commands []*Command
Expand Down Expand Up @@ -108,17 +106,38 @@ 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 {
c.Subcommands = append(c.Subcommands, helpCommand)
}
}

if c.helpAction == nil {
c.helpAction = helpCommand.Action
}

if !c.HideHelp && HelpFlag != nil {
// append help to flags
c.appendFlag(HelpFlag)
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit dab5f01

Please sign in to comment.