parse-options.c: use new bug() API for optbug()

When we run into bugs in parse-options.c usage it's good to be able to note all the issues we ran into before dying. This use-case is why we have the optbug() function introduced in 1e5ce570ca3 (parse-options: clearer reporting of API misuse, 2010-12-02) Let's change this code to use the new bug() API introduced in the preceding commit, which cuts down on the verbosity of parse_options_check(). There are existing uses of BUG() in adjacent code that should have been using optbug() that aren't being changed here. That'll be done in a subsequent commit. This only changes the optbug() callers. Since this will invoke BUG() the previous exit(128) code will be changed, but in this case that's what we want, i.e. to have encountering a BUG() return the specific "BUG" exit code. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Jun 2, 2022 at 14:25 UTC 53ca569419aed9706b052c48656715ab0b5f14fd
1 file changed +17 -19
parse-options.c
+17 -19
@@ -14,15 +14,15 @@ enum opt_parsed {
14 OPT_UNSET = 1<<1,
15 };
16
17 -static int optbug(const struct option *opt, const char *reason)
17 +static void optbug(const struct option *opt, const char *reason)
18 {
19 - if (opt->long_name) {
20 - if (opt->short_name)
21 - return error("BUG: switch '%c' (--%s) %s",
22 - opt->short_name, opt->long_name, reason);
23 - return error("BUG: option '%s' %s", opt->long_name, reason);
24 - }
25 - return error("BUG: switch '%c' %s", opt->short_name, reason);
19 + if (opt->long_name && opt->short_name)
20 + bug("switch '%c' (--%s) %s", opt->short_name,
21 + opt->long_name, reason);
22 + else if (opt->long_name)
23 + bug("option '%s' %s", opt->long_name, reason);
24 + else
25 + bug("switch '%c' %s", opt->short_name, reason);
26 }
27
28 static const char *optname(const struct option *opt, enum opt_parsed flags)
@@ -441,28 +441,27 @@ static void check_typos(const char *arg, const struct option *options)
441
442 static void parse_options_check(const struct option *opts)
443 {
444 - int err = 0;
444 char short_opts[128];
445
446 memset(short_opts, '\0', sizeof(short_opts));
447 for (; opts->type != OPTION_END; opts++) {
448 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
449 (opts->flags & PARSE_OPT_OPTARG))
451 - err |= optbug(opts, "uses incompatible flags "
452 - "LASTARG_DEFAULT and OPTARG");
450 + optbug(opts, "uses incompatible flags "
451 + "LASTARG_DEFAULT and OPTARG");
452 if (opts->short_name) {
453 if (0x7F <= opts->short_name)
455 - err |= optbug(opts, "invalid short name");
454 + optbug(opts, "invalid short name");
455 else if (short_opts[opts->short_name]++)
457 - err |= optbug(opts, "short name already used");
456 + optbug(opts, "short name already used");
457 }
458 if (opts->flags & PARSE_OPT_NODASH &&
459 ((opts->flags & PARSE_OPT_OPTARG) ||
460 !(opts->flags & PARSE_OPT_NOARG) ||
461 !(opts->flags & PARSE_OPT_NONEG) ||
462 opts->long_name))
464 - err |= optbug(opts, "uses feature "
465 - "not supported for dashless options");
463 + optbug(opts, "uses feature "
464 + "not supported for dashless options");
465 switch (opts->type) {
466 case OPTION_COUNTUP:
467 case OPTION_BIT:
@@ -471,7 +470,7 @@ static void parse_options_check(const struct option *opts)
470 case OPTION_NUMBER:
471 if ((opts->flags & PARSE_OPT_OPTARG) ||
472 !(opts->flags & PARSE_OPT_NOARG))
474 - err |= optbug(opts, "should not accept an argument");
473 + optbug(opts, "should not accept an argument");
474 break;
475 case OPTION_CALLBACK:
476 if (!opts->callback && !opts->ll_callback)
@@ -494,10 +493,9 @@ static void parse_options_check(const struct option *opts)
493 }
494 if (opts->argh &&
495 strcspn(opts->argh, " _") != strlen(opts->argh))
497 - err |= optbug(opts, "multi-word argh should use dash to separate words");
496 + optbug(opts, "multi-word argh should use dash to separate words");
497 }
499 - if (err)
500 - exit(128);
498 + BUG_if_bug("invalid 'struct option'");
499 }
500
501 static void parse_options_start_1(struct parse_opt_ctx_t *ctx,