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