From: Jeff King Date: Sun, 15 Mar 2026 03:14:47 GMT Subject: Re: Unexpected exit code for --help with rev-parse --parseopt Message-ID: <20260315031447.GB926820@coredump.intra.peff.net> In-Reply-To: On Sun, Mar 15, 2026 at 12:52:22AM +0000, brian m. carlson wrote: > I use git rev-parse --parseopt to parse command-line options in various > script. I've noticed that it exits 129 if the options are invalid, > which is fine, but that it also exits 129 if --help or -h are given, > which is not. > > The standard philosophy is that if the user explicitly asked for help, > then help output should be printed to standard output (since that's what > the user asked for) and it should exit 0, since the program fulfilled > the user's request successfully. If the help output is provided because > the user provided an invalid option or argument, then the output should > Go to standard error (since it's an error message) and the program > should exit unsuccessfully (since it did not fulfill the user's request > successfully). I agree with this philosophy in general, but there's catch here with "rev-parse --parseopt", because it's running as a separate program. It needs to signal to the calling program that "-h" was seen, the program should not proceed normally (because rev-parse already dumped help output). So if your proposal is to just exit with code 0 from rev-parse, I don't think that works. My first instinct is that it would have to exit with some other well-known code, the callers would recognize that, and then exit themselves (with code 0). But there's a little more to the story. The output of --parseopt is meant to be eval'd by the shell. And so when it works, we get a "set" command: $ git rev-parse --parseopt ] ... Do stuff. -h, --help show this help -o, --[no-]output ... output to this file -d, --[no-]dir ... specify dependencies relative to this directory But when the user asks for "-h", we get a here-doc! Like this: $ git rev-parse --parseopt ] ... Do stuff. -h, --help show this help -o, --[no-]output ... output to this file -d, --[no-]dir ... specify dependencies relative to this directory EOF So the calling program runs that cat command. And I think what you really want is to tack "exit 0" onto the end of that output, which would tell the callers to exit. Something like this: diff --git a/parse-options.c b/parse-options.c index a676da86f5..b990f38419 100644 --- a/parse-options.c +++ b/parse-options.c @@ -1473,8 +1473,10 @@ static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t } fputc('\n', outfile); - if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL) + if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL) { fputs("EOF\n", outfile); + fputs("exit 0\n", outfile); + } return PARSE_OPT_HELP; } And then you don't even need to change the exit code of rev-parse itself (since we'd never hit the "exit $?" that the caller tacks on in case of failure). Though I think it might be reasonable to switch it to 0 anyway. -Peff