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

[PATCH v3 0/4] parseopt: exit 0 on help

From
brian m. carlson <sandals@crustytoothpaste.net>
Date
Jul 8, 2026, 00:15 UTC
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,
Previous: Junio C HamanoNext: brian m. carlson
Message 31 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.