rev-parse --parseopt: allow [*=?!] in argument hints

A line in the input to "rev-parse --parseopt" describes an option by listing a short and/or long name, optional flags [*=?!], argument hint, and then whitespace and help string. We did not allow any of the [*=?!] characters in the argument hints. The following input pair=key=value equals sign in the hint used to generate a help line like this: --pair=key <value> equals sign in the hint and used to expect "pair=key" as the argument name. That is not very helpful as we generally do not want any of the [*=?!] characters in the argument names. But we do want to use at least the equals sign in the argument hints. Update the parser to make long argument names stop at the first [*=?!] character. Add test case with equals sign in the argument hint and update the test to perform all the operations in test_expect_success matching the t/README requirements and allowing commands like ./t1502-rev-parse-parseopt.sh --run=1-2 to stop at the test case 2 without any further modification of the test state area. Signed-off-by: Ilya Bobyr <ilya.bobyr@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ilya Bobyr committed Jul 14, 2015 at 01:17 UTC 2d893dff4c3c49750557da7a1701b46c1fe95844
3 files changed +92 -68
Documentation/git-rev-parse.txt
+2 -2
@@ -311,8 +311,8 @@ Each line of options has this format:
311 `<opt-spec>`::
312 its format is the short option character, then the long option name
313 separated by a comma. Both parts are not required, though at least one
314 - is necessary. `h,help`, `dry-run` and `f` are all three correct
315 - `<opt-spec>`.
314 + is necessary. May not contain any of the `<flags>` characters.
315 + `h,help`, `dry-run` and `f` are examples of correct `<opt-spec>`.
316
317 `<flags>`::
318 `<flags>` are of `*`, `=`, `?` or `!`.
builtin/rev-parse.c
+30 -27
@@ -371,6 +371,7 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
371 N_("output in stuck long form")),
372 OPT_END(),
373 };
374 + static const char * const flag_chars = "*=?!";
375
376 struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT;
377 const char **usage = NULL;
@@ -400,7 +401,7 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
401 /* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
402 while (strbuf_getline(&sb, stdin, '\n') != EOF) {
403 const char *s;
403 - const char *end;
404 + const char *help;
405 struct option *o;
406
407 if (!sb.len)
@@ -410,54 +411,56 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
411 memset(opts + onb, 0, sizeof(opts[onb]));
412
413 o = &opts[onb++];
413 - s = strchr(sb.buf, ' ');
414 - if (!s || *sb.buf == ' ') {
414 + help = strchr(sb.buf, ' ');
415 + if (!help || *sb.buf == ' ') {
416 o->type = OPTION_GROUP;
417 o->help = xstrdup(skipspaces(sb.buf));
418 continue;
419 }
420
421 o->type = OPTION_CALLBACK;
421 - o->help = xstrdup(skipspaces(s));
422 + o->help = xstrdup(skipspaces(help));
423 o->value = &parsed;
424 o->flags = PARSE_OPT_NOARG;
425 o->callback = &parseopt_dump;
426
426 - /* Possible argument name hint */
427 - end = s;
428 - while (s > sb.buf && strchr("*=?!", s[-1]) == NULL)
429 - --s;
430 - if (s != sb.buf && s != end)
431 - o->argh = xmemdupz(s, end - s);
432 - if (s == sb.buf)
433 - s = end;
434 -
435 - while (s > sb.buf && strchr("*=?!", s[-1])) {
436 - switch (*--s) {
427 + /* name(s) */
428 + s = strpbrk(sb.buf, flag_chars);
429 + if (s == NULL)
430 + s = help;
431 +
432 + if (s - sb.buf == 1) /* short option only */
433 + o->short_name = *sb.buf;
434 + else if (sb.buf[1] != ',') /* long option only */
435 + o->long_name = xmemdupz(sb.buf, s - sb.buf);
436 + else {
437 + o->short_name = *sb.buf;
438 + o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
439 + }
440 +
441 + /* flags */
442 + while (s < help) {
443 + switch (*s++) {
444 case '=':
445 o->flags &= ~PARSE_OPT_NOARG;
439 - break;
446 + continue;
447 case '?':
448 o->flags &= ~PARSE_OPT_NOARG;
449 o->flags |= PARSE_OPT_OPTARG;
443 - break;
450 + continue;
451 case '!':
452 o->flags |= PARSE_OPT_NONEG;
446 - break;
453 + continue;
454 case '*':
455 o->flags |= PARSE_OPT_HIDDEN;
449 - break;
456 + continue;
457 }
458 + s--;
459 + break;
460 }
461
453 - if (s - sb.buf == 1) /* short option only */
454 - o->short_name = *sb.buf;
455 - else if (sb.buf[1] != ',') /* long option only */
456 - o->long_name = xmemdupz(sb.buf, s - sb.buf);
457 - else {
458 - o->short_name = *sb.buf;
459 - o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
460 - }
462 + if (s < help)
463 + o->argh = xmemdupz(s, help - s);
464 }
465 strbuf_release(&sb);
466
t/t1502-rev-parse-parseopt.sh
+60 -39
@@ -3,7 +3,40 @@
3 test_description='test git rev-parse --parseopt'
4 . ./test-lib.sh
5
6 -sed -e 's/^|//' >expect <<\END_EXPECT
6 +test_expect_success 'setup optionspec' '
7 + sed -e "s/^|//" >optionspec <<\EOF
8 +|some-command [options] <args>...
9 +|
10 +|some-command does foo and bar!
11 +|--
12 +|h,help show the help
13 +|
14 +|foo some nifty option --foo
15 +|bar= some cool option --bar with an argument
16 +|b,baz a short and long option
17 +|
18 +| An option group Header
19 +|C? option C with an optional argument
20 +|d,data? short and long option with an optional argument
21 +|
22 +| Argument hints
23 +|B=arg short option required argument
24 +|bar2=arg long option required argument
25 +|e,fuz=with-space short and long option required argument
26 +|s?some short option optional argument
27 +|long?data long option optional argument
28 +|g,fluf?path short and long option optional argument
29 +|longest=very-long-argument-hint a very long argument hint
30 +|pair=key=value with an equals sign in the hint
31 +|short-hint=a with a one symbol hint
32 +|
33 +|Extras
34 +|extra1 line above used to cause a segfault but no longer does
35 +EOF
36 +'
37 +
38 +test_expect_success 'test --parseopt help output' '
39 + sed -e "s/^|//" >expect <<\END_EXPECT &&
40 |cat <<\EOF
41 |usage: some-command [options] <args>...
42 |
@@ -28,49 +61,23 @@ sed -e 's/^|//' >expect <<\END_EXPECT
61 | -g, --fluf[=<path>] short and long option optional argument
62 | --longest <very-long-argument-hint>
63 | a very long argument hint
64 +| --pair <key=value> with an equals sign in the hint
65 +| --short-hint <a> with a one symbol hint
66 |
67 |Extras
68 | --extra1 line above used to cause a segfault but no longer does
69 |
70 |EOF
71 END_EXPECT
37 -
38 -sed -e 's/^|//' >optionspec <<\EOF
39 -|some-command [options] <args>...
40 -|
41 -|some-command does foo and bar!
42 -|--
43 -|h,help show the help
44 -|
45 -|foo some nifty option --foo
46 -|bar= some cool option --bar with an argument
47 -|b,baz a short and long option
48 -|
49 -| An option group Header
50 -|C? option C with an optional argument
51 -|d,data? short and long option with an optional argument
52 -|
53 -| Argument hints
54 -|B=arg short option required argument
55 -|bar2=arg long option required argument
56 -|e,fuz=with-space short and long option required argument
57 -|s?some short option optional argument
58 -|long?data long option optional argument
59 -|g,fluf?path short and long option optional argument
60 -|longest=very-long-argument-hint a very long argument hint
61 -|
62 -|Extras
63 -|extra1 line above used to cause a segfault but no longer does
64 -EOF
65 -
66 -test_expect_success 'test --parseopt help output' '
72 test_expect_code 129 git rev-parse --parseopt -- -h > output < optionspec &&
73 test_i18ncmp expect output
74 '
75
71 -cat > expect <<EOF
76 +test_expect_success 'setup expect.1' "
77 + cat > expect <<EOF
78 set -- --foo --bar 'ham' -b -- 'arg'
79 EOF
80 +"
81
82 test_expect_success 'test --parseopt' '
83 git rev-parse --parseopt -- --foo --bar=ham --baz arg < optionspec > output &&
@@ -82,9 +89,11 @@ test_expect_success 'test --parseopt with mixed options and arguments' '
89 test_cmp expect output
90 '
91
85 -cat > expect <<EOF
92 +test_expect_success 'setup expect.2' "
93 + cat > expect <<EOF
94 set -- --foo -- 'arg' '--bar=ham'
95 EOF
96 +"
97
98 test_expect_success 'test --parseopt with --' '
99 git rev-parse --parseopt -- --foo -- arg --bar=ham < optionspec > output &&
@@ -96,54 +105,66 @@ test_expect_success 'test --parseopt --stop-at-non-option' '
105 test_cmp expect output
106 '
107
99 -cat > expect <<EOF
108 +test_expect_success 'setup expect.3' "
109 + cat > expect <<EOF
110 set -- --foo -- '--' 'arg' '--bar=ham'
111 EOF
112 +"
113
114 test_expect_success 'test --parseopt --keep-dashdash' '
115 git rev-parse --parseopt --keep-dashdash -- --foo -- arg --bar=ham < optionspec > output &&
116 test_cmp expect output
117 '
118
108 -cat >expect <<EOF
119 +test_expect_success 'setup expect.4' "
120 + cat >expect <<EOF
121 set -- --foo -- '--' 'arg' '--spam=ham'
122 EOF
123 +"
124
125 test_expect_success 'test --parseopt --keep-dashdash --stop-at-non-option with --' '
126 git rev-parse --parseopt --keep-dashdash --stop-at-non-option -- --foo -- arg --spam=ham <optionspec >output &&
127 test_cmp expect output
128 '
129
117 -cat > expect <<EOF
130 +test_expect_success 'setup expect.5' "
131 + cat > expect <<EOF
132 set -- --foo -- 'arg' '--spam=ham'
133 EOF
134 +"
135
136 test_expect_success 'test --parseopt --keep-dashdash --stop-at-non-option without --' '
137 git rev-parse --parseopt --keep-dashdash --stop-at-non-option -- --foo arg --spam=ham <optionspec >output &&
138 test_cmp expect output
139 '
140
126 -cat > expect <<EOF
141 +test_expect_success 'setup expect.6' "
142 + cat > expect <<EOF
143 set -- --foo --bar='z' --baz -C'Z' --data='A' -- 'arg'
144 EOF
145 +"
146
147 test_expect_success 'test --parseopt --stuck-long' '
148 git rev-parse --parseopt --stuck-long -- --foo --bar=z -b arg -CZ -dA <optionspec >output &&
149 test_cmp expect output
150 '
151
135 -cat > expect <<EOF
152 +test_expect_success 'setup expect.7' "
153 + cat > expect <<EOF
154 set -- --data='' -C --baz -- 'arg'
155 EOF
156 +"
157
158 test_expect_success 'test --parseopt --stuck-long and empty optional argument' '
159 git rev-parse --parseopt --stuck-long -- --data= arg -C -b <optionspec >output &&
160 test_cmp expect output
161 '
162
144 -cat > expect <<EOF
163 +test_expect_success 'setup expect.8' "
164 + cat > expect <<EOF
165 set -- --data --baz -- 'arg'
166 EOF
167 +"
168
169 test_expect_success 'test --parseopt --stuck-long and long option with unset optional argument' '
170 git rev-parse --parseopt --stuck-long -- --data arg -b <optionspec >output &&