Skip to content

Commit bc8620b

Browse files
bonzinigitster
authored andcommitted
parse-options: convert "command mode" to a flag
OPTION_CMDMODE is essentially OPTION_SET_INT plus an extra check that the variable had not set before. In order to allow custom processing of the option, for example a "command mode" option that also has an argument, it would be nice to use OPTION_CALLBACK and not have to rewrite the extra check on incompatible options. In other words, making the processing of the option orthogonal to the "only one of these" behavior provided by OPTION_CMDMODE. Add a new flag that takes care of the check, and modify OPT_CMDMODE to use it together with OPTION_SET_INT. The new flag still requires that the option value points to an int, but any OPTION_* value can be specified as long as it does not require a non-int type for opt->value. Signed-off-by: Paolo Bonzini <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 62e7a6f commit bc8620b

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

parse-options.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ static enum parse_opt_result opt_command_mode_error(
6161
*/
6262
for (that = all_opts; that->type != OPTION_END; that++) {
6363
if (that == opt ||
64-
that->type != OPTION_CMDMODE ||
64+
!(that->flags & PARSE_OPT_CMDMODE) ||
6565
that->value != opt->value ||
6666
that->defval != *(int *)opt->value)
6767
continue;
@@ -95,6 +95,14 @@ static enum parse_opt_result get_value(struct parse_opt_ctx_t *p,
9595
if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
9696
return error(_("%s takes no value"), optname(opt, flags));
9797

98+
/*
99+
* Giving the same mode option twice, although unnecessary,
100+
* is not a grave error, so let it pass.
101+
*/
102+
if ((opt->flags & PARSE_OPT_CMDMODE) &&
103+
*(int *)opt->value && *(int *)opt->value != opt->defval)
104+
return opt_command_mode_error(opt, all_opts, flags);
105+
98106
switch (opt->type) {
99107
case OPTION_LOWLEVEL_CALLBACK:
100108
return opt->ll_callback(p, opt, NULL, unset);
@@ -130,16 +138,6 @@ static enum parse_opt_result get_value(struct parse_opt_ctx_t *p,
130138
*(int *)opt->value = unset ? 0 : opt->defval;
131139
return 0;
132140

133-
case OPTION_CMDMODE:
134-
/*
135-
* Giving the same mode option twice, although is unnecessary,
136-
* is not a grave error, so let it pass.
137-
*/
138-
if (*(int *)opt->value && *(int *)opt->value != opt->defval)
139-
return opt_command_mode_error(opt, all_opts, flags);
140-
*(int *)opt->value = opt->defval;
141-
return 0;
142-
143141
case OPTION_STRING:
144142
if (unset)
145143
*(const char **)opt->value = NULL;

parse-options.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ enum parse_opt_type {
1818
OPTION_BITOP,
1919
OPTION_COUNTUP,
2020
OPTION_SET_INT,
21-
OPTION_CMDMODE,
2221
/* options with arguments (usually) */
2322
OPTION_STRING,
2423
OPTION_INTEGER,
@@ -47,7 +46,8 @@ enum parse_opt_option_flags {
4746
PARSE_OPT_LITERAL_ARGHELP = 64,
4847
PARSE_OPT_SHELL_EVAL = 256,
4948
PARSE_OPT_NOCOMPLETE = 512,
50-
PARSE_OPT_COMP_ARG = 1024
49+
PARSE_OPT_COMP_ARG = 1024,
50+
PARSE_OPT_CMDMODE = 2048
5151
};
5252

5353
enum parse_opt_result {
@@ -168,8 +168,8 @@ struct option {
168168
#define OPT_BOOL(s, l, v, h) OPT_BOOL_F(s, l, v, h, 0)
169169
#define OPT_HIDDEN_BOOL(s, l, v, h) { OPTION_SET_INT, (s), (l), (v), NULL, \
170170
(h), PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 1}
171-
#define OPT_CMDMODE(s, l, v, h, i) { OPTION_CMDMODE, (s), (l), (v), NULL, \
172-
(h), PARSE_OPT_NOARG|PARSE_OPT_NONEG, NULL, (i) }
171+
#define OPT_CMDMODE(s, l, v, h, i) { OPTION_SET_INT, (s), (l), (v), NULL, \
172+
(h), PARSE_OPT_CMDMODE|PARSE_OPT_NOARG|PARSE_OPT_NONEG, NULL, (i) }
173173
#define OPT_INTEGER(s, l, v, h) OPT_INTEGER_F(s, l, v, h, 0)
174174
#define OPT_MAGNITUDE(s, l, v, h) { OPTION_MAGNITUDE, (s), (l), (v), \
175175
N_("n"), (h), PARSE_OPT_NONEG }

0 commit comments

Comments
 (0)