git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 2/3] Improve argument parsing in cat-file
@ 2007-04-22  1:14 Shawn O. Pearce
  0 siblings, 0 replies; only message in thread
From: Shawn O. Pearce @ 2007-04-22  1:14 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

Small refactoring of the command line parsing so we can
more easily add new long options to the mix.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 builtin-cat-file.c |   27 ++++++++++++++-------------
 1 files changed, 14 insertions(+), 13 deletions(-)

diff --git a/builtin-cat-file.c b/builtin-cat-file.c
index f132d58..b2437fe 100644
--- a/builtin-cat-file.c
+++ b/builtin-cat-file.c
@@ -82,25 +82,26 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
 	enum object_type type;
 	void *buf;
 	unsigned long size;
-	int opt;
-	const char *exp_type, *obj_name;
+	int opt = 0, i;
+	const char *exp_type = NULL, *obj_name = NULL;
 
 	git_config(git_default_config);
-	if (argc != 3)
+	for (i = 1; i < argc; i++) {
+		const char *a = argv[i];
+		if (!opt && a[0] == '-' && a[1])
+			opt = a[1];
+		else if (!opt && !exp_type)
+			exp_type = a;
+		else if (!obj_name)
+			obj_name = a;
+		else
+			break;
+	}
+	if (!obj_name)
 		usage("git-cat-file [-t|-s|-e|-p|<type>] <sha1>");
-	exp_type = argv[1];
-	obj_name = argv[2];
-
 	if (get_sha1(obj_name, sha1))
 		die("Not a valid object name %s", obj_name);
 
-	opt = 0;
-	if ( exp_type[0] == '-' ) {
-		opt = exp_type[1];
-		if ( !opt || exp_type[2] )
-			opt = -1; /* Not a single character option */
-	}
-
 	buf = NULL;
 	switch (opt) {
 	case 't':
-- 
1.5.1.1.135.gf948

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2007-04-22  1:15 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-04-22  1:14 [PATCH 2/3] Improve argument parsing in cat-file Shawn O. Pearce

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).