Skip to content

Commit

Permalink
Backward compatibility
Browse files Browse the repository at this point in the history
An error is only thrown if there are arguments to a valid command.
Without any arguments the help is printed as before.
An invalid subcommand will return an error.
  • Loading branch information
andremueller-cosateq committed Oct 25, 2024
1 parent 133c649 commit 0f196f5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
2 changes: 1 addition & 1 deletion cobra.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ var EnableTraverseRunHooks = defaultTraverseRunHooks

// EnableErrorOnUnknownSubcommand controls the behavior of subcommand handling.
// When the flag is set true the behavior of Command.Execute() will change:
// If a sub-subcommand is not found ErrUnknownSubcommand will be returned on calling
// If a sub-subcommand is not found an error will be returned on calling
// Command.Exec() instead of the old behavior where a nil error was sent.
// If the flag is false (default) the old behavior is performed.
// For this behavior the child subcommand must be nil.
Expand Down
18 changes: 7 additions & 11 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ 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
Expand Down Expand Up @@ -926,9 +922,14 @@ func (c *Command) execute(a []string) (err error) {
}
}

argWoFlags := c.Flags().Args()
if c.DisableFlagParsing {
argWoFlags = a
}

if !c.Runnable() {
if EnableErrorOnUnknownSubcommand {
return ErrUnknownSubcommand
if EnableErrorOnUnknownSubcommand && len(argWoFlags) > 0 {
return fmt.Errorf("unknown command %q for %q%s", argWoFlags[0], c.CommandPath(), c.findSuggestions(argWoFlags[0]))
} else {
return flag.ErrHelp
}
Expand All @@ -937,11 +938,6 @@ func (c *Command) execute(a []string) (err error) {

defer c.postRun()

argWoFlags := c.Flags().Args()
if c.DisableFlagParsing {
argWoFlags = a
}

if err := c.ValidateArgs(argWoFlags); err != nil {
return err
}
Expand Down
32 changes: 25 additions & 7 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package cobra
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -224,7 +223,8 @@ func TestSubcommandExecuteMissingSubcommand(t *testing.T) {
}

func TestSubcommandExecuteMissingSubcommandWithErrorOnUnknownSubcommand(t *testing.T) {
rootCmd := &Command{Use: "root", Run: emptyRun}
const rootName = "root"
rootCmd := &Command{Use: rootName, Run: emptyRun}
const childName = "child"
const grandchildName = "grandchild"
EnableErrorOnUnknownSubcommand = true
Expand All @@ -236,10 +236,10 @@ func TestSubcommandExecuteMissingSubcommandWithErrorOnUnknownSubcommand(t *testi

// test existing command
c, output, err := executeCommandC(rootCmd, childName)
if !strings.HasPrefix(output, "Error:") {
if strings.HasPrefix(output, "Error:") {
t.Errorf("Unexpected output: %v", output)
}
if !errors.Is(err, ErrUnknownSubcommand) {
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if c.Name() != childName {
Expand All @@ -258,12 +258,30 @@ func TestSubcommandExecuteMissingSubcommandWithErrorOnUnknownSubcommand(t *testi
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")
// test a child command which does not exist, we expect an error because of the ErrorOnUnknownSubcommand flag
c, output, err = executeCommandC(rootCmd, "unknownChild")
if !strings.HasPrefix(output, "Error:") {
t.Errorf("Unexpected output: %v", output)
}
if err == nil {
t.Error("Expected error")
}
if err != nil && !strings.HasPrefix(err.Error(), "unknown command") {
t.Errorf("Unexpected error: %v", err)
}
if c.Name() != rootName {
t.Errorf(`invalid command returned from ExecuteC: expected "child"', got: %q`, c.Name())
}

// test a grandchild command which does not exist, we expect an error because of the ErrorOnUnknownSubcommand flag
c, output, err = executeCommandC(rootCmd, childName, "unknownGrandChild")
if !strings.HasPrefix(output, "Error:") {
t.Errorf("Unexpected output: %v", output)
}
if !errors.Is(err, ErrUnknownSubcommand) {
if err == nil {
t.Error("Expected error")
}
if err != nil && !strings.HasPrefix(err.Error(), "unknown command") {
t.Errorf("Unexpected error: %v", err)
}
if c.Name() != childName {
Expand Down

0 comments on commit 0f196f5

Please sign in to comment.