Skip to content

Commit d3df2cb

Browse files
Merge pull request #66 from marioortizmanero/fix-args
Fix argument parsing
2 parents ab1d8e5 + 04f2f73 commit d3df2cb

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

pulseaudio-control.bash

+27-13
Original file line numberDiff line numberDiff line change
@@ -419,23 +419,22 @@ More info on GitHub:
419419
https://github.com/marioortizmanero/polybar-pulseaudio-control"
420420
}
421421

422-
while [[ "$1" = --* ]]; do
423-
unset arg
424-
unset val
422+
# Obtains the value for an option and returns 1 if no shift is needed.
423+
function getOptVal() {
425424
if [[ "$1" = *=* ]]; then
426-
arg="${1//=*/}"
427425
val="${1//*=/}"
428-
shift
429-
else
430-
arg="$1"
431-
# Support space-separated values, but also value-less flags
432-
if [[ "$2" != --* ]]; then
433-
val="$2"
434-
shift
435-
fi
436-
shift
426+
return 1
437427
fi
438428

429+
val="$2"
430+
}
431+
432+
# Parsing the options from the arguments
433+
while [[ "$1" = --* ]]; do
434+
unset arg
435+
unset val
436+
437+
arg="$1"
439438
case "$arg" in
440439
--autosync)
441440
AUTOSYNC=yes
@@ -444,6 +443,7 @@ while [[ "$1" = --* ]]; do
444443
AUTOSYNC=no
445444
;;
446445
--color-muted|--colour-muted)
446+
if getOptVal "$@"; then shift; fi
447447
COLOR_MUTED="%{F#$val}"
448448
;;
449449
--notifications)
@@ -459,31 +459,40 @@ while [[ "$1" = --* ]]; do
459459
OSD=no
460460
;;
461461
--icon-muted)
462+
if getOptVal "$@"; then shift; fi
462463
ICON_MUTED="$val"
463464
;;
464465
--icon-sink)
466+
if getOptVal "$@"; then shift; fi
465467
# shellcheck disable=SC2034
466468
ICON_SINK="$val"
467469
;;
468470
--icons-volume)
471+
if getOptVal "$@"; then shift; fi
469472
IFS=, read -r -a ICONS_VOLUME <<< "${val//[[:space:]]/}"
470473
;;
471474
--volume-max)
475+
if getOptVal "$@"; then shift; fi
472476
VOLUME_MAX="$val"
473477
;;
474478
--volume-step)
479+
if getOptVal "$@"; then shift; fi
475480
VOLUME_STEP="$val"
476481
;;
477482
--sink-blacklist)
483+
if getOptVal "$@"; then shift; fi
478484
IFS=, read -r -a SINK_BLACKLIST <<< "${val//[[:space:]]/}"
479485
;;
480486
--sink-nicknames-from)
487+
if getOptVal "$@"; then shift; fi
481488
SINK_NICKNAMES_PROP="$val"
482489
;;
483490
--sink-nickname)
491+
if getOptVal "$@"; then shift; fi
484492
SINK_NICKNAMES["${val//:*/}"]="${val//*:}"
485493
;;
486494
--format)
495+
if getOptVal "$@"; then shift; fi
487496
FORMAT="$val"
488497
;;
489498
# Undocumented because the `help` action already exists, but makes the
@@ -497,8 +506,10 @@ while [[ "$1" = --* ]]; do
497506
exit 1
498507
;;
499508
esac
509+
shift
500510
done
501511

512+
# Parsing the action from the arguments
502513
case "$1" in
503514
up)
504515
volUp
@@ -530,6 +541,9 @@ case "$1" in
530541
help)
531542
usage
532543
;;
544+
"")
545+
echo "No action specified. Run \`$0 help\` for more information." >&2
546+
;;
533547
*)
534548
echo "Unrecognised action: $1" >&2
535549
exit 1

0 commit comments

Comments
 (0)