planime: Use getopts for argument parsing.
authorFredrik Tolf <fredrik@dolda2000.com>
Sun, 3 Jan 2021 15:52:59 +0000 (16:52 +0100)
committerFredrik Tolf <fredrik@dolda2000.com>
Sun, 3 Jan 2021 15:52:59 +0000 (16:52 +0100)
planime

diff --git a/planime b/planime
index 854ef7f..9aea34f 100755 (executable)
--- a/planime
+++ b/planime
@@ -76,63 +76,64 @@ debug=
 log=y
 unset pretend printfile
 
 log=y
 unset pretend printfile
 
-while [ "${1:0:1}" = - ]; do
-    a="$1"
-    shift
-    case "$a" in
-       -h)
-           echo "usage: planime [-fdhtCL] [-A PLAYER-ARGS... ;] [-s PAR VAL] [--] [NAME-QUAL] [EP|.] [TYPE-QUAL]" >&2
+usage() {
+    echo "usage: planime [-fdhtCL] [-A PLAYER-ARGS... ;] [-s PAR=VAL] [--] [NAME-QUAL] [EP|.] [TYPE-QUAL]"
+}
+
+while getopts hdtCLA:s:fv opt; do
+    case "$opt" in
+       h)
+           usage
            exit 0
            ;;
            exit 0
            ;;
-       -d)
+       d)
            debug=y
            ;;
            debug=y
            ;;
-       -t)
+       t)
            cmdline=("${cmdline[@]}" -audio-device 'alsa/hdmi:CARD=PCH,DEV=0')
            DISPLAY=:1
            chwp=y
            ;;
            cmdline=("${cmdline[@]}" -audio-device 'alsa/hdmi:CARD=PCH,DEV=0')
            DISPLAY=:1
            chwp=y
            ;;
-       -C)
+       C)
            pretend=y
            ;;
            pretend=y
            ;;
-       -L)
+       L)
            log=
            ;;
            log=
            ;;
-       -A)
-           while :; do
-               if [ $# -lt 1 ]; then
+       A)
+           while [ "$OPTARG" != \; ]; do
+               cmdline+=("$OPTARG")
+               if [ "$OPTIND" -gt $# ]; then
                    echo "planime: unterminated argument list" >&2
                    exit 1
                fi
                    echo "planime: unterminated argument list" >&2
                    exit 1
                fi
-               a="$1"
-               shift
-               if [ "$a" = \; ]; then
-                   break;
-               fi
-               cmdline=("${cmdline[@]}" "$a")
+               OPTARG="${!OPTIND}"
+               let OPTIND++
            done
            ;;
            done
            ;;
-       -s)
-           savepar="$1"
-           shift
-           saveval="$1"
-           shift
+       s)
+           p="$(expr index "$OPTARG" =)"
+           if [ "$p" -le 0 ]; then
+               usage >&2
+               exit 1
+           fi
+           savepar="${OPTARG:0:p}"
+           let p++
+           saveval="${OPTARG:p}"
            ;;
            ;;
-       -f)
+       f)
            printfile=y
            ;;
            printfile=y
            ;;
-       -v)
-           cmdline=("${cmdline[@]}" -v)
-           ;;
-       --)
-           break
+       v)
+           cmdline+=(-v)
            ;;
        *)
            ;;
        *)
-           echo "planime: unknown option \`$a'"
+           usage >&2
            exit 1
            ;;
     esac
 done
            exit 1
            ;;
     esac
 done
+shift $((OPTIND - 1))
 
 dirbase="$(basename "$(pwd)")"
 base=.
 
 dirbase="$(basename "$(pwd)")"
 base=.