test-parse-options: fix output when callback option fails

When test-parse-options detects an error on the command line, it gives the usage string just like any parse-options API users do, without showing any "variable dump". An exception is the callback test, where a "variable dump" for the option is done before the command line options are fully parsed. Do not expose this implementation detail by separating the handling of callback test into two phases, one to capture the fact that an option was given during the option parsing phase, and the other to show that fact as a part of normal "variable dump". The effect of this fix is seen in the patch to t/t0040 where it tried "test-parse-options --no-length" where "--length" is a callback that does not take a negative form. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed May 5, 2016 at 13:30 UTC accac4199c1d28dfd6c860b32d7111c3de8df7a6
2 files changed +17 -5
t/t0040-parse-options.sh
+1 -3
@@ -356,9 +356,7 @@ test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '
356 test_cmp expect output
357 '
358
359 -cat >expect <<\EOF
360 -Callback: "not set", 1
361 -EOF
359 +>expect
360
361 test_expect_success 'OPT_CALLBACK() and callback errors work' '
362 test_must_fail test-parse-options --no-length >output 2>output.err &&
test-parse-options.c
+16 -2
@@ -14,10 +14,18 @@ static char *file = NULL;
14 static int ambiguous;
15 static struct string_list list;
16
17 +static struct {
18 + int called;
19 + const char *arg;
20 + int unset;
21 +} length_cb;
22 +
23 static int length_callback(const struct option *opt, const char *arg, int unset)
24 {
19 - printf("Callback: \"%s\", %d\n",
20 - (arg ? arg : "not set"), unset);
25 + length_cb.called = 1;
26 + length_cb.arg = arg;
27 + length_cb.unset = unset;
28 +
29 if (unset)
30 return 1; /* do not support unset */
31
@@ -84,6 +92,12 @@ int main(int argc, char **argv)
92
93 argc = parse_options(argc, (const char **)argv, prefix, options, usage, 0);
94
95 + if (length_cb.called) {
96 + const char *arg = length_cb.arg;
97 + int unset = length_cb.unset;
98 + printf("Callback: \"%s\", %d\n",
99 + (arg ? arg : "not set"), unset);
100 + }
101 printf("boolean: %d\n", boolean);
102 printf("integer: %d\n", integer);
103 printf("magnitude: %lu\n", magnitude);