Skip to content

Commit 53ca569

Browse files
avargitster
authored andcommitted
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 1e5ce57 (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0cc05b0 commit 53ca569

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

parse-options.c

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ enum opt_parsed {
1414
OPT_UNSET = 1<<1,
1515
};
1616

17-
static int optbug(const struct option *opt, const char *reason)
17+
static void optbug(const struct option *opt, const char *reason)
1818
{
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);
2626
}
2727

2828
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)
441441

442442
static void parse_options_check(const struct option *opts)
443443
{
444-
int err = 0;
445444
char short_opts[128];
446445

447446
memset(short_opts, '\0', sizeof(short_opts));
448447
for (; opts->type != OPTION_END; opts++) {
449448
if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
450449
(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");
453452
if (opts->short_name) {
454453
if (0x7F <= opts->short_name)
455-
err |= optbug(opts, "invalid short name");
454+
optbug(opts, "invalid short name");
456455
else if (short_opts[opts->short_name]++)
457-
err |= optbug(opts, "short name already used");
456+
optbug(opts, "short name already used");
458457
}
459458
if (opts->flags & PARSE_OPT_NODASH &&
460459
((opts->flags & PARSE_OPT_OPTARG) ||
461460
!(opts->flags & PARSE_OPT_NOARG) ||
462461
!(opts->flags & PARSE_OPT_NONEG) ||
463462
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");
466465
switch (opts->type) {
467466
case OPTION_COUNTUP:
468467
case OPTION_BIT:
@@ -471,7 +470,7 @@ static void parse_options_check(const struct option *opts)
471470
case OPTION_NUMBER:
472471
if ((opts->flags & PARSE_OPT_OPTARG) ||
473472
!(opts->flags & PARSE_OPT_NOARG))
474-
err |= optbug(opts, "should not accept an argument");
473+
optbug(opts, "should not accept an argument");
475474
break;
476475
case OPTION_CALLBACK:
477476
if (!opts->callback && !opts->ll_callback)
@@ -494,10 +493,9 @@ static void parse_options_check(const struct option *opts)
494493
}
495494
if (opts->argh &&
496495
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");
498497
}
499-
if (err)
500-
exit(128);
498+
BUG_if_bug("invalid 'struct option'");
501499
}
502500

503501
static void parse_options_start_1(struct parse_opt_ctx_t *ctx,

0 commit comments

Comments
 (0)