mirror of
https://github.com/spf13/cobra
synced 2024-11-24 14:47:12 +00:00
Issue Reference: https://github.com/spf13/cobra/issues/1056 https://github.com/spf13/cobra/pull/922 introduced a new error type that emitted when a command was not runnable. This caused all commands w/o a run function set to error w/ that message and a status code of 1. This change reverts the addition of that new error. Similar functionality can be accomplished by leveraging RunE.
This commit is contained in:
parent
95f2f73ed9
commit
6607e6b860
2 changed files with 3 additions and 14 deletions
13
command.go
13
command.go
|
@ -18,7 +18,6 @@ package cobra
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
@ -29,8 +28,6 @@ import (
|
||||||
flag "github.com/spf13/pflag"
|
flag "github.com/spf13/pflag"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ErrSubCommandRequired = errors.New("subcommand is required")
|
|
||||||
|
|
||||||
// FParseErrWhitelist configures Flag parse errors to be ignored
|
// FParseErrWhitelist configures Flag parse errors to be ignored
|
||||||
type FParseErrWhitelist flag.ParseErrorsWhitelist
|
type FParseErrWhitelist flag.ParseErrorsWhitelist
|
||||||
|
|
||||||
|
@ -801,7 +798,7 @@ func (c *Command) execute(a []string) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if !c.Runnable() {
|
if !c.Runnable() {
|
||||||
return ErrSubCommandRequired
|
return flag.ErrHelp
|
||||||
}
|
}
|
||||||
|
|
||||||
c.preRun()
|
c.preRun()
|
||||||
|
@ -952,14 +949,6 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
|
||||||
return cmd, nil
|
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,
|
// If root command has SilentErrors flagged,
|
||||||
// all subcommands should respect it
|
// all subcommands should respect it
|
||||||
if !cmd.SilenceErrors && !c.SilenceErrors {
|
if !cmd.SilenceErrors && !c.SilenceErrors {
|
||||||
|
|
|
@ -903,8 +903,8 @@ func TestHelpExecutedOnNonRunnableChild(t *testing.T) {
|
||||||
rootCmd.AddCommand(childCmd)
|
rootCmd.AddCommand(childCmd)
|
||||||
|
|
||||||
output, err := executeCommand(rootCmd, "child")
|
output, err := executeCommand(rootCmd, "child")
|
||||||
if err != ErrSubCommandRequired {
|
if err != nil {
|
||||||
t.Errorf("Expected error")
|
t.Errorf("Unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
checkStringContains(t, output, childCmd.Long)
|
checkStringContains(t, output, childCmd.Long)
|
||||||
|
|
Loading…
Reference in a new issue