[PATCH] Make builtin-describe use parse_options

!MAILaRCHIVE_VOTE_RePLACE
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: <git@...>, Junio C Hamano <gitster@...>
Cc: Pierre Habouzit <madcoder@...>, <git@...>
Date: Saturday, October 13, 2007 - 9:29 am

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
---
 builtin-describe.c |   76 ++++++++++++++++++++++++----------------------------
 1 files changed, 35 insertions(+), 41 deletions(-)

diff --git a/builtin-describe.c b/builtin-describe.c
index 669110c..d4e4b01 100644
--- a/builtin-describe.c
+++ b/builtin-describe.c
@@ -4,12 +4,15 @@
 #include "refs.h"
 #include "builtin.h"
 #include "exec_cmd.h"
+#include "parse-options.h"
 
 #define SEEN		(1u<<0)
 #define MAX_TAGS	(FLAG_BITS - 1)
 
-static const char describe_usage[] =
-"git-describe [--all] [--tags] [--abbrev=<n>] <committish>*";
+static const char * const describe_usage[] = {
+	"git-describe [options] <committish>*",
+	NULL
+};
 
 static int debug;	/* Display lots of verbose info */
 static int all;	/* Default to annotated tags only */
@@ -242,57 +245,48 @@ static void describe(const char *arg, int last_one)
 
 int cmd_describe(int argc, const char **argv, const char *prefix)
 {
-	int i;
+	int count;
 	int contains = 0;
+	struct option builtin_describe_options[] = {
+		OPT_BOOLEAN(0, "contains",   &contains, "find the tag that comes after the commit"),
+		OPT_BOOLEAN(0, "debug",      &debug, "debug search strategy on stderr"),
+		OPT_BOOLEAN(0, "all",        &all, "use any ref in .git/refs"),
+		OPT_BOOLEAN(0, "tags",       &tags, "use any tag in .git/refs/tags"),
+		OPT_INTEGER(0, "abbrev",     &abbrev, "use <n> digits to display sha1"),
+		OPT_INTEGER(0, "candidates", &max_candidates,
+					"consider <n> most recent tags (default: 10)"),
+	};
 
-	for (i = 1; i < argc; i++) {
-		const char *arg = argv[i];
-
-		if (*arg != '-')
-			break;
-		else if (!strcmp(arg, "--contains"))
-			contains = 1;
-		else if (!strcmp(arg, "--debug"))
-			debug = 1;
-		else if (!strcmp(arg, "--all"))
-			all = 1;
-		else if (!strcmp(arg, "--tags"))
-			tags = 1;
-		else if (!prefixcmp(arg, "--abbrev=")) {
-			abbrev = strtoul(arg + 9, NULL, 10);
-			if (abbrev != 0 && (abbrev < MINIMUM_ABBREV || 40 < abbrev))
-				abbrev = DEFAULT_ABBREV;
-		}
-		else if (!prefixcmp(arg, "--candidates=")) {
-			max_candidates = strtoul(arg + 13, NULL, 10);
-			if (max_candidates < 1)
-				max_candidates = 1;
-			else if (max_candidates > MAX_TAGS)
-				max_candidates = MAX_TAGS;
-		}
-		else
-			usage(describe_usage);
-	}
+	count = parse_options(argc, argv, builtin_describe_options,
+	                      ARRAY_SIZE(builtin_describe_options),
+	                      describe_usage, 0);
+	if (abbrev && abbrev < MINIMUM_ABBREV)
+		abbrev = MINIMUM_ABBREV;
+	else if (abbrev > 40)
+		abbrev = 40;
+	if (max_candidates < 1)
+		max_candidates = 1;
+	else if (max_candidates > MAX_TAGS)
+		max_candidates = MAX_TAGS;
 
 	save_commit_buffer = 0;
 
 	if (contains) {
-		const char **args = xmalloc((4 + argc - i) * sizeof(char*));
+		const char **args = xmalloc((4 + count) * sizeof(char*));
 		args[0] = "name-rev";
 		args[1] = "--name-only";
 		args[2] = "--tags";
-		memcpy(args + 3, argv + i, (argc - i) * sizeof(char*));
-		args[3 + argc - i] = NULL;
-		return cmd_name_rev(3 + argc - i, args, prefix);
+		memcpy(args + 3, argv, count * sizeof(char*));
+		args[3 + count] = NULL;
+		return cmd_name_rev(3 + count, args, prefix);
 	}
 
-	if (argc <= i)
+	if (count == 0) {
 		describe("HEAD", 1);
-	else
-		while (i < argc) {
-			describe(argv[i], (i == argc - 1));
-			i++;
+	} else {
+		while (count-- > 0) {
+			describe(*argv++, count == 0);
 		}
-
+	}
 	return 0;
 }
-- 
1.5.3.GIT

-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[RFC] CLI option parsing and usage generation for porcelains, Pierre Habouzit, (Sat Oct 13, 9:29 am)
[PATCH] parse-options: Allow abbreviated options when unambi..., Johannes Schindelin, (Sun Oct 14, 12:54 pm)
Re: [PATCH] parse-options: Allow abbreviated options when un..., Johannes Schindelin, (Sun Oct 14, 2:02 pm)
Re: [PATCH] parse-options: Allow abbreviated options when un..., Johannes Schindelin, (Sun Oct 14, 6:12 pm)
git-svn and submodules, was Re: [PATCH] parse-options: Allow..., Johannes Schindelin, (Sun Oct 14, 6:59 pm)
Re: git-svn and submodules, Benoit SIGOURE, (Mon Oct 15, 3:07 am)
Re: git-svn and submodules, Linus Torvalds, (Mon Oct 15, 11:53 am)
Re: git-svn and submodules, Karl , (Mon Oct 15, 10:45 am)
.gitignore and svn:ignore [WAS: git-svn and submodules], Chris Shoemaker, (Mon Oct 15, 11:14 am)
Re: .gitignore and svn:ignore [WAS: git-svn and submodules], Chris Shoemaker, (Tue Oct 16, 9:05 am)
Re: git-svn and submodules, Andreas Ericsson, (Mon Oct 15, 6:00 am)
Re: git-svn and submodules, Benoit SIGOURE, (Mon Oct 15, 6:51 am)
Re: [RFC] CLI option parsing and usage generation for porcel..., Wincent Colaiuta, (Sat Oct 13, 10:53 am)
[PATCH] Add a simple option parser., Pierre Habouzit, (Sat Oct 13, 9:29 am)
Re: [PATCH] Add a simple option parser., Alex Riesen, (Sat Oct 13, 3:16 pm)
Re: [PATCH] Add a simple option parser., Pierre Habouzit, (Sat Oct 13, 4:54 pm)
Re: [PATCH] Add a simple option parser., Alex Riesen, (Sat Oct 13, 6:14 pm)
Re: [PATCH] Add a simple option parser., Pierre Habouzit, (Sun Oct 14, 3:02 am)
Re: [PATCH] Add a simple option parser., Johannes Schindelin, (Sat Oct 13, 10:39 am)
Re: [PATCH] Add a simple option parser., Pierre Habouzit, (Sat Oct 13, 10:58 am)
[PATCH] Port builtin-add.c to use the new option parser., Pierre Habouzit, (Sat Oct 13, 9:29 am)
Re: [PATCH] Port builtin-add.c to use the new option parser., Johannes Schindelin, (Sat Oct 13, 10:47 am)
Re: [PATCH] Port builtin-add.c to use the new option parser., Pierre Habouzit, (Sat Oct 13, 11:03 am)
Re: [PATCH] Port builtin-add.c to use the new option parser., Pierre Habouzit, (Sat Oct 13, 4:27 pm)
[PATCH] Make builtin-rm use parse_options., Pierre Habouzit, (Sat Oct 13, 9:29 am)
[PATCH] Make builtin-mv use parse-options, Pierre Habouzit, (Sat Oct 13, 9:29 am)
[PATCH] Make builtin-branch.c use parse_options., Pierre Habouzit, (Sat Oct 13, 9:29 am)
[PATCH] Make builtin-describe use parse_options, Pierre Habouzit, (Sat Oct 13, 9:29 am)
[PATCH] Make git-fetch use parse_options., Pierre Habouzit, (Sat Oct 13, 9:29 am)
[PATCH] Make builtin-revert.c use parse_options., Pierre Habouzit, (Sat Oct 13, 9:29 am)
[PATCH] Make builtin-update-ref.c use parse_options, Pierre Habouzit, (Sat Oct 13, 9:29 am)
[PATCH] Simplify usage string printing, Jonas Fonseca, (Sun Oct 14, 10:01 am)
Re: [PATCH] Simplify usage string printing, Pierre Habouzit, (Sun Oct 14, 12:26 pm)
[PATCH] Make builtin-symbolic-ref.c use parse_options., Pierre Habouzit, (Sat Oct 13, 9:29 am)
[PATCH] Make builtin-http-fetch use parse_options., Pierre Habouzit, (Sat Oct 13, 9:29 am)