public inbox for git@vger.kernel.org
 help / color / mirror / Atom feed
From: "brian m. carlson" <sandals@crustytoothpaste.net>
To: git@vger.kernel.org
Subject: Unexpected exit code for --help with rev-parse --parseopt
Date: Sun, 15 Mar 2026 00:52:22 +0000	[thread overview]
Message-ID: <abYCxrEEPaI21g3H@fruit.crustytoothpaste.net> (raw)

[-- Attachment #1: Type: text/plain, Size: 2295 bytes --]

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

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 262 bytes --]

             reply	other threads:[~2026-03-15  0:52 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-15  0:52 brian m. carlson [this message]
2026-03-15  3:14 ` Unexpected exit code for --help with rev-parse --parseopt Jeff King
2026-03-15 16:59   ` brian m. carlson
2026-03-15 18:16     ` Jeff King
2026-03-16 22:07 ` [PATCH] rev-parse: have --parseopt callers exit 0 on --help brian m. carlson
2026-03-17  0:47   ` Junio C Hamano
2026-03-17 11:59     ` brian m. carlson
2026-03-17 14:55       ` Jeff King
2026-03-17 15:07         ` Jeff King
2026-03-17 17:06         ` Junio C Hamano
2026-03-17 18:44           ` Jeff King
2026-03-18  0:24             ` brian m. carlson
2026-03-18  1:22               ` Jeff King
2026-03-18  2:45                 ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=abYCxrEEPaI21g3H@fruit.crustytoothpaste.net \
    --to=sandals@crustytoothpaste.net \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox