mirror of
https://github.com/git/git.git
synced 2026-11-01 22:11:38 +01:00
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.
Note that Git subcommands implemented using scripts, such as git
submodule, don't have this problem because Git itself intercepts the
--help option and runs man (or a similar tool), which then exits 0.
However, this still affects the myriad scripts that use this
functionality because Git is widespread and the --parseopt functionality
is a good way to get sensible option parsing across shells in a portable
way.
Because git rev-parse --parseopt is intended to be eval'd by the shell,
when help output is to be printed to standard output, Git actually
prints a cat command with a heredoc since the standard output is being
evaluated by the shell. Thus, to do the right thing, simply add an
"exit 0" right after the end of the heredoc, which will cause the
invoking program to exit successfully.
The usual invocation recommended by the manual page is this:
eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
Thus, the fact that git rev-parse --parseopt still exits 129 in this
case is irrelevant, since the "echo exit $?" will print "exit 129", but
that will be after the "exit 0" printed by Git—and thus ignored, since
the shell will have already exited successfully.
Update the tests for this case. Note that we no longer need to delete
only the first and last lines in some tests, so add a command to delete
the end of the heredoc as well. We could do something clever with sed
to delete all but the last two lines or switch to head and tail, but
those would be more complicated and less readable, so just stick with
the simple approach.
In t1517, add three shell scripts to the failure case because they no
longer return 129 as expected. In a future commit, we'll change the
expected result to exit 0 and these will become successful again.
Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
38 lines
1.4 KiB
Plaintext
Executable File
38 lines
1.4 KiB
Plaintext
Executable File
cat <<\EOF
|
|
usage: some-command [options] <args>...
|
|
|
|
some-command does foo and bar!
|
|
|
|
-h, --help show the help
|
|
--[no-]foo some nifty option --foo
|
|
--[no-]bar ... some cool option --bar with an argument
|
|
-b, --[no-]baz a short and long option
|
|
|
|
An option group Header
|
|
-C[...] option C with an optional argument
|
|
-d, --[no-]data[=...] short and long option with an optional argument
|
|
|
|
Argument hints
|
|
-B <arg> short option required argument
|
|
--[no-]bar2 <arg> long option required argument
|
|
-e, --[no-]fuz <with-space>
|
|
short and long option required argument
|
|
-s[<some>] short option optional argument
|
|
--[no-]long[=<data>] long option optional argument
|
|
-g, --[no-]fluf[=<path>]
|
|
short and long option optional argument
|
|
--[no-]longest <very-long-argument-hint>
|
|
a very long argument hint
|
|
--[no-]pair <key=value>
|
|
with an equals sign in the hint
|
|
--[no-]aswitch help te=t contains? fl*g characters!`
|
|
--[no-]bswitch <hint> hint has trailing tab character
|
|
--[no-]cswitch switch has trailing tab character
|
|
--[no-]short-hint <a> with a one symbol hint
|
|
|
|
Extras
|
|
--[no-]extra1 line above used to cause a segfault but no longer does
|
|
|
|
EOF
|
|
exit 0
|