Better argument handling.

This commit is contained in:
Jack L. Frost 2015-01-31 18:45:47 +03:00
parent aa8929ff8e
commit 7d778ecd66
1 changed files with 49 additions and 8 deletions

57
zpac
View File

@ -18,10 +18,10 @@ usage() {
Flags:
-h Show this message.
-s Search for <package>.
-S Search only in the sync db. Implies -s.
-A Search only in AUR. Implies -s.
-S Search only in the sync db. Implies -s.
-A Search only in AUR. Implies -s.
-d Get the package sources (default).
-D Get the dependencies also. Implies -d.
Set twice to also get the dependencies.
EOF
}
@ -112,6 +112,44 @@ abs.get() {
svn checkout "svn://svn.archlinux.org/${svn_repo}/${1}/trunk" "${cfg_workdir}/${1}"
}
args_norm() {
while (( $# )); do
unset -v arg opt c keys
case "$1" in
--) shift; break;;
--*)
IFS='=' read arg opt <<< "$1"
printf '%s\n' "$arg"
[[ "$opt" ]] && {
printf '%s\n' "$opt"
}
;;
-*)
while read -n1 c
do
case "$c" in
-|'') :;;
*) keys+=( "-$c" );;
esac
done <<< "$1"
printf '%s\n' "${keys[@]}"
;;
esac
[[ "$2" && ! "$2" =~ ^- ]] && {
printf '%s\n' "$2"
shift
}
shift
done
}
main() {
if [[ -f "$HOME/.config/zpac.rc.sh" ]]; then
source "$HOME/.config/zpac.rc.sh"
@ -129,10 +167,13 @@ main() {
action='search'
flag_search_syncdb=1;;
(-d) action='get';;
(-D)
action='get'
flag_get_deps=1;;
(-d)
if [[ "$action" == 'get' ]]; then
flag_get_deps=1
else
action='get'
fi
;;
(--) shift; break;;
(-*)
@ -224,4 +265,4 @@ main() {
esac
}
main "$@"
main $(args_norm "$@")