summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLuke Shumaker <LukeShu@sbcglobal.net>2014-01-04 19:37:33 -0500
committerLuke Shumaker <LukeShu@sbcglobal.net>2014-01-04 19:37:33 -0500
commit6eb64cb5bdce43fb5b1a83f93496083761ea2718 (patch)
tree7849e4cbb1c23b132901b082d571a62afb25b641 /src
parent0f078d1763a156993c372a49ae0f8ceefde52444 (diff)
librefetch: clean up option parsing
Diffstat (limited to 'src')
-rwxr-xr-xsrc/librefetch/librefetch74
1 files changed, 43 insertions, 31 deletions
diff --git a/src/librefetch/librefetch b/src/librefetch/librefetch
index 42253b2..eb7eb61 100755
--- a/src/librefetch/librefetch
+++ b/src/librefetch/librefetch
@@ -99,11 +99,6 @@ main() {
# Mode: checksums ######################################################
if [[ $mode =~ checksums ]]; then
- if [[ ${#extra_opts[@]} != 0 ]]; then
- print "%s: found extra non-flag arguments: %s" "$cmd" "${extra_opts[*]}" >> /dev/stderr
- usage >> /dev/stderr
- return 1
- fi
"$makepkg" "${makepkg_opts[@]}" -g -p "$srcbuild" |
case ${BUILDFILE##*/} in
PKGBUILD) sed -e 's/^[a-z]/mk&/' -e 's/^\s/ &/';;
@@ -115,42 +110,27 @@ main() {
# Mode: print ##########################################################
if [[ $mode =~ print ]]; then
- if [[ ${#extra_opts[@]} != 0 ]]; then
- print "%s: found extra non-flag arguments: %s" "$cmd" "${extra_opts[*]}" >> /dev/stderr
- usage >> /dev/stderr
- return 1
- fi
cat "$srcbuild"
return 0
fi
########################################################################
- local src dst
- case ${#extra_opts[@]} in
- 1)
- src="${extra_opts[0]}"
- dst="${src##*/}"
- ;;
- 2)
- src="${extra_opts[0]}"
- dst="${extra_opts[1]}"
- ;;
- *)
- print "%s: %d non-flag arguments found, expected 1 or 2: %s" "$cmd" ${#extra_opts[@]} >> /dev/stderr
- usage >> /dev/stderr
- return 1
- esac
+ local src="${extra_opts[0]}"
+ local dst="${extra_opts[1]:-${src##*/}}"
+ # canonicalize $src
if [[ "$src" == libre://* ]]; then
src="${MIRROR}/${src#libre://}"
fi
+ # canonicalize $dst
+ dst="$(readlink -m -- "$dst")"
+ # check to see if $src is a candidate for create mode
if [[ "$src" != "$MIRROR"* ]]; then
# inhibit create
mode=download
fi
- dst="$(readlink -m -- "$dst")" # canonicalize $dst
# Mode: download #######################################################
@@ -179,10 +159,17 @@ main() {
# sets the variables BUILDFILE, makepkg_opts, extra_opts, mode
parse_options() {
- # Detect makepkg options that take a second argument
+ # Detect makepkg options
local makepkg_orig="$(which makepkg)"
- local makepkg_opt2long=($("${makepkg_orig}" -h | sed -rn 's/\s*(--\S*) <.*/\1/p'))
- local makepkg_opt2short=($("${makepkg_orig}" -h | sed -rn 's/\s*(-.) <.*/\1/p'))
+ # --long flags that take a second argument
+ local makepkg_opt2long=( $(LC_ALL=C "${makepkg_orig}" -h | sed -rn 's/\s*(--\S*) <.*/\1/p'))
+ # -s hort flags that take a second argument
+ local makepkg_opt2short=($(LC_ALL=C "${makepkg_orig}" -h | sed -rn 's/\s*(-.) <.*/\1/p'))
+ # all flags
+ local makepkg_argall=( $(LC_ALL=C "${makepkg_orig}" -h | sed -rn \
+ -e 's/^ +(-.) .*/\1/p' \
+ -e 's/^ +(-.), (--\S*) .*/\1\n\2/p' \
+ -e 's/^ +(--\S*) .*/\1/p'))
local opt
local have_opt
@@ -207,8 +194,13 @@ parse_options() {
-p) BUILDFILE="$(readlink -m -- "$opt")";;
-h|--help) mode=help;;
-*)
- makepkg_opts+=("$arg")
- $have_opt && makepkg_opts+=("$opt")
+ if in_array "${arg}" "${makepkg_argall[@]}"; then
+ makepkg_opts+=("$arg")
+ $have_opt && makepkg_opts+=("$opt")
+ else
+ printf '%s: invalid flag: %s' "$cmd" "$arg"
+ return 1
+ fi
;;
--) shift; break;;
*) extra_opts+=("$arg");;
@@ -216,6 +208,26 @@ parse_options() {
shift
done
extra_opts+=("$@")
+
+ # check the number of extra_opts
+ case "$mode" in
+ help) # don't worry about it
+ :;;
+ checksums|print) # don't take any extra arguments
+ if [[ ${#extra_opts[@]} != 0 ]]; then
+ print "%s: found extra non-flag arguments: %s" "$cmd" "${extra_opts[*]}" >> /dev/stderr
+ usage >> /dev/stderr
+ return 1
+ fi
+ ;;
+ *download*|*create*) # take 1 or 2 extra arguments
+ if [[ ${#extra_opts[@]} != 1 ]] && [[ ${#extra_opts[@]} != 2 ]]; then
+ print "%s: %d non-flag arguments found, expected 1 or 2: %s" "$cmd" ${#extra_opts[@]} >> /dev/stderr
+ usage >> /dev/stderr
+ return 1
+ fi
+ ;;
+ esac
}
# Modify makepkg ###############################################################