Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func helpCommandAction(ctx context.Context, cmd *Command) error {
// $ app --help / -h # flag; show root help (ignores subsequent args)
// $ app help / h # subcommand; show root help
// $ app help / h foo # subcommand; show help for subcommand "foo"
// $ app help / h foo bar # subcommand; show help for foo's subcommand "bar"
// $ app --help / -h foo # flag; show help for subcommand "foo" if it
// # matches a child command; otherwise ignore it
// $ app foo --help / -h # flag on subcommand; show help for "foo"
Expand All @@ -118,8 +119,17 @@ func helpCommandAction(ctx context.Context, cmd *Command) error {
// foo is the command for which help needs to be shown
if firstArg != "" {
if !helpFromFlag || cmd.Command(firstArg) != nil {
tracef("returning ShowCommandHelp with %[1]q", firstArg)
return ShowCommandHelp(ctx, cmd, firstArg)
// follow the args down as long as they name subcommands
parent, name := cmd, firstArg
for _, arg := range args.Tail() {
child := parent.Command(name)
if child == nil || child.Command(arg) == nil {
break
}
parent, name = child, arg
}
tracef("returning ShowCommandHelp with %[1]q", name)
return ShowCommandHelp(ctx, parent, name)
}

tracef("ignoring positional help argument %[1]q that is not a child command", firstArg)
Expand Down
56 changes: 56 additions & 0 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,62 @@ func TestHelpCommand_FullName(t *testing.T) {
}
}

func TestHelpCommand_NestedPath(t *testing.T) {
testCases := []struct {
name string
args []string
contains string
}{
{
name: "help with a subcommand path",
args: []string{"app", "help", "cmd", "subcmd"},
contains: "app cmd subcmd - runs subcmd",
},
{
name: "help with a subcommand alias in the path",
args: []string{"app", "help", "cmd", "s"},
contains: "app cmd subcmd - runs subcmd",
},
{
name: "help flag with a subcommand path",
args: []string{"app", "-h", "cmd", "subcmd"},
contains: "app cmd subcmd - runs subcmd",
},
{
name: "help stops at an arg that is not a subcommand",
args: []string{"app", "help", "cmd", "nope"},
contains: "app cmd - runs cmd",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
out := &bytes.Buffer{}
cmd := &Command{
Name: "app",
Commands: []*Command{
{
Name: "cmd",
Usage: "runs cmd",
Commands: []*Command{
{
Name: "subcmd",
Aliases: []string{"s"},
Usage: "runs subcmd",
},
},
},
},
Writer: out,
ErrWriter: out,
}

r := require.New(t)
r.NoError(cmd.Run(buildTestContext(t), tc.args))
r.Contains(out.String(), tc.contains)
})
}
}

func Test_helpCommand_HideHelpCommand(t *testing.T) {
buf := &bytes.Buffer{}
cmd := &Command{
Expand Down
Loading