Subject: [PATCH 1/3] rev-parse: support OPT_NUMBER_CALLBACK in --parseopt

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Sun, 2 Mar 2014 09:53:12 +0700

If the option spec is

-NUM Help string

then rev-parse will accept and parse -([0-9]+) and return "-NUM $1"

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 builtin/rev-parse.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c
index 45901df..b37676f 100644
--- a/builtin/rev-parse.c
+++ b/builtin/rev-parse.c
@@ -331,6 +331,8 @@ static int parseopt_dump(const struct option *o, const char *arg, int unset)
 	struct strbuf *parsed = o->value;
 	if (unset)
 		strbuf_addf(parsed, " --no-%s", o->long_name);
+	else if (o->type == OPTION_NUMBER)
+		strbuf_addf(parsed, " -NUM");
 	else if (o->short_name && (o->long_name == NULL || !stuck_long))
 		strbuf_addf(parsed, " -%c", o->short_name);
 	else
@@ -338,7 +340,7 @@ static int parseopt_dump(const struct option *o, const char *arg, int unset)
 	if (arg) {
 		if (!stuck_long)
 			strbuf_addch(parsed, ' ');
-		else if (o->long_name)
+		else if (o->long_name || o->type == OPTION_NUMBER)
 			strbuf_addch(parsed, '=');
 		sq_quote_buf(parsed, arg);
 	}
@@ -439,7 +441,10 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
 
 		if (s - sb.buf == 1) /* short option only */
 			o->short_name = *sb.buf;
-		else if (sb.buf[1] != ',') /* long option only */
+		else if (s - sb.buf == 4 && !strncmp(sb.buf, "-NUM", 4)) {
+			o->type = OPTION_NUMBER;
+			o->flags = PARSE_OPT_NOARG | PARSE_OPT_NONEG;
+		} else if (sb.buf[1] != ',') /* long option only */
 			o->long_name = xmemdupz(sb.buf, s - sb.buf);
 		else {
 			o->short_name = *sb.buf;
-- 
1.9.0.40.gaa8c3ea