Skip to content

Commit d342834

Browse files
avargitster
authored andcommitted
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 0f1930c (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 62f2ffc commit d342834

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

parse-options.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88

99
static int disallow_abbreviated_options;
1010

11-
#define OPT_SHORT 1
12-
#define OPT_UNSET 2
11+
enum opt_parsed {
12+
OPT_LONG = 0,
13+
OPT_SHORT = 1<<0,
14+
OPT_UNSET = 1<<1,
15+
};
1316

1417
static int optbug(const struct option *opt, const char *reason)
1518
{
@@ -22,7 +25,7 @@ static int optbug(const struct option *opt, const char *reason)
2225
return error("BUG: switch '%c' %s", opt->short_name, reason);
2326
}
2427

25-
static const char *optname(const struct option *opt, int flags)
28+
static const char *optname(const struct option *opt, enum opt_parsed flags)
2629
{
2730
static struct strbuf sb = STRBUF_INIT;
2831

@@ -31,15 +34,17 @@ static const char *optname(const struct option *opt, int flags)
3134
strbuf_addf(&sb, "switch `%c'", opt->short_name);
3235
else if (flags & OPT_UNSET)
3336
strbuf_addf(&sb, "option `no-%s'", opt->long_name);
34-
else
37+
else if (flags == OPT_LONG)
3538
strbuf_addf(&sb, "option `%s'", opt->long_name);
39+
else
40+
BUG("optname() got unknown flags %d", flags);
3641

3742
return sb.buf;
3843
}
3944

4045
static enum parse_opt_result get_arg(struct parse_opt_ctx_t *p,
4146
const struct option *opt,
42-
int flags, const char **arg)
47+
enum opt_parsed flags, const char **arg)
4348
{
4449
if (p->opt) {
4550
*arg = p->opt;
@@ -65,7 +70,7 @@ static void fix_filename(const char *prefix, const char **file)
6570
static enum parse_opt_result opt_command_mode_error(
6671
const struct option *opt,
6772
const struct option *all_opts,
68-
int flags)
73+
enum opt_parsed flags)
6974
{
7075
const struct option *that;
7176
struct strbuf that_name = STRBUF_INIT;
@@ -97,7 +102,7 @@ static enum parse_opt_result opt_command_mode_error(
97102
static enum parse_opt_result get_value(struct parse_opt_ctx_t *p,
98103
const struct option *opt,
99104
const struct option *all_opts,
100-
int flags)
105+
enum opt_parsed flags)
101106
{
102107
const char *s, *arg;
103108
const int unset = flags & OPT_UNSET;
@@ -313,11 +318,11 @@ static enum parse_opt_result parse_long_opt(
313318
const struct option *all_opts = options;
314319
const char *arg_end = strchrnul(arg, '=');
315320
const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
316-
int abbrev_flags = 0, ambiguous_flags = 0;
321+
enum opt_parsed abbrev_flags = OPT_LONG, ambiguous_flags = OPT_LONG;
317322

318323
for (; options->type != OPTION_END; options++) {
319324
const char *rest, *long_name = options->long_name;
320-
int flags = 0, opt_flags = 0;
325+
enum opt_parsed flags = OPT_LONG, opt_flags = OPT_LONG;
321326

322327
if (!long_name)
323328
continue;

0 commit comments

Comments
 (0)