Skip to content

Commit

Permalink
Merge branch 'ib/rev-parse-parseopt-argh'
Browse files Browse the repository at this point in the history
Teaches the "rev-parse --parseopt" mechanism used by scripted
Porcelains to parse command line options and give help text how to
supply argv-help (the placeholder string for an option parameter,
e.g. "key-id" in "--gpg-sign=<key-id>").

* ib/rev-parse-parseopt-argh:
  t1502: protect runs of SPs used in the indentation
  rev-parse --parseopt: option argument name hints
  • Loading branch information
Junio C Hamano committed Mar 31, 2014
2 parents a79cbc1 + ce7f874 commit 24b9cb1
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 39 deletions.
34 changes: 32 additions & 2 deletions Documentation/git-rev-parse.txt
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,13 @@ Input Format

'git rev-parse --parseopt' input format is fully text based. It has two parts,
separated by a line that contains only `--`. The lines before the separator
(should be more than one) are used for the usage.
(should be one or more) are used for the usage.
The lines after the separator describe the options.

Each line of options has this format:

------------
<opt_spec><flags>* SP+ help LF
<opt_spec><flags>*<arg_hint>? SP+ help LF
------------

`<opt_spec>`::
Expand All @@ -313,6 +313,12 @@ Each line of options has this format:

* Use `!` to not make the corresponding negated long option available.

`<arg_hint>`::
`<arg_hint>`, if specified, is used as a name of the argument in the
help output, for options that take arguments. `<arg_hint>` is
terminated by the first whitespace. When you need to use space in the
argument hint use dash instead.

The remainder of the line, after stripping the spaces, is used
as the help associated to the option.

Expand All @@ -333,13 +339,37 @@ h,help show the help

foo some nifty option --foo
bar= some cool option --bar with an argument
baz=arg another cool option --baz with a named argument
qux?path qux may take a path argument but has meaning by itself

An option group Header
C? option C with an optional argument"

eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
------------


Usage text
~~~~~~~~~~

When `"$@"` is `-h` or `--help` in the above example, the following
usage text would be shown:

------------
usage: some-command [options] <args>...

some-command does foo and bar!

-h, --help show the help
--foo some nifty option --foo
--bar ... some cool option --bar with an argument
--bar <arg> another cool option --baz with a named argument
--qux[=<path>] qux may take a path argument but has meaning by itself

An option group Header
-C[...] option C with an optional argument
------------

SQ-QUOTE
--------

Expand Down
13 changes: 12 additions & 1 deletion builtin/rev-parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,10 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
usage[unb++] = strbuf_detach(&sb, NULL);
}

/* parse: (<short>|<short>,<long>|<long>)[=?]? SP+ <help> */
/* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
while (strbuf_getline(&sb, stdin, '\n') != EOF) {
const char *s;
const char *end;
struct option *o;

if (!sb.len)
Expand All @@ -419,6 +420,16 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
o->value = &parsed;
o->flags = PARSE_OPT_NOARG;
o->callback = &parseopt_dump;

/* Possible argument name hint */
end = s;
while (s > sb.buf && strchr("*=?!", s[-1]) == NULL)
--s;
if (s != sb.buf && s != end)
o->argh = xmemdupz(s, end - s);
if (s == sb.buf)
s = end;

while (s > sb.buf && strchr("*=?!", s[-1])) {
switch (*--s) {
case '=':
Expand Down
92 changes: 56 additions & 36 deletions t/t1502-rev-parse-parseopt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,64 @@
test_description='test git rev-parse --parseopt'
. ./test-lib.sh

cat > expect <<\END_EXPECT
cat <<\EOF
usage: some-command [options] <args>...
some-command does foo and bar!
-h, --help show the help
--foo some nifty option --foo
--bar ... some cool option --bar with an argument
-b, --baz a short and long option
An option group Header
-C[...] option C with an optional argument
-d, --data[=...] short and long option with an optional argument
Extras
--extra1 line above used to cause a segfault but no longer does
EOF
sed -e 's/^|//' >expect <<\END_EXPECT
|cat <<\EOF
|usage: some-command [options] <args>...
|
| some-command does foo and bar!
|
| -h, --help show the help
| --foo some nifty option --foo
| --bar ... some cool option --bar with an argument
| -b, --baz a short and long option
|
|An option group Header
| -C[...] option C with an optional argument
| -d, --data[=...] short and long option with an optional argument
|
|Argument hints
| -b <arg> short option required argument
| --bar2 <arg> long option required argument
| -e, --fuz <with-space>
| short and long option required argument
| -s[<some>] short option optional argument
| --long[=<data>] long option optional argument
| -g, --fluf[=<path>] short and long option optional argument
| --longest <very-long-argument-hint>
| a very long argument hint
|
|Extras
| --extra1 line above used to cause a segfault but no longer does
|
|EOF
END_EXPECT

cat > optionspec << EOF
some-command [options] <args>...
some-command does foo and bar!
--
h,help show the help
foo some nifty option --foo
bar= some cool option --bar with an argument
b,baz a short and long option
An option group Header
C? option C with an optional argument
d,data? short and long option with an optional argument
Extras
extra1 line above used to cause a segfault but no longer does
sed -e 's/^|//' >optionspec <<\EOF
|some-command [options] <args>...
|
|some-command does foo and bar!
|--
|h,help show the help
|
|foo some nifty option --foo
|bar= some cool option --bar with an argument
|b,baz a short and long option
|
| An option group Header
|C? option C with an optional argument
|d,data? short and long option with an optional argument
|
| Argument hints
|b=arg short option required argument
|bar2=arg long option required argument
|e,fuz=with-space short and long option required argument
|s?some short option optional argument
|long?data long option optional argument
|g,fluf?path short and long option optional argument
|longest=very-long-argument-hint a very long argument hint
|
|Extras
|extra1 line above used to cause a segfault but no longer does
EOF

test_expect_success 'test --parseopt help output' '
Expand Down

0 comments on commit 24b9cb1

Please sign in to comment.