From: "brian m. carlson" <sandals@crustytoothpaste.net>
To: <git@vger.kernel.org>
Cc: Junio C Hamano <gitster@pobox.com>, Jeff King <peff@peff.net>
Subject: [PATCH v2 2/4] parse-options: add a separate case for help output on error
Date: Wed, 1 Jul 2026 21:24:40 +0000 [thread overview]
Message-ID: <20260701212442.1430084-3-sandals@crustytoothpaste.net> (raw)
In-Reply-To: <20260701212442.1430084-1-sandals@crustytoothpaste.net>
When we parse a command line option such as -h or --help, we currently
exit 129, since that is the exit code when help output is printed. In a
future commit, we'll change this to exit 0 instead, since we're doing
what the user wanted successfully.
However, there are some cases where we print help output because the
user has provided ambiguous or invalid input, such as an ambiguous
option, and we'll want to exit unsuccessfully there. Make this easier
by defining a new return code, PARSE_OPT_HELP_ERROR, that can be used in
this case, while reserving PARSE_OPT_HELP for those cases where the user
has requested help directly.
Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
---
builtin/blame.c | 1 +
builtin/shortlog.c | 1 +
builtin/update-index.c | 1 +
parse-options.c | 7 ++++++-
parse-options.h | 3 ++-
5 files changed, 11 insertions(+), 2 deletions(-)
diff --git a/builtin/blame.c b/builtin/blame.c
index ffbd3ce5c5..65d43c7d48 100644
--- a/builtin/blame.c
+++ b/builtin/blame.c
@@ -1013,6 +1013,7 @@ int cmd_blame(int argc,
case PARSE_OPT_UNKNOWN:
break;
case PARSE_OPT_HELP:
+ case PARSE_OPT_HELP_ERROR:
case PARSE_OPT_ERROR:
case PARSE_OPT_SUBCOMMAND:
exit(129);
diff --git a/builtin/shortlog.c b/builtin/shortlog.c
index 6b2a0b93b5..cd262bd376 100644
--- a/builtin/shortlog.c
+++ b/builtin/shortlog.c
@@ -433,6 +433,7 @@ int cmd_shortlog(int argc,
case PARSE_OPT_UNKNOWN:
break;
case PARSE_OPT_HELP:
+ case PARSE_OPT_HELP_ERROR:
case PARSE_OPT_ERROR:
case PARSE_OPT_SUBCOMMAND:
exit(129);
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 3d6646c318..ac4610ec94 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -1133,6 +1133,7 @@ int cmd_update_index(int argc,
break;
switch (parseopt_state) {
case PARSE_OPT_HELP:
+ case PARSE_OPT_HELP_ERROR:
case PARSE_OPT_ERROR:
exit(129);
case PARSE_OPT_COMPLETE:
diff --git a/parse-options.c b/parse-options.c
index f4647e0099..a623961800 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -583,7 +583,7 @@ static enum parse_opt_result parse_long_opt(
ambiguous.option->long_name,
(abbrev.flags & OPT_UNSET) ? "no-" : "",
abbrev.option->long_name);
- return PARSE_OPT_HELP;
+ return PARSE_OPT_HELP_ERROR;
}
if (abbrev.option) {
if (*arg_end)
@@ -1037,6 +1037,7 @@ enum parse_opt_result parse_options_step(struct parse_opt_ctx_t *ctx,
usage_with_options(usagestr, options);
case PARSE_OPT_COMPLETE:
case PARSE_OPT_HELP:
+ case PARSE_OPT_HELP_ERROR:
case PARSE_OPT_ERROR:
case PARSE_OPT_DONE:
case PARSE_OPT_NON_OPTION:
@@ -1072,6 +1073,7 @@ enum parse_opt_result parse_options_step(struct parse_opt_ctx_t *ctx,
case PARSE_OPT_NON_OPTION:
case PARSE_OPT_SUBCOMMAND:
case PARSE_OPT_HELP:
+ case PARSE_OPT_HELP_ERROR:
case PARSE_OPT_COMPLETE:
BUG("parse_short_opt() cannot return these");
case PARSE_OPT_DONE:
@@ -1099,6 +1101,7 @@ enum parse_opt_result parse_options_step(struct parse_opt_ctx_t *ctx,
case PARSE_OPT_SUBCOMMAND:
case PARSE_OPT_COMPLETE:
case PARSE_OPT_HELP:
+ case PARSE_OPT_HELP_ERROR:
BUG("parse_short_opt() cannot return these");
case PARSE_OPT_DONE:
break;
@@ -1132,6 +1135,7 @@ enum parse_opt_result parse_options_step(struct parse_opt_ctx_t *ctx,
case PARSE_OPT_UNKNOWN:
goto unknown;
case PARSE_OPT_HELP:
+ case PARSE_OPT_HELP_ERROR:
goto show_usage;
case PARSE_OPT_NON_OPTION:
case PARSE_OPT_SUBCOMMAND:
@@ -1197,6 +1201,7 @@ int parse_options(int argc, const char **argv,
parse_options_start_1(&ctx, argc, argv, prefix, options, flags);
switch (parse_options_step(&ctx, options, usagestr)) {
case PARSE_OPT_HELP:
+ case PARSE_OPT_HELP_ERROR:
case PARSE_OPT_ERROR:
exit(129);
case PARSE_OPT_COMPLETE:
diff --git a/parse-options.h b/parse-options.h
index 0d1f738f8d..3ec8ba5cc8 100644
--- a/parse-options.h
+++ b/parse-options.h
@@ -57,7 +57,8 @@ enum parse_opt_option_flags {
};
enum parse_opt_result {
- PARSE_OPT_COMPLETE = -3,
+ PARSE_OPT_COMPLETE = -4,
+ PARSE_OPT_HELP_ERROR = -3,
PARSE_OPT_HELP = -2,
PARSE_OPT_ERROR = -1, /* must be the same as error() */
PARSE_OPT_DONE = 0, /* fixed so that "return 0" works */
next prev parent reply other threads:[~2026-07-01 21:24 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-15 0:52 Unexpected exit code for --help with rev-parse --parseopt brian m. carlson
2026-03-15 3:14 ` 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
2026-07-01 21:24 ` [PATCH v2 0/4] rev-parse: " brian m. carlson
2026-07-01 21:24 ` [PATCH v2 1/4] t1517: skip svn tests if svn is not installed brian m. carlson
2026-07-01 22:10 ` Junio C Hamano
2026-07-01 22:27 ` Junio C Hamano
2026-07-02 14:47 ` [PATCH v2 1/4] t1517: skip svn tests if svn is not installed2sy brian m. carlson
2026-07-02 5:37 ` [PATCH v2 1/4] t1517: skip svn tests if svn is not installed Jeff King
2026-07-03 20:36 ` Junio C Hamano
2026-07-04 4:47 ` Jeff King
2026-07-01 21:24 ` brian m. carlson [this message]
2026-07-02 8:38 ` [PATCH v2 2/4] parse-options: add a separate case for help output on error Jeff King
2026-07-01 21:24 ` [PATCH v2 3/4] rev-parse: have --parseopt callers exit 0 on --help brian m. carlson
2026-07-01 22:16 ` Junio C Hamano
2026-07-01 21:24 ` [PATCH v2 4/4] parse-options: exit 0 on -h brian m. carlson
2026-07-01 22:06 ` [PATCH v2 0/4] rev-parse: exit 0 on --help Junio C Hamano
2026-07-02 8:45 ` Jeff King
2026-07-03 20:38 ` 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=20260701212442.1430084-3-sandals@crustytoothpaste.net \
--to=sandals@crustytoothpaste.net \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=peff@peff.net \
/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