-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
closes #2130 new command flag ErrorOnUnknownSubcommand #2167
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,10 @@ type Group struct { | |
Title string | ||
} | ||
|
||
// ErrUnknownSubcommand is returned by Command.Execute() when the subcommand was not found. | ||
// Hereto, the ErrorOnUnknownSubcommand flag must be set true. | ||
var ErrUnknownSubcommand = errors.New("subcommand is unknown") | ||
|
||
// Command is just that, a command for your application. | ||
// E.g. 'go run ...' - 'run' is the command. Cobra requires | ||
// you to define the usage and description as part of your command | ||
|
@@ -923,9 +927,12 @@ func (c *Command) execute(a []string) (err error) { | |
} | ||
|
||
if !c.Runnable() { | ||
return flag.ErrHelp | ||
if EnableErrorOnUnknownSubcommand { | ||
return ErrUnknownSubcommand | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's only return the error if there is at least one arg. You should be able to move up the following lines to get the number of args:
|
||
} else { | ||
return flag.ErrHelp | ||
} | ||
} | ||
|
||
c.preRun() | ||
|
||
defer c.postRun() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ package cobra | |
import ( | ||
"bytes" | ||
"context" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
@@ -174,6 +175,102 @@ func TestSubcommandExecuteC(t *testing.T) { | |
} | ||
} | ||
|
||
func TestSubcommandExecuteMissingSubcommand(t *testing.T) { | ||
rootCmd := &Command{Use: "root", Run: emptyRun} | ||
const childName = "child" | ||
const grandchildName = "grandchild" | ||
EnableErrorOnUnknownSubcommand = false | ||
defer func() { EnableErrorOnUnknownSubcommand = defaultErrorOnUnknownSubcommand }() | ||
childCmd := &Command{Use: childName, Run: nil} | ||
child2Cmd := &Command{Use: grandchildName, Run: emptyRun} | ||
rootCmd.AddCommand(childCmd) | ||
childCmd.AddCommand(child2Cmd) | ||
|
||
// test existing command | ||
c, output, err := executeCommandC(rootCmd, childName) | ||
if !strings.HasPrefix(output, "Usage:") { | ||
t.Errorf("Unexpected output: %v", output) | ||
} | ||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
if c.Name() != childName { | ||
t.Errorf(`invalid command returned from ExecuteC: expected "child"', got: %q`, c.Name()) | ||
} | ||
|
||
// test existing sub command | ||
c, output, err = executeCommandC(rootCmd, childName, grandchildName) | ||
if output != "" { | ||
t.Errorf("Unexpected output: %v", output) | ||
} | ||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
if c.Name() != grandchildName { | ||
t.Errorf(`invalid command returned from ExecuteC: expected "grandchild"', got: %q`, c.Name()) | ||
} | ||
|
||
// now test a command which does not exist, we will get no error, just "Usage:" is printed | ||
c, output, err = executeCommandC(rootCmd, childName, "unknownChild") | ||
if !strings.HasPrefix(output, "Usage:") { | ||
t.Errorf("Expected: 'Usage: ...'\nGot:\n %q\n", output) | ||
} | ||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
if c.Name() != childName { | ||
t.Errorf(`invalid command returned from ExecuteC: expected "child"', got: %q`, c.Name()) | ||
} | ||
} | ||
|
||
func TestSubcommandExecuteMissingSubcommandWithErrorOnUnknownSubcommand(t *testing.T) { | ||
rootCmd := &Command{Use: "root", Run: emptyRun} | ||
const childName = "child" | ||
const grandchildName = "grandchild" | ||
EnableErrorOnUnknownSubcommand = true | ||
defer func() { EnableErrorOnUnknownSubcommand = defaultErrorOnUnknownSubcommand }() | ||
childCmd := &Command{Use: childName, Run: nil} | ||
child2Cmd := &Command{Use: grandchildName, Run: emptyRun} | ||
rootCmd.AddCommand(childCmd) | ||
childCmd.AddCommand(child2Cmd) | ||
|
||
// test existing command | ||
c, output, err := executeCommandC(rootCmd, childName) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This case should not show an error but should print the usage like before. |
||
if !strings.HasPrefix(output, "Error:") { | ||
t.Errorf("Unexpected output: %v", output) | ||
} | ||
if !errors.Is(err, ErrUnknownSubcommand) { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
if c.Name() != childName { | ||
t.Errorf(`invalid command returned from ExecuteC: expected "child"', got: %q`, c.Name()) | ||
} | ||
|
||
// test existing sub command | ||
c, output, err = executeCommandC(rootCmd, childName, grandchildName) | ||
if output != "" { | ||
t.Errorf("Unexpected output: %v", output) | ||
} | ||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
if c.Name() != grandchildName { | ||
t.Errorf(`invalid command returned from ExecuteC: expected "child"', got: %q`, c.Name()) | ||
} | ||
|
||
// now test a command which does not exist, we expect an error because of the ErrorOnUnknownSubcommand flag | ||
c, output, err = executeCommandC(rootCmd, childName, "unknownChild") | ||
if !strings.HasPrefix(output, "Error:") { | ||
t.Errorf("Unexpected output: %v", output) | ||
} | ||
if !errors.Is(err, ErrUnknownSubcommand) { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
if c.Name() != childName { | ||
t.Errorf(`invalid command returned from ExecuteC: expected "child"', got: %q`, c.Name()) | ||
} | ||
} | ||
|
||
func TestExecuteContext(t *testing.T) { | ||
ctx := context.TODO() | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's format this command message like the one in
legacyArgs()
to have a consistent behaviour.fmt.Errorf("unknown command %q for %q%s", args[0], cmd.CommandPath(), cmd.findSuggestions(args[0]))