@matheusbsbs / matheus-bagliano / commits / 2f4b97f910
Fork 0

parseopt: add OPT_NEGBIT

Add OPTION_NEGBIT and OPT_NEGBIT, mirroring OPTION_BIT and OPT_BIT. OPT_NEGBIT can be used together with OPT_BIT to define two options that cancel each other out. Note: this patch removes the reminder from the test script because it adds a test for --no-or4 and there already was one for --or4. Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed May 7, 2009 at 21:44 UTC 2f4b97f91071f5060bf2da482cf8b0d70486d808
5 files changed +44 -2
Documentation/technical/api-parse-options.txt
+4
@@ -137,6 +137,10 @@ There are some macros to easily define options:
137 Introduce a boolean option.
138 If used, `int_var` is bitwise-ored with `mask`.
139
140 +`OPT_NEGBIT(short, long, &int_var, description, mask)`::
141 + Introduce a boolean option.
142 + If used, `int_var` is bitwise-anded with the inverted `mask`.
143 +
144 `OPT_SET_INT(short, long, &int_var, description, integer)`::
145 Introduce a boolean option.
146 If used, set `int_var` to `integer`.
parse-options.c
+8
@@ -50,6 +50,7 @@ static int get_value(struct parse_opt_ctx_t *p,
50 /* FALLTHROUGH */
51 case OPTION_BOOLEAN:
52 case OPTION_BIT:
53 + case OPTION_NEGBIT:
54 case OPTION_SET_INT:
55 case OPTION_SET_PTR:
56 return opterror(opt, "takes no value", flags);
@@ -66,6 +67,13 @@ static int get_value(struct parse_opt_ctx_t *p,
67 *(int *)opt->value |= opt->defval;
68 return 0;
69
70 + case OPTION_NEGBIT:
71 + if (unset)
72 + *(int *)opt->value |= opt->defval;
73 + else
74 + *(int *)opt->value &= ~opt->defval;
75 + return 0;
76 +
77 case OPTION_BOOLEAN:
78 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
79 return 0;
parse-options.h
+2
@@ -8,6 +8,7 @@ enum parse_opt_type {
8 OPTION_GROUP,
9 /* options with no arguments */
10 OPTION_BIT,
11 + OPTION_NEGBIT,
12 OPTION_BOOLEAN, /* _INCR would have been a better name */
13 OPTION_SET_INT,
14 OPTION_SET_PTR,
@@ -93,6 +94,7 @@ struct option {
94 #define OPT_ARGUMENT(l, h) { OPTION_ARGUMENT, 0, (l), NULL, NULL, (h) }
95 #define OPT_GROUP(h) { OPTION_GROUP, 0, NULL, NULL, NULL, (h) }
96 #define OPT_BIT(s, l, v, h, b) { OPTION_BIT, (s), (l), (v), NULL, (h), 0, NULL, (b) }
97 +#define OPT_NEGBIT(s, l, v, h, b) { OPTION_NEGBIT, (s), (l), (v), NULL, (h), 0, NULL, (b) }
98 #define OPT_BOOLEAN(s, l, v, h) { OPTION_BOOLEAN, (s), (l), (v), NULL, (h) }
99 #define OPT_SET_INT(s, l, v, h, i) { OPTION_SET_INT, (s), (l), (v), NULL, (h), 0, NULL, (i) }
100 #define OPT_SET_PTR(s, l, v, h, p) { OPTION_SET_PTR, (s), (l), (v), NULL, (h), 0, NULL, (p) }
t/t0040-parse-options.sh
+29 -2
@@ -12,6 +12,7 @@ usage: test-parse-options <options>
12
13 -b, --boolean get a boolean
14 -4, --or4 bitwise-or boolean with ...0100
15 + --neg-or4 same as --no-or4
16
17 -i, --integer <n> get a integer
18 -j <n> get a integer, too
@@ -245,7 +246,33 @@ test_expect_success 'OPT_BIT() and OPT_SET_INT() work' '
246 test_cmp expect output
247 '
248
248 -# --or4
249 -# --no-or4
249 +test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' '
250 + test-parse-options --set23 -bbbbb --neg-or4 > output 2> output.err &&
251 + test ! -s output.err &&
252 + test_cmp expect output
253 +'
254 +
255 +cat > expect <<EOF
256 +boolean: 6
257 +integer: 0
258 +timestamp: 0
259 +string: (not set)
260 +abbrev: 7
261 +verbose: 0
262 +quiet: no
263 +dry run: no
264 +EOF
265 +
266 +test_expect_success 'OPT_BIT() works' '
267 + test-parse-options -bb --or4 > output 2> output.err &&
268 + test ! -s output.err &&
269 + test_cmp expect output
270 +'
271 +
272 +test_expect_success 'OPT_NEGBIT() works' '
273 + test-parse-options -bb --no-neg-or4 > output 2> output.err &&
274 + test ! -s output.err &&
275 + test_cmp expect output
276 +'
277
278 test_done
test-parse-options.c
+1
@@ -29,6 +29,7 @@ int main(int argc, const char **argv)
29 OPT_BOOLEAN('b', "boolean", &boolean, "get a boolean"),
30 OPT_BIT('4', "or4", &boolean,
31 "bitwise-or boolean with ...0100", 4),
32 + OPT_NEGBIT(0, "neg-or4", &boolean, "same as --no-or4", 4),
33 OPT_GROUP(""),
34 OPT_INTEGER('i', "integer", &integer, "get a integer"),
35 OPT_INTEGER('j', NULL, &integer, "get a integer, too"),