compton-trans: improve option parsing.
This commit is contained in:
parent
25d31a1f5e
commit
86b20e0a08
|
@ -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,8 +38,17 @@ wid=
|
||||||
topmost=
|
topmost=
|
||||||
lineno=
|
lineno=
|
||||||
|
|
||||||
|
# This takes into account the fact that getopts stops on
|
||||||
|
# any argument it doesn't recongize or errors on. This
|
||||||
|
# allows for things like `compton-trans -5` as well
|
||||||
|
# as `compton-trans -c +5 -s` (contrived example).
|
||||||
|
while test $# -gt 0; do
|
||||||
|
# Reset option index
|
||||||
|
OPTIND=1
|
||||||
|
LASTIND=1
|
||||||
|
|
||||||
# Read options
|
# Read options
|
||||||
while getopts 'scdn:w:o:' option; do
|
while getopts ':scdn:w:o:' option "$@"; do
|
||||||
case "$option" in
|
case "$option" in
|
||||||
s) wprefix=''; window='' ;;
|
s) wprefix=''; window='' ;;
|
||||||
c)
|
c)
|
||||||
|
@ -49,20 +60,29 @@ while getopts 'scdn:w:o:' option; do
|
||||||
n) wprefix='-name'; window=$OPTARG ;;
|
n) wprefix='-name'; window=$OPTARG ;;
|
||||||
w) wprefix='-id'; window=$OPTARG ;;
|
w) wprefix='-id'; window=$OPTARG ;;
|
||||||
o) opacity=$OPTARG ;;
|
o) opacity=$OPTARG ;;
|
||||||
\?) exit 1;;
|
\?)
|
||||||
|
# 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
|
esac
|
||||||
|
LASTIND=$OPTIND
|
||||||
done
|
done
|
||||||
|
|
||||||
# Read positional arguments
|
# Read positional arguments
|
||||||
shift $(($OPTIND - 1))
|
shift $((OPTIND - 1))
|
||||||
test -n "$1" && opacity=$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
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue