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 <peff@peff.net>
Helped-by: Eric Sunshine <sunshine@sunshineco.com>
Helped-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
diff --git a/Documentation/technical/api-parse-options.txt b/Documentation/technical/api-parse-options.txt
index 695bd4b..27bd701 100644
--- a/Documentation/technical/api-parse-options.txt
+++ b/Documentation/technical/api-parse-options.txt
@@ -144,8 +144,12 @@
`OPT_COUNTUP(short, long, &int_var, description)`::
Introduce a count-up option.
- `int_var` is incremented on each use of `--option`, and
- reset to zero with `--no-option`.
+ Each use of `--option` increments `int_var`, starting from zero
+ (even if initially negative), and `--no-option` resets it to
+ zero. To determine if `--option` or `--no-option` was encountered at
+ all, initialize `int_var` to a negative value, and if it is still
+ negative after parse_options(), then neither `--option` nor
+ `--no-option` was seen.
`OPT_BIT(short, long, &int_var, description, mask)`::
Introduce a boolean option.
diff --git a/parse-options.c b/parse-options.c
index 47a9192..312a85d 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -110,6 +110,8 @@
return 0;
case OPTION_COUNTUP:
+ if (*(int *)opt->value < 0)
+ *(int *)opt->value = 0;
*(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
return 0;
diff --git a/t/t0040-parse-options.sh b/t/t0040-parse-options.sh
index 717a514..fec3fef 100755
--- a/t/t0040-parse-options.sh
+++ b/t/t0040-parse-options.sh
@@ -63,7 +63,7 @@
timestamp: 0
string: (not set)
abbrev: 7
-verbose: 0
+verbose: -1
quiet: 0
dry run: no
file: (not set)
@@ -211,7 +211,7 @@
timestamp: 0
string: 123
abbrev: 7
-verbose: 0
+verbose: -1
quiet: 0
dry run: no
file: (not set)
@@ -234,7 +234,7 @@
timestamp: 0
string: (not set)
abbrev: 7
-verbose: 0
+verbose: -1
quiet: 0
dry run: no
file: (not set)
@@ -263,7 +263,7 @@
timestamp: 0
string: 123
abbrev: 7
-verbose: 0
+verbose: -1
quiet: 0
dry run: no
file: (not set)
@@ -302,7 +302,7 @@
timestamp: 0
string: (not set)
abbrev: 7
-verbose: 0
+verbose: -1
quiet: 0
dry run: no
file: (not set)
@@ -322,7 +322,7 @@
timestamp: 1
string: (not set)
abbrev: 7
-verbose: 0
+verbose: -1
quiet: 1
dry run: no
file: (not set)
@@ -344,7 +344,7 @@
timestamp: 0
string: (not set)
abbrev: 7
-verbose: 0
+verbose: -1
quiet: 0
dry run: no
file: (not set)
@@ -373,7 +373,7 @@
timestamp: 0
string: (not set)
abbrev: 7
-verbose: 0
+verbose: -1
quiet: 0
dry run: no
file: (not set)
@@ -398,7 +398,7 @@
timestamp: 0
string: (not set)
abbrev: 7
-verbose: 0
+verbose: -1
quiet: 0
dry run: no
file: (not set)
@@ -429,7 +429,7 @@
timestamp: 0
string: (not set)
abbrev: 7
-verbose: 0
+verbose: -1
quiet: 0
dry run: no
file: (not set)
@@ -448,7 +448,7 @@
timestamp: 0
string: (not set)
abbrev: 7
-verbose: 0
+verbose: -1
quiet: 0
dry run: no
file: (not set)
@@ -483,7 +483,7 @@
timestamp: 0
string: (not set)
abbrev: 7
-verbose: 0
+verbose: -1
quiet: 3
dry run: no
file: (not set)
@@ -521,7 +521,7 @@
timestamp: 0
string: (not set)
abbrev: 7
-verbose: 0
+verbose: -1
quiet: 0
dry run: no
file: (not set)
@@ -540,7 +540,7 @@
timestamp: 0
string: (not set)
abbrev: 7
-verbose: 0
+verbose: -1
quiet: 0
dry run: no
file: (not set)
diff --git a/test-parse-options.c b/test-parse-options.c
index 86afa98..f02c275 100644
--- a/test-parse-options.c
+++ b/test-parse-options.c
@@ -7,7 +7,8 @@
static unsigned long magnitude = 0;
static unsigned long timestamp;
static int abbrev = 7;
-static int verbose = 0, dry_run = 0, quiet = 0;
+static int verbose = -1; /* unspecified */
+static int dry_run = 0, quiet = 0;
static char *string = NULL;
static char *file = NULL;
static int ambiguous;