Created
August 11, 2021 08:11
-
-
Save raphtlw/233a591330312b8e715ca2eaa4a46a88 to your computer and use it in GitHub Desktop.
Bash script template with argument parsing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# ---------------------------------------------------------------------------- | |
# script.sh: Bash script template with argument parsing | |
# | |
# Author: Raphael Tang <[email protected]> | |
# ---------------------------------------------------------------------------- | |
# Dependencies: | |
# - dependency1 | |
# - dependency2 | |
CLEAR='\033[0m' | |
RED='\033[0;31m' | |
function usage() { | |
if [ -n "$1" ]; then | |
echo -e "${RED}👉 $1${CLEAR}\n"; | |
fi | |
cat << EOF | |
Usage: $(basename $0) [REQUIRED -u url] [-n] | |
-h, --help Show this message | |
-u, --url (Required) URL | |
-n, --nocaca Won't display colored output, uses aalib instead | |
Examples: $(basename $0) --nocaca -u "https://www.youtube.com/watch?v=5qap5aO4i9A" | |
EOF | |
} | |
# parse params | |
while [[ "$#" > 0 ]]; do case $1 in | |
-h|--help) usage; exit 0 ;; | |
-u|--url) URL="$2"; shift; shift ;; | |
-n|--nocaca) NOCACA=1; shift ;; | |
*) usage "Unknown parameter passed: $1"; exit 1 ;; | |
esac; done | |
if [ -z "$URL" ]; then | |
usage "No URL specified" | |
exit 1 | |
fi | |
if [ -n "$NOCACA" ]; then | |
youtube-dl "$URL" -f worst -o - | mplayer -vo aa -monitorpixelaspect 0.5 - | |
else | |
streamlink "$URL" worst --player mpv -a "--vo=caca" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment