git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH/RFC] parse-options.c: make OPTION__COUNTUP consider negative values
@ 2016-03-16 23:16 Pranit Bauva
  2016-03-17  1:50 ` Jeff King
  0 siblings, 1 reply; 8+ messages in thread
From: Pranit Bauva @ 2016-03-16 23:16 UTC (permalink / raw)
  To: git

The reason to make it consider negative values or more specifically
"unspecified" values is to differentiate between the option passed
once, multiple times or with --no-option. This makes the receiver
know what actually happened with the arguments which is particularly
required with option have multiple levels of that option.

Eg. :
initialize verbose = -1
`git commit` => verbose = -1
`git commit -v` => verbose = 1
`git commit -v -v` => verbose = 1
`git commit --no-verbose` => verbose = 0

Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com>

---
The discussion on the mailing list[1] suggested this approach.
I plan to include this with my previous patch regarding "configuration
for commonly used command `git commit -v`" as this is a requirement for
latter.

[1] : http://thread.gmane.org/gmane.comp.version-control.git/289027
---
 Documentation/technical/api-parse-options.txt | 6 ++++--
 parse-options.c                               | 8 +++++++-
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/Documentation/technical/api-parse-options.txt b/Documentation/technical/api-parse-options.txt
index 5f0757d..a908d8a 100644
--- a/Documentation/technical/api-parse-options.txt
+++ b/Documentation/technical/api-parse-options.txt
@@ -144,8 +144,10 @@ There are some macros to easily define options:
 
 `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`.
+	If the `int_var` is negative and `--option` is absent,
+	then it will retain its value. Otherwise it will first set
+	`int_var` to 0 and then increment it on each use of `--option`,
+	and reset to zero with `--no-option`.
 
 `OPT_BIT(short, long, &int_var, description, mask)`::
 	Introduce a boolean option.
diff --git a/parse-options.c b/parse-options.c
index 47a9192..86d30bd 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -110,7 +110,13 @@ static int get_value(struct parse_opt_ctx_t *p,
 		return 0;
 
 	case OPTION_COUNTUP:
-		*(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
+		if (unset)
+			*(int *)opt->value = 0;
+		else {
+			if (*(int *)opt->value < 0)
+				*(int *)opt->value = 0;
+			*(int *)opt->value += 1;
+		}
 		return 0;
 
 	case OPTION_SET_INT:

--
https://github.com/git/git/pull/212

^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2016-03-20  9:07 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-03-16 23:16 [PATCH/RFC] parse-options.c: make OPTION__COUNTUP consider negative values Pranit Bauva
2016-03-17  1:50 ` Jeff King
2016-03-17  7:28   ` Eric Sunshine
2016-03-17  8:14     ` Eric Sunshine
2016-03-17 20:22     ` Pranit Bauva
2016-03-18 11:23   ` Pranit Bauva
2016-03-20  4:10     ` Jeff King
2016-03-20  9:07       ` Pranit Bauva

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).