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

Default to exit code 2 on help message #47

Closed
wants to merge 1 commit into from

Conversation

Qix-
Copy link

@Qix- Qix- commented Aug 5, 2016

When a command fails because it's misused and shows the help message instead of executing normally, it should never fail with exit code 0 as that will improperly foul things up if the command is executed in an automated way (i.e. a script or make).

The convention is code 2, which means "misuse".

When a command fails because it's mis-used and shows the help message instead of executing normally, it should *never* fail with exit code 0 as that will improperly foul things up if the command is executed in an automated way (i.e. a script or `make`).

The convention is code 2, which means "misuse".
@@ -58,7 +58,7 @@ module.exports = (opts, minimistOpts) => {

const showHelp = code => {
console.log(help);
process.exit(code || 0);
process.exit(code || 2);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then you need to change it to process.exit(typeof code === 'number' ? code : 2);

Otherwise if the user tries to use 0 it will go to 2, which is wrong.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Qix- ⬆️

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@sindresorhus
Copy link
Owner

You need to update the docs too.

@sindresorhus
Copy link
Owner

Hmm, we don't really know what the user is going to use the method for though.

@Qix-
Copy link
Author

Qix- commented Aug 6, 2016

I would imagine that's an edge case. If anything we could add a third argument for an output stream to write to and have it default to process.stderr.

@sindresorhus
Copy link
Owner

@Qix- No, I meant that the user might use showHelp() in another scenario than misuse.

With your change here, it also exits with 2 when running $ foo --help. Is that really desirable? Both $ node --help and $ python --help exits with 0.

@Qix-
Copy link
Author

Qix- commented Aug 27, 2016

Right; 2 doesn't always mean misuse but it's used as a human indicator.

With --help, there's no convention there really. It should, in theory, error out since if --help is in a script (unlikely but possible) it's probably being misused and thus should halt further execution because the program did not execute in a 'successful' way.

@sindresorhus
Copy link
Owner

@Qix- What is "success" though? I would consider showing the help when I asked for it with --help success. Same with --version. Do you have any source on this? I'm genuinely curious. I always feel this is so unstandardized, tools do pretty much what they feel like.

@Qix-
Copy link
Author

Qix- commented Aug 28, 2016

Success means the main functionality of the utility has executed without error*. The main functionality in almost all cases isn't displaying a help message.

Put it this way:

my-program: main.o
    gcc -o $@ $<

main.o: main.c
    gcc -v -o $@ -c $<

Ignore the fact that GCC doesn't error on version, but this is something I've seen quite a few times. -v on gcc means version, not verbose (misused flag). In this case, the error message is going to be about main.o being missing since the latter gcc call completes with a 0 return code, telling the build system that it was successful, when in fact it only printed version information and returned.

Not only is that flow incorrect (the latter call was not well-formed and thus the linking step shouldn't execute at all) but it makes future error messages quite cryptic. Plus, if this was a huge tree there's the potential for a lot of re-compilation after fixing the error (albeit a bit rarer).

All of this can be applied to set -e as well.


*Also consider the fact a lot of this is semantics of the concept of "success" and thus is slightly subjective. 💃

@sindresorhus
Copy link
Owner

It seems like the problem here is using short flags, not just misuse. If you would instead use long flags, which are more readable, you wouldn't have this problem.

I tried out a lot of different binaries on my Mac and very few exits with other than 0 on --help and --version.

I agree it should exit with 2 on showHelp(), but I have yet to see enough evidence for doing it on --help.

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 this pull request may close these issues.

2 participants