Merge branch 'feature/getopt' into develop
diff --git a/.gitmodules b/.gitmodules
new file mode 100644
index 0000000..8566567
--- /dev/null
+++ b/.gitmodules
@@ -0,0 +1,3 @@
+[submodule "shFlags"]
+	path = shFlags
+	url = git://github.com/nvie/shFlags.git
diff --git a/git-flow b/git-flow
index 5be05f4..303e8a7 100755
--- a/git-flow
+++ b/git-flow
@@ -53,6 +53,13 @@
 		exit 1
 	fi
 
+	# use the shFlags project to parse the command line arguments
+	. "$GITFLOW_DIR/shFlags/src/shFlags"
+	#DEFINE_boolean quiet 0 'run without output' q
+	#DEFINE_boolean verbose 0 'run verbose (more output)' v
+	FLAGS "$@" || exit $?
+	eval set -- "${FLAGS_ARGV}"
+
 	# sanity checks
 	SUBCOMMAND="$1"; shift
 
@@ -73,7 +80,13 @@
 	# run command
 	. "$GITFLOW_DIR/git-flow-$SUBCOMMAND"
 
-	SUBACTION="${1:-default}"; shift
+	# test if the first argument is a flag (i.e. starts with '-')
+	# in that case, we interpret this arg as a flag for the default
+	# command
+	SUBACTION="default"
+	if [ "$1" != "" ] && ! echo "$1" | grep -q "^-"; then
+		SUBACTION="$1"; shift
+	fi
 	if ! typeset -f cmd_$SUBACTION 2>&1 >/dev/null; then
 		warn "Unknown subcommand: '$1'"
 		usage
diff --git a/git-flow-feature b/git-flow-feature
index 26f647a..5e42b15 100644
--- a/git-flow-feature
+++ b/git-flow-feature
@@ -13,7 +13,6 @@
 #
 
 PREFIX=$(git config --get gitflow.prefix.feature || echo feature/)
-FLAG_FETCH=0
 
 usage() {
 	echo "usage: git flow feature [list]"
@@ -42,6 +41,9 @@
 }
 
 cmd_list() {
+	DEFINE_boolean verbose 0 'verbose (more) output' v
+	parse_args "$@"
+
 	FEATURE_BRANCHES="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX")"
 	if [ -z "$FEATURE_BRANCHES" ]; then
 		warn "No feature branches exist."
@@ -64,63 +66,63 @@
 
 	MATCHES="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX$1")"
 	NUM_MATCHES=$(echo "$MATCHES" | wc -l)
-	if [ $NUM_MATCHES -eq 1 ]; then
-		# sed arg looks a bit weird, but $PREFIX should not contain spaces,
-		# so this one is safe
-		echo "$MATCHES" | sed "s $PREFIX  g"
-	elif [ $NUM_MATCHES -eq 0 ]; then
+	if [ -z "$MATCHES" ]; then
 		# no prefix match, so take it literally
 		echo "$1"
 	else
-		# multiple matches, cannot decide
-		warn "Multiple branches match for prefix '$1':"
-		for match in $MATCHES; do
-			warn "- $match"
-		done
-		die "Aborting. Use an unambiguous prefix."
+		if [ $NUM_MATCHES -eq 1 ]; then
+			# sed arg looks a bit weird, but $PREFIX should not contain spaces,
+			# so this one is safe
+			echo "$MATCHES" | sed "s $PREFIX  g"
+		else
+			# multiple matches, cannot decide
+			warn "Multiple branches match for prefix '$1':"
+			for match in $MATCHES; do
+				warn "- $match"
+			done
+			die "Aborting. Use an unambiguous prefix."
+		fi
 	fi
 }
 
-get_name_by_prefix() {
-	NAME=$(resolve_name_by_prefix "$1")
-	if [ -z "$NAME" ]; then
+require_name() {
+	if [ "$NAME" = "" ]; then
+		echo "Missing argument <name>"
+		usage
 		exit 1
 	fi
 }
 
-parse_args_common() {
-	# TODO: When we have a nice structured way of parsing flags with getopt,
-	# implement the following flags:
-	# --fetch, to set FLAG_FETCH=1
-	# --no-fetch, to set FLAG_FETCH=0
-	BASE="${2:-$DEVELOP_BRANCH}"
-	if [ "$NAME" = "" ]; then
-		echo "Missing argument <name>."
-		usage
+expand_name_arg_prefix() {
+	require_name
+	NAME=$(resolve_name_by_prefix "$NAME")
+	if echo "$NAME" | grep -q "^[ \t\n\r]+$"; then
 		exit 1
 	fi
 	BRANCH=$PREFIX$NAME
 }
 
-parse_args_with_name_prefix() {
-	get_name_by_prefix "$1"
-	parse_args_common
-}
-
 parse_args() {
+	# parse options
+	FLAGS "$@" || exit $?
+	eval set -- "${FLAGS_ARGV}"
+
+	# read arguments into global variables
 	NAME="$1"
-	parse_args_common
+	BASE="${2:-$DEVELOP_BRANCH}"
 }
 
 cmd_start() {
+	DEFINE_boolean fetch 0 'fetch from origin before performing local operation' F
 	parse_args "$@"
+	require_name
 
 	# sanity checks
 	gitflow_require_clean_working_tree
 	gitflow_require_branch_absent $BRANCH
 
 	# update the local repo with remote changes, if asked
-	if [ $FLAG_FETCH -eq 1 ]; then
+	if [ $FLAGS_fetch -eq 1 ]; then
 		git fetch -q $ORIGIN $DEVELOP_BRANCH
 	fi
 
@@ -141,7 +143,11 @@
 }
 
 cmd_finish() {
-	parse_args_with_name_prefix "$@"
+	DEFINE_boolean fetch 0 'fetch from origin before performing local operation' F
+	DEFINE_boolean rebase 0 'rebase instead of merge' r
+	DEFINE_boolean squash 0 'squash all commits when rebasing (implies --rebase)' s
+	parse_args "$@"
+	expand_name_arg_prefix
 
 	# sanity checks
 	gitflow_require_branch $BRANCH
@@ -190,7 +196,7 @@
 	gitflow_require_clean_working_tree
 
 	# update local repo with remote changes first, if asked
-	if [ $FLAG_FETCH -eq 1 ]; then
+	if [ $FLAGS_fetch -eq 1 ]; then
 		git fetch -q $ORIGIN $BRANCH
 	fi
 
@@ -249,6 +255,7 @@
 
 cmd_publish() {
 	parse_args "$@"
+	expand_name_arg_prefix
 
 	# sanity checks
 	gitflow_require_clean_working_tree
@@ -275,6 +282,7 @@
 
 cmd_track() {
 	parse_args "$@"
+	require_name
 
 	# sanity checks
 	gitflow_require_clean_working_tree
@@ -293,7 +301,9 @@
 }
 
 cmd_diff() {
-	parse_args_with_name_prefix "$@"
+	parse_args "$@"
+	expand_name_arg_prefix 
+
 	# TODO: if this feature has been based on a non-develop branch, we really
 	# should not be comparing to $DEVELOP. How to deal with this?
 	git diff $DEVELOP_BRANCH..$BRANCH
diff --git a/shFlags b/shFlags
new file mode 160000
index 0000000..2fb06af
--- /dev/null
+++ b/shFlags
@@ -0,0 +1 @@
+Subproject commit 2fb06af13de884e9680f14a00c82e52a67c867f1