git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Unexpected exit code for --help with rev-parse --parseopt

From
Jeff King <peff@peff.net>
Date
Mar 15, 2026, 03:14 UTC
Message-ID
<20260315031447.GB926820@coredump.intra.peff.net>
In-Reply-To
<abYCxrEEPaI21g3H@fruit.crustytoothpaste.net>
On Sun, Mar 15, 2026 at 12:52:22AM +0000, brian m. carlson wrote:
Show 13 quoted lines
> 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 <input -- --output=foo
  set -- -o 'foo' --

And when there's an error, it dumps the usage text straight to stderr and exits 129:

  $ git rev-parse --parseopt <input -- --foo
  error: unknown option `foo'
  usage: foo-cmd [<options>] <args>...
  
      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 <input -- -h
  cat <<\EOF
  usage: foo-cmd [<options>] <args>...
  
      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
Previous: brian m. carlsonNext: brian m. carlson
Message 2 of 39 in “Unexpected exit code for --help with rev-parse --parseopt”
  1. brian m. carlsonMar 15, 2026
  2. Jeff KingMar 15, 2026
  3. brian m. carlsonMar 15, 2026
  4. Jeff KingMar 15, 2026
  5. rev-parse: have --parseopt callers exit 0 on --helpbrian m. carlson, Mar 16, 2026
  6. Junio C HamanoMar 17, 2026
  7. brian m. carlsonMar 17, 2026
  8. Jeff KingMar 17, 2026
  9. Jeff KingMar 17, 2026
  10. Junio C HamanoMar 17, 2026
  11. Jeff KingMar 17, 2026
  12. brian m. carlsonMar 18, 2026
  13. Jeff KingMar 18, 2026
  14. Junio C HamanoMar 18, 2026
  15. 0/4 rev-parse: exit 0 on --helpbrian m. carlson, Jul 1, 2026
  16. 3/4 rev-parse: have --parseopt callers exit 0 on --helpbrian m. carlson, Jul 1, 2026
  17. Junio C HamanoJul 1, 2026
  18. 1/4 t1517: skip svn tests if svn is not installedbrian m. carlson, Jul 1, 2026
  19. Junio C HamanoJul 1, 2026
  20. Junio C HamanoJul 1, 2026
  21. brian m. carlsonJul 2, 2026
  22. Jeff KingJul 2, 2026
  23. Junio C HamanoJul 3, 2026
  24. Jeff KingJul 4, 2026
  25. 4/4 parse-options: exit 0 on -hbrian m. carlson, Jul 1, 2026
  26. 2/4 parse-options: add a separate case for help output on errorbrian m. carlson, Jul 1, 2026
  27. Jeff KingJul 2, 2026
  28. Junio C HamanoJul 1, 2026
  29. Jeff KingJul 2, 2026
  30. Junio C HamanoJul 3, 2026
  31. 0/4 parseopt: exit 0 on helpbrian m. carlson, Jul 8, 2026
  32. 1/4 t1517: skip svn tests if svn is not installedbrian m. carlson, Jul 8, 2026
  33. Junio C HamanoJul 8, 2026
  34. 3/4 rev-parse: have --parseopt callers exit 0 on --helpbrian m. carlson, Jul 8, 2026
  35. 2/4 parse-options: add a separate case for help output on errorbrian m. carlson, Jul 8, 2026
  36. Junio C HamanoJul 8, 2026
  37. 4/4 parse-options: exit 0 on -hbrian m. carlson, Jul 8, 2026
  38. Jeff KingJul 8, 2026
  39. Junio C HamanoJul 8, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.