parse-options: add OPT_BITOP()

This is needed for diff_opt_parse() where we do value = (value & ~mask) | some_more; Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Jan 27, 2019 at 07:35 UTC f62470c650e0db9536f32b9e1ecbe7d25f759031
2 files changed +12
parse-options.c
+7
@@ -109,6 +109,13 @@ static int get_value(struct parse_opt_ctx_t *p,
109 *(int *)opt->value &= ~opt->defval;
110 return 0;
111
112 + case OPTION_BITOP:
113 + if (unset)
114 + BUG("BITOP can't have unset form");
115 + *(int *)opt->value &= ~opt->extra;
116 + *(int *)opt->value |= opt->defval;
117 + return 0;
118 +
119 case OPTION_COUNTUP:
120 if (*(int *)opt->value < 0)
121 *(int *)opt->value = 0;
parse-options.h
+5
@@ -10,6 +10,7 @@ enum parse_opt_type {
10 /* options with no arguments */
11 OPTION_BIT,
12 OPTION_NEGBIT,
13 + OPTION_BITOP,
14 OPTION_COUNTUP,
15 OPTION_SET_INT,
16 OPTION_CMDMODE,
@@ -118,6 +119,7 @@ struct option {
119 int flags;
120 parse_opt_cb *callback;
121 intptr_t defval;
122 + intptr_t extra;
123 };
124
125 #define OPT_BIT_F(s, l, v, h, b, f) { OPTION_BIT, (s), (l), (v), NULL, (h), \
@@ -133,6 +135,9 @@ struct option {
135 (h), PARSE_OPT_NOARG}
136 #define OPT_GROUP(h) { OPTION_GROUP, 0, NULL, NULL, NULL, (h) }
137 #define OPT_BIT(s, l, v, h, b) OPT_BIT_F(s, l, v, h, b, 0)
138 +#define OPT_BITOP(s, l, v, h, set, clear) { OPTION_BITOP, (s), (l), (v), NULL, (h), \
139 + PARSE_OPT_NOARG|PARSE_OPT_NONEG, NULL, \
140 + (set), (clear) }
141 #define OPT_NEGBIT(s, l, v, h, b) { OPTION_NEGBIT, (s), (l), (v), NULL, \
142 (h), PARSE_OPT_NOARG, NULL, (b) }
143 #define OPT_COUNTUP(s, l, v, h) OPT_COUNTUP_F(s, l, v, h, 0)