mirror of
https://github.com/spf13/cobra.git
synced 2024-11-15 09:48:19 -07:00
Return an error in the case of unrunnable subcommand
* credit to @chriswhelix for initial commit
This commit is contained in:
parent
9552679939
commit
9334a46bd6
16
command.go
16
command.go
@ -17,6 +17,7 @@ package cobra
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
@ -27,6 +28,9 @@ import (
|
||||
flag "github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
// NotRunnable defines subcommand error
|
||||
var NotRunnable = errors.New("subcommand is required")
|
||||
|
||||
// FParseErrWhitelist configures Flag parse errors to be ignored
|
||||
type FParseErrWhitelist flag.ParseErrorsWhitelist
|
||||
|
||||
@ -369,7 +373,7 @@ func (c *Command) HelpFunc() func(*Command, []string) {
|
||||
}
|
||||
return func(c *Command, a []string) {
|
||||
c.mergePersistentFlags()
|
||||
err := tmpl(c.OutOrStdout(), c.HelpTemplate(), c)
|
||||
err := tmpl(c.OutOrStderr(), c.HelpTemplate(), c)
|
||||
if err != nil {
|
||||
c.Println(err)
|
||||
}
|
||||
@ -786,7 +790,7 @@ func (c *Command) execute(a []string) (err error) {
|
||||
}
|
||||
|
||||
if !c.Runnable() {
|
||||
return flag.ErrHelp
|
||||
return NotRunnable
|
||||
}
|
||||
|
||||
c.preRun()
|
||||
@ -920,6 +924,14 @@ 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 == NotRunnable {
|
||||
cmd.HelpFunc()(cmd, args)
|
||||
return cmd, err
|
||||
}
|
||||
|
||||
// If root command has SilentErrors flagged,
|
||||
// all subcommands should respect it
|
||||
if !cmd.SilenceErrors && !c.SilenceErrors {
|
||||
|
@ -836,8 +836,8 @@ func TestHelpExecutedOnNonRunnableChild(t *testing.T) {
|
||||
rootCmd.AddCommand(childCmd)
|
||||
|
||||
output, err := executeCommand(rootCmd, "child")
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
if err != NotRunnable {
|
||||
t.Errorf("Expected error")
|
||||
}
|
||||
|
||||
checkStringContains(t, output, childCmd.Long)
|
||||
|
Loading…
Reference in New Issue
Block a user