Subject: [PATCH] Better option parsing for gitdiff.sh

From: Pavel Roskin <[email-masked]>
Date: Tue, 19 Apr 2005 16:19:19 -0400

Hello!

This patch improves option handling for gitdiff.sh.  Now "-p" doesn't
need to precede "-r", although all options still have to be placed
before the file names.  Also, the patch introduces a minimal usage info
for the script.

The patch is against current git-pasky.

Signed-off-by: Pavel Roskin <[email-masked]>

--- a/gitdiff.sh
+++ b/gitdiff.sh
@@ -27,6 +27,13 @@ die () {
 	exit 1
 }
 
+usage () {
+	echo "Usage:" >&2
+	echo "  gitdiff.sh -r rev1:rev2 [FILES...]" >&2
+	echo "  gitdiff.sh -r rev1 -r rev2 [FILES...]" >&2
+	exit 1
+}
+
 diffqfile () {
 	dir=$1; shift
 	file=$1; shift
@@ -59,26 +66,44 @@ diffqueue () {
 	return $ret
 }
 
-
-# FIXME: The commandline parsing is awful.
-
-if [ "$1" = "-p" ]; then
-	shift
-	parent=1
-fi
-
-if [ "$1" = "-r" ]; then
-	shift
-	id1=$(echo "$1": | cut -d : -f 1)
-	[ "$id1" != "$1" ] && id2=$(echo "$1": | cut -d : -f 2)
-	shift
-fi
-
-if [ "$1" = "-r" ]; then
-	shift
-	id2="$1"
-	shift
-fi
+# Parsing the command line
+id1_set=
+id2_set=
+while true; do
+	case $1 in
+	-p)
+		parent=1
+		shift
+		;;
+	-r)
+		if test "$id2_set"; then
+			echo "Too many revision numbers" 2>&1
+			usage
+		elif test "$id1_set"; then
+			shift
+			id2="$1"
+			id2_set=1
+			shift
+		else
+			shift
+			id1=$(echo "$1": | cut -d : -f 1)
+			id1_set=1
+			if test "$id1" != "$1"; then
+				id2=$(echo "$1": | cut -d : -f 2)
+				id2_set=1
+			fi
+			shift
+		fi
+		;;
+	-*)
+		echo "Unknown option $1" 2>&1
+		usage
+		;;
+	*)
+		break
+		;;
+	esac
+done
 
 if [ "$parent" ]; then
 	id2="$id1"

-- 
Regards,
Pavel Roskin