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

Exit with code 0 when invalid subcommand is passed #2034

Closed
ldufresnegs opened this issue Sep 28, 2023 · 1 comment
Closed

Exit with code 0 when invalid subcommand is passed #2034

ldufresnegs opened this issue Sep 28, 2023 · 1 comment

Comments

@ldufresnegs
Copy link

ldufresnegs commented Sep 28, 2023

Hey, I might be missing something completely, but there seems to be a undesirable behavior when an invalid sub-command is passed. Indeed, the help is shown, but the exit code is 0.

The following code early exit when there is the error is flag.ErrHelp which I assume is meant for when the -h is passed, but also happen when an invalid sub-command is passed.

cobra/command.go

Lines 1094 to 1097 in bd4d165

if errors.Is(err, flag.ErrHelp) {
cmd.HelpFunc()(cmd, args)
return cmd, nil
}

Indeed, in execute(), there is the following:

cobra/command.go

Lines 918 to 920 in bd4d165

if !c.Runnable() {
return flag.ErrHelp
}

I'm I missing something? It seems that the desirable behavior would be to return an error when an invalid sub-command is passed.

The only workaround that may work is to call ExecuteC and check that the returned sub-command is not the root command.
Edit: Nevermind, can't distinguish with an --help flag.

@marckhouzam
Copy link
Collaborator

Duplicate of #1156

@marckhouzam marckhouzam marked this as a duplicate of #1156 Sep 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants