diff --git a/command_run.go b/command_run.go index 94e7182491..7cdf369f21 100644 --- a/command_run.go +++ b/command_run.go @@ -290,7 +290,9 @@ func (cmd *Command) run(ctx context.Context, osArgs []string) (_ context.Context argsWithDefault := cmd.argsWithDefaultCommand(cmd.parsedArgs) tracef("using default command args=%[1]q (cmd=%[2]q)", argsWithDefault, cmd.Name) subCmd = cmd.Command(argsWithDefault.First()) - cmd.parsedArgs = argsWithDefault + if subCmd != nil { + cmd.parsedArgs = argsWithDefault + } } } } else if cmd.DefaultCommand != "" { diff --git a/command_test.go b/command_test.go index 263ad82cde..e3d1c32136 100644 --- a/command_test.go +++ b/command_test.go @@ -906,6 +906,22 @@ func TestCommand_RunDefaultCommand(t *testing.T) { } } +func TestCommand_RunDefaultCommandUnknownNameDoesNotRewriteArgs(t *testing.T) { + var gotArgs Args + cmd := &Command{ + Name: "greet", + DefaultCommand: "helo", + Action: func(_ context.Context, c *Command) error { + gotArgs = c.Args() + return nil + }, + } + + err := cmd.Run(buildTestContext(t), []string{"greet", "world"}) + assert.NoError(t, err) + assert.Equal(t, []string{"world"}, gotArgs.Slice()) +} + var defaultCommandSubCommandTests = []struct { cmdName string subCmd string