parse-options: change OPT_{SHORT,UNSET} to an enum

Change the comparisons against OPT_SHORT and OPT_UNSET to an enum
which keeps track of how a given option got parsed. The case of "0"
was an implicit OPT_LONG, so let's add an explicit label for it.

Due to the xor in 0f1930c587 (parse-options: allow positivation of
options starting, with no-, 2012-02-25) the code already relied on
this being set back to 0. To avoid refactoring the logic involved in
that let's just start the enum at "0" instead of the usual "1<<0" (1),
but BUG() out if we don't have one of our expected flags.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Ævar Arnfjörð Bjarmason 2021-10-08 21:07:46 +02:00 коммит произвёл Junio C Hamano
Родитель 62f2ffc563
Коммит d342834529
1 изменённых файлов: 14 добавлений и 9 удалений

Просмотреть файл

@ -8,8 +8,11 @@
static int disallow_abbreviated_options;
#define OPT_SHORT 1
#define OPT_UNSET 2
enum opt_parsed {
OPT_LONG = 0,
OPT_SHORT = 1<<0,
OPT_UNSET = 1<<1,
};
static int optbug(const struct option *opt, const char *reason)
{
@ -22,7 +25,7 @@ static int optbug(const struct option *opt, const char *reason)
return error("BUG: switch '%c' %s", opt->short_name, reason);
}
static const char *optname(const struct option *opt, int flags)
static const char *optname(const struct option *opt, enum opt_parsed flags)
{
static struct strbuf sb = STRBUF_INIT;
@ -31,15 +34,17 @@ static const char *optname(const struct option *opt, int flags)
strbuf_addf(&sb, "switch `%c'", opt->short_name);
else if (flags & OPT_UNSET)
strbuf_addf(&sb, "option `no-%s'", opt->long_name);
else
else if (flags == OPT_LONG)
strbuf_addf(&sb, "option `%s'", opt->long_name);
else
BUG("optname() got unknown flags %d", flags);
return sb.buf;
}
static enum parse_opt_result get_arg(struct parse_opt_ctx_t *p,
const struct option *opt,
int flags, const char **arg)
enum opt_parsed flags, const char **arg)
{
if (p->opt) {
*arg = p->opt;
@ -65,7 +70,7 @@ static void fix_filename(const char *prefix, const char **file)
static enum parse_opt_result opt_command_mode_error(
const struct option *opt,
const struct option *all_opts,
int flags)
enum opt_parsed flags)
{
const struct option *that;
struct strbuf that_name = STRBUF_INIT;
@ -97,7 +102,7 @@ static enum parse_opt_result opt_command_mode_error(
static enum parse_opt_result get_value(struct parse_opt_ctx_t *p,
const struct option *opt,
const struct option *all_opts,
int flags)
enum opt_parsed flags)
{
const char *s, *arg;
const int unset = flags & OPT_UNSET;
@ -313,11 +318,11 @@ static enum parse_opt_result parse_long_opt(
const struct option *all_opts = options;
const char *arg_end = strchrnul(arg, '=');
const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
int abbrev_flags = 0, ambiguous_flags = 0;
enum opt_parsed abbrev_flags = OPT_LONG, ambiguous_flags = OPT_LONG;
for (; options->type != OPTION_END; options++) {
const char *rest, *long_name = options->long_name;
int flags = 0, opt_flags = 0;
enum opt_parsed flags = OPT_LONG, opt_flags = OPT_LONG;
if (!long_name)
continue;