@matheusbsbs / matheus-bagliano / commits / bc8620b440
Fork 0

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 <pbonzini@redhat.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Paolo Bonzini committed Feb 20, 2020 at 15:15 UTC bc8620b44094e9fb6fc20d141678f9b845c19193
2 files changed +13 -15
parse-options.c
+9 -11
@@ -61,7 +61,7 @@ static enum parse_opt_result opt_command_mode_error(
61 */
62 for (that = all_opts; that->type != OPTION_END; that++) {
63 if (that == opt ||
64 - that->type != OPTION_CMDMODE ||
64 + !(that->flags & PARSE_OPT_CMDMODE) ||
65 that->value != opt->value ||
66 that->defval != *(int *)opt->value)
67 continue;
@@ -95,6 +95,14 @@ static enum parse_opt_result get_value(struct parse_opt_ctx_t *p,
95 if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
96 return error(_("%s takes no value"), optname(opt, flags));
97
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 +
106 switch (opt->type) {
107 case OPTION_LOWLEVEL_CALLBACK:
108 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,
138 *(int *)opt->value = unset ? 0 : opt->defval;
139 return 0;
140
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 -
141 case OPTION_STRING:
142 if (unset)
143 *(const char **)opt->value = NULL;
parse-options.h
+4 -4
@@ -18,7 +18,6 @@ enum parse_opt_type {
18 OPTION_BITOP,
19 OPTION_COUNTUP,
20 OPTION_SET_INT,
21 - OPTION_CMDMODE,
21 /* options with arguments (usually) */
22 OPTION_STRING,
23 OPTION_INTEGER,
@@ -47,7 +46,8 @@ enum parse_opt_option_flags {
46 PARSE_OPT_LITERAL_ARGHELP = 64,
47 PARSE_OPT_SHELL_EVAL = 256,
48 PARSE_OPT_NOCOMPLETE = 512,
50 - PARSE_OPT_COMP_ARG = 1024
49 + PARSE_OPT_COMP_ARG = 1024,
50 + PARSE_OPT_CMDMODE = 2048
51 };
52
53 enum parse_opt_result {
@@ -168,8 +168,8 @@ struct option {
168 #define OPT_BOOL(s, l, v, h) OPT_BOOL_F(s, l, v, h, 0)
169 #define OPT_HIDDEN_BOOL(s, l, v, h) { OPTION_SET_INT, (s), (l), (v), NULL, \
170 (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) }
173 #define OPT_INTEGER(s, l, v, h) OPT_INTEGER_F(s, l, v, h, 0)
174 #define OPT_MAGNITUDE(s, l, v, h) { OPTION_MAGNITUDE, (s), (l), (v), \
175 N_("n"), (h), PARSE_OPT_NONEG }