Option parsing bug fixes and error checking for dlaudio

This commit is contained in:
Eric Torres 2018-09-15 12:34:58 -07:00
parent d0204ba256
commit 70e3b6e2f6

View File

@ -1,21 +1,6 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Download audio using youtube-dl, passing # Download audio using youtube-dl, passing
# a specific set of options specified by the user # a specific set of options specified by the user
#
# Copyright (C) 2018 Eric Torres
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
printHelp() { printHelp() {
cat << EOF cat << EOF
@ -29,16 +14,16 @@ Options:
EOF EOF
} }
declare -a opts declare -a opts
batchfile= batchfile=
filename= filename=
declare -r default_filename="--output=${HOME}/Music/%(title)s.%(ext)s"
format= format=
# error messages # error messages
readonly nobatchfile_error="Error: no batch file entered" declare -r nobatchfile_error="Error: no batch file entered"
readonly nofilename_error="Error: no filename entered" declare -r nofilename_error="Error: no filename entered"
readonly noformat_error="Error: no format entered" declare -r noformat_error="Error: no format entered"
while true; do while true; do
case "${1}" in case "${1}" in
@ -60,8 +45,8 @@ while true; do
shift 2 shift 2
continue continue
;; ;;
--batchfile=*) --batch-dl=*)
batchfile="${2#*=}" batchfile="${1#*=}"
case "${batchfile}" in case "${batchfile}" in
"") "")
echo "${nobatchfile_error}" >&2 echo "${nobatchfile_error}" >&2
@ -129,7 +114,7 @@ while true; do
continue continue
;; ;;
--filename=*) --filename=*)
filename="${2#*=}" filename="${1#*=}"
case "${filename}" in case "${filename}" in
"") "")
echo "${nofilename_error}" >&2 echo "${nofilename_error}" >&2
@ -156,23 +141,26 @@ while true; do
esac esac
done done
[[ -z "${*}" ]] && echo "No input given" >&2 && exit 1
# default options # default options
opts+=("--no-part") opts+=("--no-part")
opts+=("--no-continue") opts+=("--no-continue")
opts+=("--extract-audio") opts+=("--extract-audio")
opts+=("--audio-format=${format:-flac}") opts+=("--audio-format=${format:-flac}")
# set filename to either what the user set or its original title # filename cannot be used at the same time as batch-dl
if [[ "${filename}" ]]; then if [[ "${filename}" && -z "${batchfile}" ]]; then
opts+=("--output=${HOME}/Music/${filename}.%(ext)s") opts+=("--output=${HOME}/Music/${filename}.%(ext)s")
else elif [[ "${batchfile}" && -z "${filename}" ]]; then
opts+=("--output=${HOME}/Music/%(title)s.%(ext)s") opts+=("${default_filename}")
elif [[ "${batchfile}" && "${filename}" ]]; then
printf '%s\n' "Cannot pass '--batch-dl' and '--filename' together, ignoring"
opts+=("${default_filename}")
fi fi
if [[ "${batchfile}" ]]; then if [[ "${batchfile}" ]]; then
youtube-dl "${opts[@]}" --batch-file="${batchfile}" youtube-dl "${opts[@]}" --batch-file="${batchfile}"
else elif [[ "${*}" ]]; then
youtube-dl "${opts[@]}" "${@}" youtube-dl "${opts[@]}" "${@}"
else
printf '%s\n' "No arguments entered, cancelling"
fi fi