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

Allow help to exit with status other than 1 #80

Open
chuckwondo opened this issue Apr 22, 2021 · 3 comments · May be fixed by #213
Open

Allow help to exit with status other than 1 #80

chuckwondo opened this issue Apr 22, 2021 · 3 comments · May be fixed by #213

Comments

@chuckwondo
Copy link
Contributor

I would like to be able to exit with status 0 when specifying -h or --help, but it appears that 1 is hard-coded as the exit status. Perhaps providing a means for specifying a different exit status when help is invoked would be nice.

@cspotcode
Copy link

Are there any workaround until this is added to cmd-ts? Catching and detecting the help error and changing process.exitCode or something?

Also, I'm curious, why was it implemented this way? I've done some testing, and most CLIs exit with code 0 when you run --help.

Here are a few commands I tested, all exit with code 0:

kubectl --help
helm --help
cp --help
ls --help
cat --help
node --help

@uncenter
Copy link

Looks like this is the line that needs to be changed:

throw new Exit({ exitCode: 1, message, into: 'stdout' });

You can see that -h, --help exits with code 1 while -v, --version exits with code 0:
if (breaker.value === 'help') {
const message = value.printHelp(context);
throw new Exit({ exitCode: 1, message, into: 'stdout' });
} else if (breaker.value === 'version') {
const message = value.version || '0.0.0';
throw new Exit({ exitCode: 0, message, into: 'stdout' });
}

@uncenter uncenter linked a pull request Sep 11, 2023 that will close this issue
@lgarron
Copy link

lgarron commented Nov 7, 2024

I think this is an important detail. I believe the most useful and most common convention is:

  • If the provided arguments are invalid, the program prints the help and exits with a non-zero exit code.
  • If --help is explicitly passed, the program prints the help and exits with code 0.

This is particularly useful for testing that a program is installed correctly, for example to test in a Homebrew formula.

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

Successfully merging a pull request may close this issue.

4 participants