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 <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Mar 3, 2012 at 12:00 UTC 38916c5b4740f6db09dc140a84bb470dfb582366
2 files changed +12
parse-options.c
+2
@@ -393,6 +393,8 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
393 case -1:
394 return parse_options_usage(ctx, usagestr, options, 1);
395 case -2:
396 + if (ctx->opt)
397 + check_typos(arg + 1, options);
398 goto unknown;
399 }
400 if (ctx->opt)
t/t0040-parse-options.sh
+10
@@ -236,6 +236,16 @@ test_expect_success 'detect possible typos' '
236 test_cmp typo.err output.err
237 '
238
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 +
249 cat > expect <<EOF
250 boolean: 0
251 integer: 0