Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion command_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down
16 changes: 16 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading