Skip to content

Commit

Permalink
Merge pull request #1549 from smalnote/smalnote/issue_1548
Browse files Browse the repository at this point in the history
Fix:(issue_1548) Check root before run default cmd
  • Loading branch information
dearchap authored Oct 25, 2022
2 parents 7563894 + ba96587 commit 82bdf5f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func (c *Command) Run(cCtx *Context, arguments ...string) (err error) {
}
}
}
} else if cCtx.App.DefaultCommand != "" {
} else if c.isRoot && cCtx.App.DefaultCommand != "" {
if dc := cCtx.App.Command(cCtx.App.DefaultCommand); dc != c {
cmd = dc
}
Expand Down
30 changes: 30 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,3 +485,33 @@ func TestCommand_VisibleFlagCategories(t *testing.T) {
t.Errorf("unexpected flag %+v", fl.Names())
}
}

func TestCommand_RunSubcommandWithDefault(t *testing.T) {
app := &App{
Version: "some version",
Name: "app",
DefaultCommand: "foo",
Commands: []*Command{
{
Name: "foo",
Action: func(ctx *Context) error {
return errors.New("should not run this subcommand")
},
},
{
Name: "bar",
Usage: "this is for testing",
Subcommands: []*Command{{}}, // some subcommand
Action: func(*Context) error {
return nil
},
},
},
}

err := app.Run([]string{"app", "bar"})
expect(t, err, nil)

err = app.Run([]string{"app"})
expect(t, err, errors.New("should not run this subcommand"))
}

0 comments on commit 82bdf5f

Please sign in to comment.