[PATCH] Make builtin-mv 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-mv.c |   55 ++++++++++++++++++++++---------------------------------
 1 files changed, 22 insertions(+), 33 deletions(-)

diff --git a/builtin-mv.c b/builtin-mv.c
index b944651..17c3655 100644
--- a/builtin-mv.c
+++ b/builtin-mv.c
@@ -8,9 +8,12 @@
 #include "dir.h"
 #include "cache-tree.h"
 #include "path-list.h"
+#include "parse-options.h"
 
-static const char builtin_mv_usage[] =
-"git-mv [-n] [-f] (<source> <destination> | [-k] <source>... <destination>)";
+static const char * const builtin_mv_usage[] = {
+	"git-mv [options] <source>... <destination>",
+	NULL
+};
 
 static const char **copy_pathspec(const char *prefix, const char **pathspec,
 				  int count, int base_name)
@@ -63,6 +66,11 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 {
 	int i, newfd, count;
 	int verbose = 0, show_only = 0, force = 0, ignore_errors = 0;
+	struct option builtin_mv_options[] = {
+		OPT_BOOLEAN('n', NULL, &show_only,"dry-run"),
+		OPT_BOOLEAN('f', NULL, &force, "force move/rename even if target exists"),
+		OPT_BOOLEAN('k', NULL, &ignore_errors, "skip move/rename errors"),
+	};
 	const char **source, **destination, **dest_path;
 	enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes;
 	struct stat st;
@@ -78,47 +86,28 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 	if (read_cache() < 0)
 		die("index file corrupt");
 
-	for (i = 1; i < argc; i++) {
-		const char *arg = argv[i];
+	count = parse_options(argc, argv, builtin_mv_options,
+	                      ARRAY_SIZE(builtin_mv_options),
+	                      builtin_mv_usage, 0);
+	if (--count < 1)
+		make_usage(builtin_mv_usage, builtin_mv_options,
+				   ARRAY_SIZE(builtin_mv_options));
 
-		if (arg[0] != '-')
-			break;
-		if (!strcmp(arg, "--")) {
-			i++;
-			break;
-		}
-		if (!strcmp(arg, "-n")) {
-			show_only = 1;
-			continue;
-		}
-		if (!strcmp(arg, "-f")) {
-			force = 1;
-			continue;
-		}
-		if (!strcmp(arg, "-k")) {
-			ignore_errors = 1;
-			continue;
-		}
-		usage(builtin_mv_usage);
-	}
-	count = argc - i - 1;
-	if (count < 1)
-		usage(builtin_mv_usage);
-
-	source = copy_pathspec(prefix, argv + i, count, 0);
+	source = copy_pathspec(prefix, argv, count, 0);
 	modes = xcalloc(count, sizeof(enum update_mode));
-	dest_path = copy_pathspec(prefix, argv + argc - 1, 1, 0);
+	dest_path = copy_pathspec(prefix, argv + count, 1, 0);
 
 	if (dest_path[0][0] == '\0')
 		/* special case: "." was normalized to "" */
-		destination = copy_pathspec(dest_path[0], argv + i, count, 1);
+		destination = copy_pathspec(dest_path[0], argv, count, 1);
 	else if (!lstat(dest_path[0], &st) &&
 			S_ISDIR(st.st_mode)) {
 		dest_path[0] = add_slash(dest_path[0]);
-		destination = copy_pathspec(dest_path[0], argv + i, count, 1);
+		destination = copy_pathspec(dest_path[0], argv, count, 1);
 	} else {
 		if (count != 1)
-			usage(builtin_mv_usage);
+			make_usage(builtin_mv_usage, builtin_mv_options,
+					   ARRAY_SIZE(builtin_mv_options));
 		destination = dest_path;
 	}
 
-- 
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)