Skip to content

Commit e0070e8

Browse files
pranitbauva1997gitster
authored andcommitted
parse-options.c: make OPTION_COUNTUP respect "unspecified" values
OPT_COUNTUP() merely increments the counter upon --option, and resets it to 0 upon --no-option, which means that there is no "unspecified" value with which a client can initialize the counter to determine whether or not --[no]-option was seen at all. Make OPT_COUNTUP() treat any negative number as an "unspecified" value to address this shortcoming. In particular, if a client initializes the counter to -1, then if it is still -1 after parse_options(), then neither --option nor --no-option was seen; if it is 0, then --no-option was seen last, and if it is 1 or greater, than --option was seen last. This change does not affect the behavior of existing clients because they all use the initial value of 0 (or more). Note that builtin/clean.c initializes the variable used with OPT__FORCE (which uses OPT_COUNTUP()) to a negative value, but it is set to either 0 or 1 by reading the configuration before the code calls parse_options(), i.e. as far as parse_options() is concerned, the initial value of the variable is not negative. To test this behavior, in test-parse-options.c, "verbose" is set to "unspecified" while quiet is set to 0 which will test the new behavior with all sets of values. Helped-by: Jeff King <[email protected]> Helped-by: Eric Sunshine <[email protected]> Helped-by: Junio C Hamano <[email protected]> Signed-off-by: Pranit Bauva <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 98baeb7 commit e0070e8

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

Documentation/technical/api-parse-options.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,12 @@ There are some macros to easily define options:
144144

145145
`OPT_COUNTUP(short, long, &int_var, description)`::
146146
Introduce a count-up option.
147-
`int_var` is incremented on each use of `--option`, and
148-
reset to zero with `--no-option`.
147+
Each use of `--option` increments `int_var`, starting from zero
148+
(even if initially negative), and `--no-option` resets it to
149+
zero. To determine if `--option` or `--no-option` was encountered at
150+
all, initialize `int_var` to a negative value, and if it is still
151+
negative after parse_options(), then neither `--option` nor
152+
`--no-option` was seen.
149153

150154
`OPT_BIT(short, long, &int_var, description, mask)`::
151155
Introduce a boolean option.

parse-options.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ static int get_value(struct parse_opt_ctx_t *p,
110110
return 0;
111111

112112
case OPTION_COUNTUP:
113+
if (*(int *)opt->value < 0)
114+
*(int *)opt->value = 0;
113115
*(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
114116
return 0;
115117

t/t0040-parse-options.sh

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ magnitude: 0
6363
timestamp: 0
6464
string: (not set)
6565
abbrev: 7
66-
verbose: 0
66+
verbose: -1
6767
quiet: 0
6868
dry run: no
6969
file: (not set)
@@ -211,7 +211,7 @@ magnitude: 0
211211
timestamp: 0
212212
string: 123
213213
abbrev: 7
214-
verbose: 0
214+
verbose: -1
215215
quiet: 0
216216
dry run: no
217217
file: (not set)
@@ -234,7 +234,7 @@ magnitude: 0
234234
timestamp: 0
235235
string: (not set)
236236
abbrev: 7
237-
verbose: 0
237+
verbose: -1
238238
quiet: 0
239239
dry run: no
240240
file: (not set)
@@ -263,7 +263,7 @@ magnitude: 0
263263
timestamp: 0
264264
string: 123
265265
abbrev: 7
266-
verbose: 0
266+
verbose: -1
267267
quiet: 0
268268
dry run: no
269269
file: (not set)
@@ -302,7 +302,7 @@ magnitude: 0
302302
timestamp: 0
303303
string: (not set)
304304
abbrev: 7
305-
verbose: 0
305+
verbose: -1
306306
quiet: 0
307307
dry run: no
308308
file: (not set)
@@ -322,7 +322,7 @@ magnitude: 0
322322
timestamp: 1
323323
string: (not set)
324324
abbrev: 7
325-
verbose: 0
325+
verbose: -1
326326
quiet: 1
327327
dry run: no
328328
file: (not set)
@@ -344,7 +344,7 @@ magnitude: 0
344344
timestamp: 0
345345
string: (not set)
346346
abbrev: 7
347-
verbose: 0
347+
verbose: -1
348348
quiet: 0
349349
dry run: no
350350
file: (not set)
@@ -373,7 +373,7 @@ magnitude: 0
373373
timestamp: 0
374374
string: (not set)
375375
abbrev: 7
376-
verbose: 0
376+
verbose: -1
377377
quiet: 0
378378
dry run: no
379379
file: (not set)
@@ -398,7 +398,7 @@ magnitude: 0
398398
timestamp: 0
399399
string: (not set)
400400
abbrev: 7
401-
verbose: 0
401+
verbose: -1
402402
quiet: 0
403403
dry run: no
404404
file: (not set)
@@ -429,7 +429,7 @@ magnitude: 0
429429
timestamp: 0
430430
string: (not set)
431431
abbrev: 7
432-
verbose: 0
432+
verbose: -1
433433
quiet: 0
434434
dry run: no
435435
file: (not set)
@@ -448,7 +448,7 @@ magnitude: 0
448448
timestamp: 0
449449
string: (not set)
450450
abbrev: 7
451-
verbose: 0
451+
verbose: -1
452452
quiet: 0
453453
dry run: no
454454
file: (not set)
@@ -483,7 +483,7 @@ magnitude: 0
483483
timestamp: 0
484484
string: (not set)
485485
abbrev: 7
486-
verbose: 0
486+
verbose: -1
487487
quiet: 3
488488
dry run: no
489489
file: (not set)
@@ -521,7 +521,7 @@ magnitude: 0
521521
timestamp: 0
522522
string: (not set)
523523
abbrev: 7
524-
verbose: 0
524+
verbose: -1
525525
quiet: 0
526526
dry run: no
527527
file: (not set)
@@ -540,7 +540,7 @@ magnitude: 0
540540
timestamp: 0
541541
string: (not set)
542542
abbrev: 7
543-
verbose: 0
543+
verbose: -1
544544
quiet: 0
545545
dry run: no
546546
file: (not set)

test-parse-options.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ static int integer = 0;
77
static unsigned long magnitude = 0;
88
static unsigned long timestamp;
99
static int abbrev = 7;
10-
static int verbose = 0, dry_run = 0, quiet = 0;
10+
static int verbose = -1; /* unspecified */
11+
static int dry_run = 0, quiet = 0;
1112
static char *string = NULL;
1213
static char *file = NULL;
1314
static int ambiguous;

0 commit comments

Comments
 (0)