mirror of
https://github.com/spf13/cobra
synced 2025-05-07 22:07:23 +00:00
Fix #1056
This commit is contained in:
parent
95f2f73ed9
commit
578111308e
2 changed files with 31 additions and 2 deletions
|
@ -801,7 +801,11 @@ func (c *Command) execute(a []string) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if !c.Runnable() {
|
if !c.Runnable() {
|
||||||
return ErrSubCommandRequired
|
if c.IsAdditionalHelpTopicCommand() {
|
||||||
|
return flag.ErrHelp
|
||||||
|
} else {
|
||||||
|
return ErrSubCommandRequired
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c.preRun()
|
c.preRun()
|
||||||
|
|
|
@ -897,11 +897,36 @@ func TestFlagsInUsage(t *testing.T) {
|
||||||
checkStringContains(t, output, "[flags]")
|
checkStringContains(t, output, "[flags]")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHelpExecutedOnNonRunnableChild(t *testing.T) {
|
func TestHelpExecutedOnAdditionalHelpTopicCommand(t *testing.T) {
|
||||||
rootCmd := &Command{Use: "root", Run: emptyRun}
|
rootCmd := &Command{Use: "root", Run: emptyRun}
|
||||||
childCmd := &Command{Use: "child", Long: "Long description"}
|
childCmd := &Command{Use: "child", Long: "Long description"}
|
||||||
|
|
||||||
rootCmd.AddCommand(childCmd)
|
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")
|
output, err := executeCommand(rootCmd, "child")
|
||||||
if err != ErrSubCommandRequired {
|
if err != ErrSubCommandRequired {
|
||||||
t.Errorf("Expected error")
|
t.Errorf("Expected error")
|
||||||
|
|
Loading…
Add table
Reference in a new issue