Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix:(issue_1548) Check root before run default cmd #1549

Merged
merged 1 commit into from Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion command.go
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
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"))
}