Skip to content

Commit

Permalink
Merge latest and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dearchap committed Sep 25, 2022
1 parent cfceba9 commit 748ea87
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
12 changes: 6 additions & 6 deletions app.go
Expand Up @@ -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,
Expand Down
13 changes: 4 additions & 9 deletions command.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions command_test.go
Expand Up @@ -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)
Expand Down
12 changes: 11 additions & 1 deletion help.go
Expand Up @@ -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)
},
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 748ea87

Please sign in to comment.