Skip to content

Commit 82bdf5f

Browse files
authored
Merge pull request #1549 from smalnote/smalnote/issue_1548
Fix:(issue_1548) Check root before run default cmd
2 parents 7563894 + ba96587 commit 82bdf5f

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

command.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ func (c *Command) Run(cCtx *Context, arguments ...string) (err error) {
252252
}
253253
}
254254
}
255-
} else if cCtx.App.DefaultCommand != "" {
255+
} else if c.isRoot && cCtx.App.DefaultCommand != "" {
256256
if dc := cCtx.App.Command(cCtx.App.DefaultCommand); dc != c {
257257
cmd = dc
258258
}

command_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -485,3 +485,33 @@ func TestCommand_VisibleFlagCategories(t *testing.T) {
485485
t.Errorf("unexpected flag %+v", fl.Names())
486486
}
487487
}
488+
489+
func TestCommand_RunSubcommandWithDefault(t *testing.T) {
490+
app := &App{
491+
Version: "some version",
492+
Name: "app",
493+
DefaultCommand: "foo",
494+
Commands: []*Command{
495+
{
496+
Name: "foo",
497+
Action: func(ctx *Context) error {
498+
return errors.New("should not run this subcommand")
499+
},
500+
},
501+
{
502+
Name: "bar",
503+
Usage: "this is for testing",
504+
Subcommands: []*Command{{}}, // some subcommand
505+
Action: func(*Context) error {
506+
return nil
507+
},
508+
},
509+
},
510+
}
511+
512+
err := app.Run([]string{"app", "bar"})
513+
expect(t, err, nil)
514+
515+
err = app.Run([]string{"app"})
516+
expect(t, err, errors.New("should not run this subcommand"))
517+
}

0 commit comments

Comments
 (0)