Skip to content

Commit

Permalink
Merge pull request #1581 from remiposo/remiposo/issue_1570
Browse files Browse the repository at this point in the history
Fix: Nested help command has a incorrect HelpName
  • Loading branch information
dearchap authored Nov 11, 2022
2 parents 689dfd6 + 6185f5c commit d219eb1
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 5 deletions.
1 change: 1 addition & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ func (a *App) Setup() {

if a.Command(helpCommand.Name) == nil && !a.HideHelp {
if !a.HideHelpCommand {
helpCommand.HelpName = fmt.Sprintf("%s %s", a.HelpName, helpName)
a.appendCommand(helpCommand)
}

Expand Down
1 change: 1 addition & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func (cmd *Command) Command(name string) *Command {
func (c *Command) setup(ctx *Context) {
if c.Command(helpCommand.Name) == nil && !c.HideHelp {
if !c.HideHelpCommand {
helpCommand.HelpName = fmt.Sprintf("%s %s", c.HelpName, helpName)
c.Subcommands = append(c.Subcommands, helpCommand)
}
}
Expand Down
14 changes: 9 additions & 5 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ var helpCommandDontUse = &Command{
Aliases: []string{helpAlias},
Usage: "Shows a list of commands or help for one command",
ArgsUsage: "[command]",
HideHelp: true,
}

var helpCommand = &Command{
Name: helpName,
Aliases: []string{helpAlias},
Usage: "Shows a list of commands or help for one command",
ArgsUsage: "[command]",
HideHelp: true,
Action: func(cCtx *Context) error {
args := cCtx.Args()
argsPresent := args.First() != ""
Expand Down Expand Up @@ -246,11 +248,13 @@ func ShowCommandHelp(ctx *Context, command string) error {
}
for _, c := range commands {
if c.HasName(command) {
if !ctx.App.HideHelpCommand && !c.HasName(helpName) && len(c.Subcommands) != 0 && c.Command(helpName) == nil {
c.Subcommands = append(c.Subcommands, helpCommandDontUse)
}
if !ctx.App.HideHelp && HelpFlag != nil {
c.appendFlag(HelpFlag)
if !c.HideHelp {
if !c.HideHelpCommand && len(c.Subcommands) != 0 && c.Command(helpName) == nil {
c.Subcommands = append(c.Subcommands, helpCommandDontUse)
}
if HelpFlag != nil {
c.appendFlag(HelpFlag)
}
}
templ := c.CustomHelpTemplate
if templ == "" {
Expand Down
147 changes: 147 additions & 0 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,88 @@ func Test_helpCommand_InHelpOutput(t *testing.T) {
}
}

func Test_helpCommand_HelpName(t *testing.T) {
var tests = []struct {
name string
args []string
want string
}{
{
name: "app help's helpName",
args: []string{"app", "help", "help"},
want: "app help -",
},
{
name: "app help's helpName via flag",
args: []string{"app", "-h", "help"},
want: "app help -",
},
{
name: "cmd help's helpName",
args: []string{"app", "cmd", "help", "help"},
want: "app cmd help -",
},
{
name: "cmd help's helpName via flag",
args: []string{"app", "cmd", "-h", "help"},
want: "app cmd help -",
},
{
name: "subcmd help's helpName",
args: []string{"app", "cmd", "subcmd", "help", "help"},
want: "app cmd subcmd help -",
},
{
name: "subcmd help's helpName via flag",
args: []string{"app", "cmd", "subcmd", "-h", "help"},
want: "app cmd subcmd help -",
},
}
for _, tt := range tests {
buf := &bytes.Buffer{}
t.Run(tt.name, func(t *testing.T) {
app := &App{
Name: "app",
Commands: []*Command{
{
Name: "cmd",
Subcommands: []*Command{{Name: "subcmd"}},
},
},
Writer: buf,
}
err := app.Run(tt.args)
expect(t, err, nil)
got := buf.String()
if !strings.Contains(got, tt.want) {
t.Errorf("Expected %q contained - Got %q", tt.want, got)
}
})
}
}

func Test_helpCommand_HideHelpCommand(t *testing.T) {
buf := &bytes.Buffer{}
app := &App{
Name: "app",
Writer: buf,
}
err := app.Run([]string{"app", "help", "help"})
expect(t, err, nil)
got := buf.String()
notWant := "COMMANDS:"
if strings.Contains(got, notWant) {
t.Errorf("Not expected %q - Got %q", notWant, got)
}
}

func Test_helpCommand_HideHelpFlag(t *testing.T) {
app := newTestApp()
if err := app.Run([]string{"app", "help", "-h"}); err == nil {
t.Errorf("Expected flag error - Got nil")
}
}

func Test_helpSubcommand_Action_ErrorIfNoTopic(t *testing.T) {
app := &App{}

Expand Down Expand Up @@ -228,6 +310,71 @@ func TestShowAppHelp_CommandAliases(t *testing.T) {
}
}

func TestShowCommandHelp_AppendHelp(t *testing.T) {
var tests = []struct {
name string
hideHelp bool
hideHelpCommand bool
args []string
wantHelpCommand bool
wantHelpFlag bool
}{
{
name: "with HideHelp",
hideHelp: true,
args: []string{"app"},
wantHelpCommand: false,
wantHelpFlag: false,
},
{
name: "with HideHelpCommand",
hideHelpCommand: true,
args: []string{"app"},
wantHelpCommand: false,
wantHelpFlag: true,
},
{
name: "with Subcommand",
args: []string{"app"},
wantHelpCommand: true,
wantHelpFlag: true,
},
{
name: "without Subcommand",
args: []string{"app", "cmd"},
wantHelpCommand: false,
wantHelpFlag: true,
},
}
for _, tt := range tests {
buf := &bytes.Buffer{}
t.Run(tt.name, func(t *testing.T) {
app := &App{
Name: "app",
Action: func(ctx *Context) error { return ShowCommandHelp(ctx, "cmd") },
Commands: []*Command{
{
Name: "cmd",
HideHelp: tt.hideHelp,
HideHelpCommand: tt.hideHelpCommand,
Action: func(ctx *Context) error { return ShowCommandHelp(ctx, "subcmd") },
Subcommands: []*Command{{Name: "subcmd"}},
},
},
Writer: buf,
}
err := app.Run(tt.args)
expect(t, err, nil)
got := buf.String()
gotHelpCommand := strings.Contains(got, "help, h Shows a list of commands or help for one command")
gotHelpFlag := strings.Contains(got, "--help, -h show help")
if gotHelpCommand != tt.wantHelpCommand || gotHelpFlag != tt.wantHelpFlag {
t.Errorf("ShowCommandHelp() return unexpected help message - Got %q", got)
}
})
}
}

func TestShowCommandHelp_HelpPrinter(t *testing.T) {
/*doublecho := func(text string) string {
return text + " " + text
Expand Down

0 comments on commit d219eb1

Please sign in to comment.