Skip to content

Commit 2ac6b5a

Browse files
authored
Merge pull request #2232 from dearchap/disc_2168
Fix:(issue_2228) Fix for default command
2 parents c49c451 + 77576e3 commit 2ac6b5a

File tree

2 files changed

+80
-32
lines changed

2 files changed

+80
-32
lines changed

command_run.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"context"
66
"fmt"
77
"io"
8-
"reflect"
98
"slices"
109
"unicode"
1110
)
@@ -259,6 +258,7 @@ func (cmd *Command) run(ctx context.Context, osArgs []string) (_ context.Context
259258

260259
if cmd.SuggestCommandFunc != nil && name != "--" {
261260
name = cmd.SuggestCommandFunc(cmd.Commands, name)
261+
tracef("suggested command name=%1[q] (cmd=%[2]q)", name, cmd.Name)
262262
}
263263
subCmd = cmd.Command(name)
264264
if subCmd == nil {
@@ -270,14 +270,13 @@ func (cmd *Command) run(ctx context.Context, osArgs []string) (_ context.Context
270270
}
271271

272272
if isFlagName || hasDefault {
273-
argsWithDefault := cmd.argsWithDefaultCommand(args)
273+
argsWithDefault := cmd.argsWithDefaultCommand(cmd.parsedArgs)
274274
tracef("using default command args=%[1]q (cmd=%[2]q)", argsWithDefault, cmd.Name)
275-
if !reflect.DeepEqual(args, argsWithDefault) {
276-
subCmd = cmd.Command(argsWithDefault.First())
277-
}
275+
subCmd = cmd.Command(argsWithDefault.First())
276+
cmd.parsedArgs = argsWithDefault
278277
}
279278
}
280-
} else if cmd.parent == nil && cmd.DefaultCommand != "" {
279+
} else if cmd.DefaultCommand != "" {
281280
tracef("no positional args present; checking default command %[1]q (cmd=%[2]q)", cmd.DefaultCommand, cmd.Name)
282281

283282
if dc := cmd.Command(cmd.DefaultCommand); dc != cmd {

command_test.go

Lines changed: 75 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -814,37 +814,79 @@ func TestCommand_Command(t *testing.T) {
814814
var defaultCommandTests = []struct {
815815
cmdName string
816816
defaultCmd string
817+
args []string
817818
errNotExpected bool
818819
}{
819-
{"foobar", "foobar", true},
820-
{"batbaz", "foobar", true},
821-
{"b", "", true},
822-
{"f", "", true},
823-
{"", "foobar", true},
824-
// TBD
825-
//{"", "", true},
826-
//{" ", "", false},
827-
{"bat", "batbaz", true},
828-
{"nothing", "batbaz", true},
829-
{"nothing", "", false},
820+
{"foobar", "foobar", nil, true},
821+
{"batbaz", "foobar", nil, true},
822+
{"b", "", nil, true},
823+
{"f", "", nil, true},
824+
{"", "foobar", nil, true},
825+
{"", "", nil, true},
826+
{" ", "", nil, true},
827+
{"bat", "batbaz", nil, true},
828+
{"nothing", "batbaz", nil, true},
829+
{"nothing", "", nil, false},
830+
{"foobar", "foobar", []string{"xy", "zdf"}, true},
831+
{"", "foobar", []string{"xy", "zdf"}, true},
830832
}
831833

832834
func TestCommand_RunDefaultCommand(t *testing.T) {
833835
for _, test := range defaultCommandTests {
834-
testTitle := fmt.Sprintf("command=%[1]s-default=%[2]s", test.cmdName, test.defaultCmd)
836+
testTitle := fmt.Sprintf("command=%[1]s-default=%[2]s-args=%[3]v", test.cmdName, test.defaultCmd, test.args)
835837
t.Run(testTitle, func(t *testing.T) {
838+
fooCount := 0
839+
var fooArgs Args
840+
barCount := 0
836841
cmd := &Command{
837842
DefaultCommand: test.defaultCmd,
838843
Commands: []*Command{
839-
{Name: "foobar", Aliases: []string{"f"}},
840-
{Name: "batbaz", Aliases: []string{"b"}},
844+
{
845+
Name: "foobar",
846+
Aliases: []string{"f"},
847+
Action: func(ctx context.Context, c *Command) error {
848+
fooCount++
849+
fooArgs = c.Args()
850+
return nil
851+
},
852+
},
853+
{
854+
Name: "batbaz",
855+
Aliases: []string{"b"},
856+
Action: func(ctx context.Context, c *Command) error {
857+
barCount++
858+
return nil
859+
},
860+
},
841861
},
842862
}
843863

844-
err := cmd.Run(buildTestContext(t), []string{"c", test.cmdName})
864+
runArgs := []string{"c"}
865+
if test.cmdName != "" {
866+
runArgs = append(runArgs, test.cmdName)
867+
}
868+
if test.args != nil {
869+
runArgs = append(runArgs, test.args...)
870+
}
871+
err := cmd.Run(buildTestContext(t), runArgs)
845872
if test.errNotExpected {
846873
assert.NoError(t, err)
874+
if fooCount == 0 && barCount == 0 && test.defaultCmd != "" {
875+
t.Errorf("expected one of the commands to run")
876+
}
877+
if fooCount > 0 {
878+
expectedArgs := &stringSliceArgs{v: []string{}}
879+
if len(test.args) > 0 && (test.args[0] == "foobar" || test.args[0] == "f") {
880+
expectedArgs = &stringSliceArgs{v: test.args[1:]}
881+
} else if test.args != nil {
882+
expectedArgs = &stringSliceArgs{v: test.args}
883+
}
884+
assert.Equal(t, expectedArgs, fooArgs)
885+
}
847886
} else {
887+
if fooCount > 0 || barCount > 0 {
888+
t.Errorf("expected no commands to run")
889+
}
848890
assert.Error(t, err)
849891
}
850892
})
@@ -867,14 +909,14 @@ var defaultCommandSubCommandTests = []struct {
867909
{"", "jimbob", "foobar", true},
868910
{"", "j", "foobar", true},
869911
{"", "carly", "foobar", true},
870-
{"", "jimmers", "foobar", true},
871-
{"", "jimmers", "", true},
912+
{"", "jimmers", "foobar", false},
913+
{"", "jimmers", "", false},
872914
{" ", "jimmers", "foobar", true},
873-
/*{"", "", "", true},
874-
{" ", "", "", false},
875-
{" ", "j", "", false},*/
876-
{"bat", "", "batbaz", true},
877-
{"nothing", "", "batbaz", true},
915+
{"", "", "", true},
916+
{" ", "", "", true},
917+
{" ", "j", "", true},
918+
{"bat", "", "batbaz", false},
919+
{"nothing", "", "batbaz", false},
878920
{"nothing", "", "", false},
879921
{"nothing", "j", "batbaz", false},
880922
{"nothing", "carly", "", false},
@@ -899,7 +941,14 @@ func TestCommand_RunDefaultCommandWithSubCommand(t *testing.T) {
899941
},
900942
}
901943

902-
err := cmd.Run(buildTestContext(t), []string{"c", test.cmdName, test.subCmd})
944+
runArgs := []string{"c"}
945+
if test.cmdName != "" {
946+
runArgs = append(runArgs, test.cmdName)
947+
}
948+
if test.subCmd != "" {
949+
runArgs = append(runArgs, test.subCmd)
950+
}
951+
err := cmd.Run(buildTestContext(t), runArgs)
903952
if test.errNotExpected {
904953
assert.NoError(t, err)
905954
} else {
@@ -932,10 +981,10 @@ var defaultCommandFlagTests = []struct {
932981
{"", "", "", true},
933982
{" ", "", "", true},
934983
{" ", "-j", "", true},
935-
{"bat", "", "batbaz", true},
936-
{"nothing", "", "batbaz", true},
984+
{"bat", "", "batbaz", false},
985+
{"nothing", "", "batbaz", false},
937986
{"nothing", "", "", false},
938-
{"nothing", "--jimbob", "batbaz", true},
987+
{"nothing", "--jimbob", "batbaz", false},
939988
{"nothing", "--carly", "", false},
940989
}
941990

0 commit comments

Comments
 (0)