[pacman-dev] [PATCH 11/11] rankmirrors: proper option handling

Gordian Edenhofer gordian.edenhofer at gmail.com
Thu Sep 29 10:23:11 UTC 2016


Switch to parseopts.

Signed-off-by: Gordian Edenhofer <gordian.edenhofer at gmail.com>
---
 contrib/rankmirrors.sh.in | 104 +++++++++++++++++++++-------------------------
 1 file changed, 48 insertions(+), 56 deletions(-)

diff --git a/contrib/rankmirrors.sh.in b/contrib/rankmirrors.sh.in
index 6ded91f..c0f2316 100644
--- a/contrib/rankmirrors.sh.in
+++ b/contrib/rankmirrors.sh.in
@@ -24,6 +24,8 @@ trap finaloutput INT
 declare -r myname='rankmirrors'
 declare -r myver='@PACKAGE_VERSION@'
 
+m4_include(../scripts/library/parseopts.sh)
+
 usage() {
 	printf "%s (pacman) %s\n" "$myname" "$myver"
 	echo
@@ -43,7 +45,6 @@ provided")\n"
 	printf -- "$(gettext "  -r, --repo <repo>  Specify a repository name instead of guessing")\n"
 	printf -- "$(gettext "  --version          Show program's version number and exit")\n"
 	echo
-	exit 0
 }
 
 version() {
@@ -122,68 +123,59 @@ finaloutput() {
 	exit 0
 }
 
-
-# Argument parsing
-[[ $1 ]] || usage
-while [[ $1 ]]; do
-	if [[ ${1:0:2} = -- ]]; then
-		case "${1:2}" in
-			help) usage ;;
-			version) version ;;
-			times) TIMESONLY=1 ; shift ;;
-			verbose) VERBOSE=1 ; shift ;;
-			url)
-				CHECKURL=1;
-				[[ $2 ]] || err "Must specify URL.";
-				URL="$2";
-				shift 2;;
-			repo)
-				[[ $2 ]] || err "Must specify repository name.";
-				TARGETREPO="$2";
-				shift 2;;
-			*) err "'$1' is an invalid argument."
-		esac
-	elif [[ ${1:0:1} = - ]]; then
-
-		if [[ ! ${1:1:1} ]]; then
-			[[ -t 0 ]] && err "Stdin is empty."
-			IFS=$'\n' linearray=( $(</dev/stdin) )
-			STDIN=1
+# Parse arguments
+OPT_SHORT=':hVtvn:u:r:'
+OPT_LONG=('out:' 'jobs:' 'quiet' 'nocolor' 'pacnew' 'version')
+if ! parseopts "$OPT_SHORT" "${OPT_LONG[@]}" -- "$@"; then
+	usage
+	exit 1
+fi
+set -- "${OPTRET[@]}"
+unset OPT_SHORT OPT_LONG OPTRET
+
+while :; do
+	case "$1" in
+		-h|--help)
+			usage
+			exit 0;;
+		--version)
+			version;;
+		-t|--times)
+			TIMESONLY=1;;
+		-v|--verbose)
+			VERBOSE=1;;
+		-u|--url)
+			CHECKURL=1
+			URL="$2"
+			shift;;
+		-r|--repo)
+			TARGETREPO="$2"
+			shift;;
+		-n)
+			NUM="$2"
+			shift;;
+		--)
 			shift
-		else
-			snum=1
-			for ((i=1 ; i<${#1}; i++)); do
-				case ${1:$i:1} in
-					h) usage ;;
-					t) TIMESONLY=1 ;;
-					v) VERBOSE=1 ;;
-					u)
-						CHECKURL=1;
-						[[ $2 ]] || err "Must specify URL.";
-						URL="$2";
-						snum=2;;
-					r)
-						[[ $2 ]] || err "Must specify repository name.";
-						TARGETREPO="$2";
-						snum=2;;
-					n)
-						[[ $2 ]] || err "Must specify number.";
-						NUM="$2";
-						snum=2;;
-					*) err "'$1' is an invalid argument." ;;
-				esac
-			done
-			shift $snum
-		fi
+			break 2;;
+	esac
+	shift
+done
+if [[ ${#@} -eq 1 ]]; then
+	if [[ $1 == "-" ]]; then
+		[[ -t 0 ]] && err "Stdin is empty."
+		IFS=$'\n' linearray=( $(</dev/stdin) )
+		STDIN=1
 	elif [[ -f $1 ]]; then
 		FILE="1"
 		IFS=$'\n' linearray=( $(<$1) )
 		[[ $linearray ]] || err "File is empty."
-		shift
-	else
+	elif [[ ! -f $1 ]]; then
 		err "'$1' does not exist."
 	fi
-done
+else
+	usage
+	exit 1
+fi
 
 # Some sanity checks
 [[ $NUM ]] || NUM=0
-- 
2.10.0


More information about the pacman-dev mailing list