Skip to content

Commit 38916c5

Browse files
René Scharfegitster
authored andcommitted
parse-options: typo check for unknown switches
The user specifies a long option but forgets to type the second leading dash, we currently detect and report that fact if its first letter is a valid short option. This is done for safety, to avoid ambiguity between short options (and their arguments) and a long option with a missing dash. This diagnostic message is also helpful for long options whose first letter is not a valid short option, however. Print it in that case, too, as a courtesy. Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b22939a commit 38916c5

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

parse-options.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,8 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
393393
case -1:
394394
return parse_options_usage(ctx, usagestr, options, 1);
395395
case -2:
396+
if (ctx->opt)
397+
check_typos(arg + 1, options);
396398
goto unknown;
397399
}
398400
if (ctx->opt)

t/t0040-parse-options.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,16 @@ test_expect_success 'detect possible typos' '
236236
test_cmp typo.err output.err
237237
'
238238

239+
cat > typo.err << EOF
240+
error: did you mean \`--ambiguous\` (with two dashes ?)
241+
EOF
242+
243+
test_expect_success 'detect possible typos' '
244+
test_must_fail test-parse-options -ambiguous > output 2> output.err &&
245+
test ! -s output &&
246+
test_cmp typo.err output.err
247+
'
248+
239249
cat > expect <<EOF
240250
boolean: 0
241251
integer: 0

0 commit comments

Comments
 (0)