Skip to content

Commit accac41

Browse files
committed
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 <[email protected]>
1 parent aaab842 commit accac41

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

t/t0040-parse-options.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,7 @@ test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '
356356
test_cmp expect output
357357
'
358358

359-
cat >expect <<\EOF
360-
Callback: "not set", 1
361-
EOF
359+
>expect
362360

363361
test_expect_success 'OPT_CALLBACK() and callback errors work' '
364362
test_must_fail test-parse-options --no-length >output 2>output.err &&

test-parse-options.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,18 @@ static char *file = NULL;
1414
static int ambiguous;
1515
static struct string_list list;
1616

17+
static struct {
18+
int called;
19+
const char *arg;
20+
int unset;
21+
} length_cb;
22+
1723
static int length_callback(const struct option *opt, const char *arg, int unset)
1824
{
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+
2129
if (unset)
2230
return 1; /* do not support unset */
2331

@@ -84,6 +92,12 @@ int main(int argc, char **argv)
8492

8593
argc = parse_options(argc, (const char **)argv, prefix, options, usage, 0);
8694

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+
}
87101
printf("boolean: %d\n", boolean);
88102
printf("integer: %d\n", integer);
89103
printf("magnitude: %lu\n", magnitude);

0 commit comments

Comments
 (0)