From: brian m. carlson Date: Wed, 08 Jul 2026 00:15:53 GMT Subject: [PATCH v3 0/4] parseopt: exit 0 on help Message-ID: <20260708001557.3581080-1-sandals@crustytoothpaste.net> In-Reply-To: <20260701212442.1430084-1-sandals@crustytoothpaste.net> The standard philosophy for Unix software when a help option (such as --help) is specified is that the software should exit 0, printing the help output to standard output, since the standard output is for user-requested output and the program performed the requested task successfully. If the user specifies an incorrect option, then the help output should be printed to standard error (since the user has made a mistake) and it should exit unsuccessfully. git rev-parse --parseopt properly directs the output in both of these cases, but it currently exits 129 when it receives a --help or -h option on the command line, which causes its invoking script to do the same. This is not in line with the usual behavior and it causes scripts using this command to exit unsuccessfully on --help as well. This series introduces some changes to distinguish the --help and -h options from other cases in which we print help output and adjusts the exit code to 0 from those two options. We continue to exit 129 when the options are invalid, which is useful information to have for callers. We also make the relevant changes such that `git rev-parse --parseopt` does the same thing as long as it is invoked in the way specified in the manual page (which a quick GitHub search shows almost everyone does). Changes since v2: * Fix inverted condition in t1517. * Stop checking for old versions of SVN Perl libraries since they are so old nobody is using them. * Adjust the various cases where we choose between the error and non-error help output. brian m. carlson (4): t1517: skip svn tests if svn is not installed parse-options: add a separate case for help output on error rev-parse: have --parseopt callers exit 0 on --help parse-options: exit 0 on -h builtin/blame.c | 2 ++ builtin/shortlog.c | 2 ++ builtin/update-index.c | 2 ++ contrib/subtree/t/t7900-subtree.sh | 2 +- parse-options.c | 22 +++++++++++----- parse-options.h | 3 ++- t/for-each-ref-tests.sh | 2 +- t/t0012-help.sh | 2 +- t/t0040-parse-options.sh | 2 +- t/t0450-txt-doc-vs-help.sh | 2 +- t/t0610-reftable-basics.sh | 4 +-- t/t1403-show-ref.sh | 2 +- t/t1410-reflog.sh | 4 +-- t/t1418-reflog-exists.sh | 2 +- t/t1502-rev-parse-parseopt.sh | 23 +++++++++------- t/t1502/optionspec-neg.help | 1 + t/t1502/optionspec.help | 1 + t/t1517-outside-repo.sh | 42 +++++++++++++++++++++--------- t/t1800-hook.sh | 4 +-- t/t1900-repo-info.sh | 2 +- t/t1901-repo-structure.sh | 2 +- t/t2006-checkout-index-basic.sh | 6 ++--- t/t2107-update-index-basic.sh | 2 +- t/t3004-ls-files-basic.sh | 6 ++--- t/t3200-branch.sh | 2 +- t/t3903-stash.sh | 4 +-- t/t4200-rerere.sh | 2 +- t/t5200-update-server-info.sh | 2 +- t/t5304-prune.sh | 2 +- t/t5400-send-pack.sh | 4 +-- t/t5512-ls-remote.sh | 2 +- t/t6300-for-each-ref.sh | 4 +-- t/t6500-gc.sh | 2 +- t/t7030-verify-tag.sh | 4 +-- t/t7508-status.sh | 4 +-- t/t7510-signed-commit.sh | 4 +-- t/t7600-merge.sh | 2 +- t/t7800-difftool.sh | 3 +-- t/t7900-maintenance.sh | 2 +- usage.c | 2 +- 40 files changed, 113 insertions(+), 74 deletions(-) Range-diff against v2: 1: 558a53cc20 ! 1: c8c7eac5f7 t1517: skip svn tests if svn is not installed @@ t/t1517-outside-repo.sh: test_description='check random commands outside repo' . ./test-lib.sh +test_lazy_prereq SVN ' -+ test_have_prereq PERL && test -n "$NO_SVN_TESTS" && perl -w -e " ++ test_have_prereq PERL && test -z "$NO_SVN_TESTS" && perl -w -e " + use SVN::Core; + use SVN::Repos; -+ \$SVN::Core::VERSION gt '1.1.0' or exit(42); + " +' + 2: 2b5ce2fb4c ! 2: daa7aa2534 parse-options: add a separate case for help output on error @@ parse-options.c: int parse_options(int argc, const char **argv, case PARSE_OPT_ERROR: exit(129); case PARSE_OPT_COMPLETE: +@@ parse-options.c: static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t + parse_options_check_harder(opts); + + if (!usagestr) +- return PARSE_OPT_HELP; ++ return err ? PARSE_OPT_HELP_ERROR : PARSE_OPT_HELP; + + if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL) + fprintf(outfile, "cat <<\\EOF\n"); +@@ parse-options.c: static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t + if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL) + fputs("EOF\n", outfile); + +- return PARSE_OPT_HELP; ++ return err ? PARSE_OPT_HELP_ERROR : PARSE_OPT_HELP; + } + + void NORETURN usage_with_options(const char * const *usagestr, ## parse-options.h ## @@ parse-options.h: enum parse_opt_option_flags { 3: e5d0167544 ! 3: af69daffc3 rev-parse: have --parseopt callers exit 0 on --help @@ parse-options.c: static enum parse_opt_result usage_with_options_internal(struct - fputs("EOF\n", outfile); + fputs("EOF\nexit 0\n", outfile); - return PARSE_OPT_HELP; + return err ? PARSE_OPT_HELP_ERROR : PARSE_OPT_HELP; } ## t/t1502-rev-parse-parseopt.sh ## 4: 98481005ff ! 4: f68c53015c parse-options: exit 0 on -h @@ parse-options.c: int parse_options(int argc, const char **argv, case PARSE_OPT_HELP_ERROR: case PARSE_OPT_ERROR: exit(129); -@@ parse-options.c: static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t - if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL) - fputs("EOF\nexit 0\n", outfile); - -- return PARSE_OPT_HELP; -+ return err ? PARSE_OPT_HELP_ERROR : PARSE_OPT_HELP; - } - - void NORETURN usage_with_options(const char * const *usagestr, @@ parse-options.c: void show_usage_with_options_if_asked(int ac, const char **av, if (!strcmp(av[1], "-h")) { usage_with_options_internal(NULL, usagestr, opts,