compton-trans: improve option parsing.

This commit is contained in:
Christopher Jeffrey 2013-05-04 14:29:10 -05:00
parent 25d31a1f5e
commit 86b20e0a08
1 changed files with 41 additions and 21 deletions

View File

@ -7,6 +7,7 @@
# #
# Usage: # Usage:
# $ compton-trans [options] [+|-]opacity
# By window id # By window id
# $ compton-trans -w "$WINDOWID" 75 # $ compton-trans -w "$WINDOWID" 75
# By name # By name
@ -26,6 +27,7 @@ if test -z "$(command -v xprop)" -o -z "$(command -v xwininfo)"; then
fi fi
# Variables # Variables
active=
wprefix= wprefix=
window= window=
opacity= opacity=
@ -36,33 +38,51 @@ wid=
topmost= topmost=
lineno= lineno=
# Read options # This takes into account the fact that getopts stops on
while getopts 'scdn:w:o:' option; do # any argument it doesn't recongize or errors on. This
case "$option" in # allows for things like `compton-trans -5` as well
s) wprefix=''; window='' ;; # as `compton-trans -c +5 -s` (contrived example).
c) while test $# -gt 0; do
active=$(xprop -root -notype _NET_ACTIVE_WINDOW \ # Reset option index
| sed 's/^.*\(0x\S*\).*$/\1/') OPTIND=1
wprefix='-id'; window=$active LASTIND=1
;;
d) delete=1 ;;
n) wprefix='-name'; window=$OPTARG ;;
w) wprefix='-id'; window=$OPTARG ;;
o) opacity=$OPTARG ;;
\?) exit 1;;
esac
done
# Read positional arguments # Read options
shift $(($OPTIND - 1)) while getopts ':scdn:w:o:' option "$@"; do
test -n "$1" && opacity=$1 case "$option" in
s) wprefix=''; window='' ;;
c)
active=$(xprop -root -notype _NET_ACTIVE_WINDOW \
| sed 's/^.*\(0x\S*\).*$/\1/')
wprefix='-id'; window=$active
;;
d) delete=1 ;;
n) wprefix='-name'; window=$OPTARG ;;
w) wprefix='-id'; window=$OPTARG ;;
o) opacity=$OPTARG ;;
\?)
# For some reason single char options `-x` stop on the OPTIND after the
# argument index, whereas two or more character options (`-foo`) stop
# on the argument's index, so we use LASTIND here instead. If OPTIND is
# not equal to LASTIND, this means -x was used instead of -xx.
OPTIND=$LASTIND
break
;;
esac
LASTIND=$OPTIND
done
# Read positional arguments
shift $((OPTIND - 1))
test -n "$1" && opacity=$1 && shift
done
# clean up opacity. xargs == a poor man's trim. # clean up opacity. xargs == a poor man's trim.
opacity=$(echo "$opacity" | sed 's/%//g' | xargs) opacity=$(echo "$opacity" | sed 's/%//g' | xargs)
# Validate opacity value # Validate opacity value
if test -z "$opacity" -a -z "$delete"; then if test -z "$delete" && ! echo "$opacity" | grep -q '^[+-]\?[0-9]\+$'; then
echo 'No opacity specified.' echo "Invalid opacity specified: $opacity."
exit 1 exit 1
fi fi