Better argument handling; cleanup

This commit is contained in:
Jack L. Frost 2015-02-10 16:45:15 +03:00
parent fcb4c6bdb0
commit 71f6a3e256

58
sup
View File

@ -34,7 +34,7 @@ sup.upload() {
-F submit="" \ -F submit="" \
"$cfg_script_url" -sL -A 'zerofiles.org upload script' || { return 1; } "$cfg_script_url" -sL -A 'zerofiles.org upload script' || { return 1; }
[[ "$flag_rm" ]] && { rm "$file"; } (( "$flag_rm" )) && { rm "$file"; }
return 0 return 0
} }
@ -57,7 +57,7 @@ sup.scrot() {
return 1 return 1
} }
[[ "$flag_scrot_fullscreen" ]] || { scrot_args+=( '-s' ); } (( "$flag_scrot_fullscreen" )) || { scrot_args+=( '-s' ); }
[[ "$cfg_scrot_delay" ]] && { scrot_args+=( "-d $cfg_scrot_delay" ); } [[ "$cfg_scrot_delay" ]] && { scrot_args+=( "-d $cfg_scrot_delay" ); }
tmp_file="$(sup.mktemp "${cfg_tmp_dir}" ".${cfg_screenshot_ext}")" tmp_file="$(sup.mktemp "${cfg_tmp_dir}" ".${cfg_screenshot_ext}")"
@ -92,32 +92,43 @@ sup.mktemp() {
echo "${tmp_dir}/${tmp_file_name}" echo "${tmp_dir}/${tmp_file_name}"
} }
sup.getopt() { set_argv() {
local keys declare arg opt c
declare -g argv
for i in "$@"; do while (( $# )); do
case "$i" in unset -v arg opt c
--*) echo "$i";;
-*) case "$1" in
keys=( $(echo "${i##*-}" | fold -w1) ) (--) argv+=( "$1" ); break;;
if [[ "${#keys[@]}" -gt 1 ]]; then (--*)
for k in "${keys[@]}"; do IFS='=' read arg opt <<< "$1"
echo "-${k}" argv+=( "$arg" )
done
else [[ "$opt" ]] && {
echo "$i" argv+=( "$opt" )
fi }
;; ;;
*) echo "$i";; (-*)
while read -n1 c
do
case "$c" in
-|'') :;;
*) argv+=( "-$c" );;
esac esac
done <<< "$1"
;;
(*) argv+=( "$1" );;
esac
shift
done done
} }
main() { main() {
local args file target flag_private flag_rm flag_scrot flag_scrot_fullscreen cfg_scrot_delay declare args file target flag_private flag_rm flag_scrot flag_scrot_fullscreen cfg_scrot_delay
while [[ "$1" ]]; do while [[ "$1" ]]; do
case "$1" in case "$1" in
@ -130,19 +141,19 @@ main() {
-h|--help|--usage) sup.usage; return;; -h|--help|--usage) sup.usage; return;;
--) break;; --) shift; break;;
*) args+=( "$1" );; *) break;;
esac esac
shift shift
done done
sup.env sup.env
if [[ "$flag_scrot" ]]; then if (( flag_scrot )); then
file="$(sup.scrot)" || { return 1; } file="$(sup.scrot)" || { return 1; }
else else
target="${args[0]}"; [[ "$target" ]] || { sup.usage; exit 1; } target="$1"; [[ "$target" ]] || { sup.usage; exit 1; }
if sup.if_url "$target"; then if sup.if_url "$target"; then
file=$(sup.mktemp "$cfg_tmp_dir") file=$(sup.mktemp "$cfg_tmp_dir")
@ -159,4 +170,5 @@ main() {
sup.upload sup.upload
} }
main $(sup.getopt "$@") set_argv "$@"
main "${argv[@]}"