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

Partial Revert of #922 #1068

Merged
merged 1 commit into from Mar 27, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 1 addition & 12 deletions command.go
Expand Up @@ -18,7 +18,6 @@ package cobra
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
Expand All @@ -29,8 +28,6 @@ import (
flag "github.com/spf13/pflag"
)

var ErrSubCommandRequired = errors.New("subcommand is required")

// FParseErrWhitelist configures Flag parse errors to be ignored
type FParseErrWhitelist flag.ParseErrorsWhitelist

Expand Down Expand Up @@ -801,7 +798,7 @@ func (c *Command) execute(a []string) (err error) {
}

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

c.preRun()
Expand Down Expand Up @@ -952,14 +949,6 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
return cmd, nil
}

// If command wasn't runnable, show full help, but do return the error.
// This will result in apps by default returning a non-success exit code, but also gives them the option to
// handle specially.
if err == ErrSubCommandRequired {
cmd.HelpFunc()(cmd, args)
return cmd, err
}

// If root command has SilentErrors flagged,
// all subcommands should respect it
if !cmd.SilenceErrors && !c.SilenceErrors {
Expand Down
4 changes: 2 additions & 2 deletions command_test.go
Expand Up @@ -903,8 +903,8 @@ func TestHelpExecutedOnNonRunnableChild(t *testing.T) {
rootCmd.AddCommand(childCmd)

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

checkStringContains(t, output, childCmd.Long)
Expand Down