Skip to content

Commit

Permalink
parse-options.c: use new bug() API for optbug()
Browse files Browse the repository at this point in the history
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 <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
avar authored and gitster committed Jun 2, 2022
1 parent 0cc05b0 commit 53ca569
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions parse-options.c
Expand Up @@ -14,15 +14,15 @@ enum opt_parsed {
OPT_UNSET = 1<<1,
};

static int optbug(const struct option *opt, const char *reason)
static void optbug(const struct option *opt, const char *reason)
{
if (opt->long_name) {
if (opt->short_name)
return error("BUG: switch '%c' (--%s) %s",
opt->short_name, opt->long_name, reason);
return error("BUG: option '%s' %s", opt->long_name, reason);
}
return error("BUG: switch '%c' %s", opt->short_name, reason);
if (opt->long_name && opt->short_name)
bug("switch '%c' (--%s) %s", opt->short_name,
opt->long_name, reason);
else if (opt->long_name)
bug("option '%s' %s", opt->long_name, reason);
else
bug("switch '%c' %s", opt->short_name, reason);
}

static const char *optname(const struct option *opt, enum opt_parsed flags)
Expand Down Expand Up @@ -441,28 +441,27 @@ static void check_typos(const char *arg, const struct option *options)

static void parse_options_check(const struct option *opts)
{
int err = 0;
char short_opts[128];

memset(short_opts, '\0', sizeof(short_opts));
for (; opts->type != OPTION_END; opts++) {
if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
(opts->flags & PARSE_OPT_OPTARG))
err |= optbug(opts, "uses incompatible flags "
"LASTARG_DEFAULT and OPTARG");
optbug(opts, "uses incompatible flags "
"LASTARG_DEFAULT and OPTARG");
if (opts->short_name) {
if (0x7F <= opts->short_name)
err |= optbug(opts, "invalid short name");
optbug(opts, "invalid short name");
else if (short_opts[opts->short_name]++)
err |= optbug(opts, "short name already used");
optbug(opts, "short name already used");
}
if (opts->flags & PARSE_OPT_NODASH &&
((opts->flags & PARSE_OPT_OPTARG) ||
!(opts->flags & PARSE_OPT_NOARG) ||
!(opts->flags & PARSE_OPT_NONEG) ||
opts->long_name))
err |= optbug(opts, "uses feature "
"not supported for dashless options");
optbug(opts, "uses feature "
"not supported for dashless options");
switch (opts->type) {
case OPTION_COUNTUP:
case OPTION_BIT:
Expand All @@ -471,7 +470,7 @@ static void parse_options_check(const struct option *opts)
case OPTION_NUMBER:
if ((opts->flags & PARSE_OPT_OPTARG) ||
!(opts->flags & PARSE_OPT_NOARG))
err |= optbug(opts, "should not accept an argument");
optbug(opts, "should not accept an argument");
break;
case OPTION_CALLBACK:
if (!opts->callback && !opts->ll_callback)
Expand All @@ -494,10 +493,9 @@ static void parse_options_check(const struct option *opts)
}
if (opts->argh &&
strcspn(opts->argh, " _") != strlen(opts->argh))
err |= optbug(opts, "multi-word argh should use dash to separate words");
optbug(opts, "multi-word argh should use dash to separate words");
}
if (err)
exit(128);
BUG_if_bug("invalid 'struct option'");
}

static void parse_options_start_1(struct parse_opt_ctx_t *ctx,
Expand Down

0 comments on commit 53ca569

Please sign in to comment.