Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1056 #1062

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion command.go
Expand Up @@ -801,7 +801,11 @@ func (c *Command) execute(a []string) (err error) {
}

if !c.Runnable() {
return ErrSubCommandRequired
if c.IsAdditionalHelpTopicCommand() {
return flag.ErrHelp
} else {
return ErrSubCommandRequired
}
}

c.preRun()
Expand Down
27 changes: 26 additions & 1 deletion command_test.go
Expand Up @@ -897,11 +897,36 @@ func TestFlagsInUsage(t *testing.T) {
checkStringContains(t, output, "[flags]")
}

func TestHelpExecutedOnNonRunnableChild(t *testing.T) {
func TestHelpExecutedOnAdditionalHelpTopicCommand(t *testing.T) {
rootCmd := &Command{Use: "root", Run: emptyRun}
childCmd := &Command{Use: "child", Long: "Long description"}

rootCmd.AddCommand(childCmd)

if !childCmd.IsAdditionalHelpTopicCommand() {
t.Errorf("child should be an additional help topic command")
}

output, err := executeCommand(rootCmd, "child")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}

checkStringContains(t, output, childCmd.Long)
}

func TestHelpExecutedOnNonRunnableChildWithGrandchild(t *testing.T) {
rootCmd := &Command{Use: "root", Run: emptyRun}
childCmd := &Command{Use: "child", Long: "Long description"}
granchildCmd := &Command{Use: "grandchild", Run: emptyRun}

childCmd.AddCommand(granchildCmd)
rootCmd.AddCommand(childCmd)

if childCmd.IsAdditionalHelpTopicCommand() {
t.Errorf("child should not be an additional help topic command")
}

output, err := executeCommand(rootCmd, "child")
if err != ErrSubCommandRequired {
t.Errorf("Expected error")
Expand Down