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

From: "brian m. carlson" <[email-masked]>
Date: Sun, 15 Mar 2026 00:52:22 +0000

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).

Git gets the location of the output just fine, but currently there's no
way for a program to detect the help output and exit successfully.  Note
that Git subcommands don't have this problem because Git itself
intercepts the help output and sends it to man (or whatever the user has
configured), which then exits successfully.

I'd like to fix this in git rev-parse --parseopt (that is, I am willing
to send a patch), but I'm unsure about the best way to go about this.
Does anyone have ideas about how they'd like this to be fixed?  I could
simply change the exit code in this case or I could add an option to
control this behaviour for backwards compatibility.

Example:

----
#!/bin/sh

OPTS_SPEC="\
foo-cmd [<options>] <args>...

Do stuff.
--
h,help!         show this help

o,output=       output to this file
d,dir=          specify dependencies relative to this directory
"

main () {
    eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"

    # Do something with the options here.
}

main "$@"
----

Output:

----
% ./foo-cmd --help >/dev/null; echo $?
129
% ./foo-cmd -h >/dev/null; echo $?
129
% ./foo-cmd --bassoon >/dev/null; echo $?
error: unknown option `bassoon'
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

129
----
-- 
brian m. carlson (they/them)
Toronto, Ontario, CA