[pacman-dev] [RFC v3 03/13] rankmirrors: proper option handling
Gordian Edenhofer
gordian.edenhofer at gmail.com
Fri Sep 30 11:47:51 UTC 2016
Switch to parseopts.
Signed-off-by: Gordian Edenhofer <gordian.edenhofer at gmail.com>
---
contrib/rankmirrors.sh.in | 102 +++++++++++++++++++++-------------------------
1 file changed, 46 insertions(+), 56 deletions(-)
diff --git a/contrib/rankmirrors.sh.in b/contrib/rankmirrors.sh.in
index 7b50eb2..0988fe9 100644
--- a/contrib/rankmirrors.sh.in
+++ b/contrib/rankmirrors.sh.in
@@ -41,7 +41,6 @@ usage() {
echo " -u, --url test a specific URL"
echo " -v, --verbose be verbose in ouptut"
echo " -r, --repo specify a repository name instead of guessing"
- exit 0
}
version() {
@@ -120,68 +119,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