Skip to content

Commit 2f4b97f

Browse files
René Scharfegitster
authored andcommitted
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>
1 parent 5a0e4a2 commit 2f4b97f

File tree

5 files changed

+44
-2
lines changed

5 files changed

+44
-2
lines changed

Documentation/technical/api-parse-options.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ There are some macros to easily define options:
137137
Introduce a boolean option.
138138
If used, `int_var` is bitwise-ored with `mask`.
139139

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+
140144
`OPT_SET_INT(short, long, &int_var, description, integer)`::
141145
Introduce a boolean option.
142146
If used, set `int_var` to `integer`.

parse-options.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ static int get_value(struct parse_opt_ctx_t *p,
5050
/* FALLTHROUGH */
5151
case OPTION_BOOLEAN:
5252
case OPTION_BIT:
53+
case OPTION_NEGBIT:
5354
case OPTION_SET_INT:
5455
case OPTION_SET_PTR:
5556
return opterror(opt, "takes no value", flags);
@@ -66,6 +67,13 @@ static int get_value(struct parse_opt_ctx_t *p,
6667
*(int *)opt->value |= opt->defval;
6768
return 0;
6869

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+
6977
case OPTION_BOOLEAN:
7078
*(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
7179
return 0;

parse-options.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ enum parse_opt_type {
88
OPTION_GROUP,
99
/* options with no arguments */
1010
OPTION_BIT,
11+
OPTION_NEGBIT,
1112
OPTION_BOOLEAN, /* _INCR would have been a better name */
1213
OPTION_SET_INT,
1314
OPTION_SET_PTR,
@@ -93,6 +94,7 @@ struct option {
9394
#define OPT_ARGUMENT(l, h) { OPTION_ARGUMENT, 0, (l), NULL, NULL, (h) }
9495
#define OPT_GROUP(h) { OPTION_GROUP, 0, NULL, NULL, NULL, (h) }
9596
#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) }
9698
#define OPT_BOOLEAN(s, l, v, h) { OPTION_BOOLEAN, (s), (l), (v), NULL, (h) }
9799
#define OPT_SET_INT(s, l, v, h, i) { OPTION_SET_INT, (s), (l), (v), NULL, (h), 0, NULL, (i) }
98100
#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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ usage: test-parse-options <options>
1212
1313
-b, --boolean get a boolean
1414
-4, --or4 bitwise-or boolean with ...0100
15+
--neg-or4 same as --no-or4
1516
1617
-i, --integer <n> get a integer
1718
-j <n> get a integer, too
@@ -245,7 +246,33 @@ test_expect_success 'OPT_BIT() and OPT_SET_INT() work' '
245246
test_cmp expect output
246247
'
247248

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+
'
250277

251278
test_done

test-parse-options.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ int main(int argc, const char **argv)
2929
OPT_BOOLEAN('b', "boolean", &boolean, "get a boolean"),
3030
OPT_BIT('4', "or4", &boolean,
3131
"bitwise-or boolean with ...0100", 4),
32+
OPT_NEGBIT(0, "neg-or4", &boolean, "same as --no-or4", 4),
3233
OPT_GROUP(""),
3334
OPT_INTEGER('i', "integer", &integer, "get a integer"),
3435
OPT_INTEGER('j', NULL, &integer, "get a integer, too"),

0 commit comments

Comments
 (0)