From 31889fa4042f3d3cb1572a2758c0fa5b954514d3 Mon Sep 17 00:00:00 2001 From: Euro20179 Date: Tue, 2 Mar 2021 12:47:22 -0800 Subject: option-parsing-longopts --- ytfzf | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 82 insertions(+), 21 deletions(-) diff --git a/ytfzf b/ytfzf index 2bc603c..b9f63ea 100755 --- a/ytfzf +++ b/ytfzf @@ -45,26 +45,27 @@ dep_ck "jq" "youtube-dl" "$YTFZF_PLAYER"; helpinfo () { printf "Usage: %bytfzf [OPTIONS] %b%b\n" "\033[1;32m" "\033[1;33m" "\033[0m"; printf " OPTIONS:\n" -printf " -h Show this help text\n"; -printf " -t Show thumbnails (requires ueberzug)\n"; -printf " Doesn't work with -H -D\n"; -printf " -D Use external menu(default dmenu) instead of fzf \n"; -printf " -H Choose from history \n"; -printf " -x Delete history\n"; -printf " -m Audio only (for music)\n"; -printf " -d Download to current directory\n"; -printf " -f Show available formats before proceeding\n"; -printf " -a Auto play the first result, no selector\n"; -printf " -r Auto play a random result, no selector\n"; -printf " -n To specify number of videos to select with -a or -r\n"; -printf " -l Loop: prompt selector again after video ends\n"; -printf " -s After the video ends make another search \n"; -printf " -L Prints the selected URL only, helpful for scripting\n"; +printf " -h, --help Show this help text\n"; +printf " -t, --thumbnails Show thumbnails (requires ueberzug)\n"; +printf " Doesn't work with -H -D\n"; +printf " -D, --ext-menu Use external menu(default dmenu) instead of fzf \n"; +printf " -H, --choose-from-history Choose from history \n"; +printf " -x, --clear-history Delete history\n"; +printf " -m, --audio-only Audio only (for music)\n"; +printf " -d, --download Download to current directory\n"; +printf " -f Show available formats before proceeding\n"; +printf " -a, --auto-play Auto play the first result, no selector\n"; +printf " -r Auto play a random result, no selector\n"; +printf " -n, --link-count To specify number of videos to select with -a or -r\n"; +printf " -l, --loop Loop: prompt selector again after video ends\n"; +printf " -s After the video ends make another search \n"; +printf " -L, --link-only Prints the selected URL only, helpful for scripting\n"; printf " Use - instead of for stdin\n"; printf "\n" -printf " Option can be combined. Like\n" -printf " ytfzf -fDH to show history using external \n" -printf " menu and show formats\n" +printf " Option usage:\n" +printf " ytfzf -fDH to show history using external \n" +printf " menu and show formats\n" +printf " ytfzf -fD --choose-from-history same as above\n" printf "\n" printf " Defaults can be modified through ENV variables\n"; printf " Defaults:\n"; @@ -411,9 +412,68 @@ random_select=0 search_again=0 show_format=0 link_count=1 -# OPT -while getopts "LhDmdfxHarltsn:U:" opt; do + +#OPT +while getopts "LhDmdfxHarltsn:U:-:" opt; do case ${opt} in + #Long options + -) + case "${OPTARG}" in + help) + helpinfo + exit; + ;; + ext-menu) + is_ext_menu="1" + ;; + download) + YTFZF_PLAYER="youtube-dl" + YTFZF_PLAYER_FORMAT="youtube-dl -f" + ;; + choose-from-history) + get_history + ;; + clear-history) + [ -e "$history_file" ] && rm "$history_file" && touch "$history_file" && printf "History has been cleared\n" + exit + ;; + auto-select) + auto_select=1 + ;; + random-select) + random_select=1 + ;; + search) + search_again=1 + YTFZF_LOOP=1 + ;; + loop) + YTFZF_LOOP=1 + ;; + show-thumbnails|thumbnails) + show_thumbnails=1 + ;; + link|link-only) + show_link_only=1 + ;; + link-count=*) + link_count=${OPTARG#*=} + ;; + audio-only) + YTFZF_PREF="bestaudio" + ;; + auto-play) + auto_select=1 + ;; + *) + printf "--$OPTARG not found\n" + usageinfo + exit 2; + ;; + + esac + ;; + #Short options h) helpinfo exit; ;; @@ -452,13 +512,14 @@ while getopts "LhDmdfxHarltsn:U:" opt; do # Not to be used by explicitly ;; *) - printf "Option not found!\n" + printf "${OPT-Option} not found!\n" usageinfo exit 2; ;; esac done shift $((OPTIND-1)) + [ "$*" = "-" ] && is_stdin=1 || is_stdin=0 search_query="$*" check_if_url "$search_query" -- cgit v1.2.3 From 4646fe312a86400cd60c401f126cccf8d9ef8cf7 Mon Sep 17 00:00:00 2001 From: Euro20179 Date: Tue, 2 Mar 2021 14:42:06 -0800 Subject: long optarg parse simplification --- ytfzf | 118 +++++++++++++++++++++++++++++------------------------------------- 1 file changed, 51 insertions(+), 67 deletions(-) diff --git a/ytfzf b/ytfzf index b9f63ea..da69749 100755 --- a/ytfzf +++ b/ytfzf @@ -45,21 +45,21 @@ dep_ck "jq" "youtube-dl" "$YTFZF_PLAYER"; helpinfo () { printf "Usage: %bytfzf [OPTIONS] %b%b\n" "\033[1;32m" "\033[1;33m" "\033[0m"; printf " OPTIONS:\n" -printf " -h, --help Show this help text\n"; -printf " -t, --thumbnails Show thumbnails (requires ueberzug)\n"; -printf " Doesn't work with -H -D\n"; -printf " -D, --ext-menu Use external menu(default dmenu) instead of fzf \n"; -printf " -H, --choose-from-history Choose from history \n"; -printf " -x, --clear-history Delete history\n"; -printf " -m, --audio-only Audio only (for music)\n"; -printf " -d, --download Download to current directory\n"; -printf " -f Show available formats before proceeding\n"; -printf " -a, --auto-play Auto play the first result, no selector\n"; -printf " -r Auto play a random result, no selector\n"; -printf " -n, --link-count To specify number of videos to select with -a or -r\n"; -printf " -l, --loop Loop: prompt selector again after video ends\n"; -printf " -s After the video ends make another search \n"; -printf " -L, --link-only Prints the selected URL only, helpful for scripting\n"; +printf " -h, --help Show this help text\n"; +printf " -t, --thumbnails Show thumbnails (requires ueberzug)\n"; +printf " Doesn't work with -H -D\n"; +printf " -D, --ext-menu Use external menu(default dmenu) instead of fzf \n"; +printf " -H, --choose-from-history Choose from history \n"; +printf " -x, --clear-history Delete history\n"; +printf " -m, --audio-only Audio only (for music)\n"; +printf " -d, --download Download to current directory\n"; +printf " -f Show available formats before proceeding\n"; +printf " -a, --auto-play Auto play the first result, no selector\n"; +printf " -r --random-play Auto play a random result, no selector\n"; +printf " -n, --link-count To specify number of videos to select with -a or -r\n"; +printf " -l, --loop Loop: prompt selector again after video ends\n"; +printf " -s After the video ends make another search \n"; +printf " -L, --link-only Prints the selected URL only, helpful for scripting\n"; printf " Use - instead of for stdin\n"; printf "\n" printf " Option usage:\n" @@ -414,63 +414,43 @@ show_format=0 link_count=1 #OPT -while getopts "LhDmdfxHarltsn:U:-:" opt; do + +parse_opt () { + opt=$1 + OPTARG="${2-}" case ${opt} in #Long options -) case "${OPTARG}" in - help) - helpinfo - exit; - ;; - ext-menu) - is_ext_menu="1" - ;; - download) - YTFZF_PLAYER="youtube-dl" - YTFZF_PLAYER_FORMAT="youtube-dl -f" - ;; - choose-from-history) - get_history - ;; - clear-history) - [ -e "$history_file" ] && rm "$history_file" && touch "$history_file" && printf "History has been cleared\n" - exit - ;; - auto-select) - auto_select=1 - ;; - random-select) - random_select=1 - ;; - search) - search_again=1 - YTFZF_LOOP=1 - ;; - loop) - YTFZF_LOOP=1 - ;; - show-thumbnails|thumbnails) - show_thumbnails=1 - ;; - link|link-only) - show_link_only=1 - ;; - link-count=*) - link_count=${OPTARG#*=} - ;; - audio-only) - YTFZF_PREF="bestaudio" - ;; - auto-play) - auto_select=1 - ;; - *) - printf "--$OPTARG not found\n" - usageinfo - exit 2; - ;; + help) parse_opt "h" ;; + + ext-menu) parse_opt "D" ;; + + download) parse_opt "d" ;; + + choose-from-history) parse_opt "H" ;; + + clear-history) parse_opt "x" ;; + + random-select) parase_opt "r" ;; + + search) parse_opt "s" ;; + + loop) parse_opt "l" ;; + thumbnails) parse_opt "t" ;; + + link-only) parse_opt "L" ;; + + link-count=*) parse_opt "n" "${OPTARG#*=}" ;; + + audio-only) parse_opt "m" ;; + + auto-play) parse_opt "a" ;; + + random-auto-play) parse_opt "r" ;; + + *) parse_opt "UNKNOWN" ;; esac ;; #Short options @@ -517,6 +497,10 @@ while getopts "LhDmdfxHarltsn:U:-:" opt; do exit 2; ;; esac +} + +while getopts "LhDmdfxHarltsn:U:-:" opt; do + parse_opt "$opt" "$OPTARG" done shift $((OPTIND-1)) -- cgit v1.2.3 From 565e37ff22d6a1893d431e0b51f52bf7310c12cb Mon Sep 17 00:00:00 2001 From: Euro20179 Date: Tue, 2 Mar 2021 14:42:42 -0800 Subject: added = to link-count in help --- ytfzf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ytfzf b/ytfzf index da69749..e903b96 100755 --- a/ytfzf +++ b/ytfzf @@ -56,7 +56,7 @@ printf " -d, --download Download to current directory\ printf " -f Show available formats before proceeding\n"; printf " -a, --auto-play Auto play the first result, no selector\n"; printf " -r --random-play Auto play a random result, no selector\n"; -printf " -n, --link-count To specify number of videos to select with -a or -r\n"; +printf " -n, --link-count= To specify number of videos to select with -a or -r\n"; printf " -l, --loop Loop: prompt selector again after video ends\n"; printf " -s After the video ends make another search \n"; printf " -L, --link-only Prints the selected URL only, helpful for scripting\n"; -- cgit v1.2.3 From 7b3af8443d8a68b2e8c432d77bc27fab8389eb25 Mon Sep 17 00:00:00 2001 From: Euro20179 Date: Tue, 2 Mar 2021 14:52:53 -0800 Subject: better help, commenting --- README.md | 32 ++++++++++++++++---------------- ytfzf | 6 ++++-- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 0e3c551..4a60117 100644 --- a/README.md +++ b/README.md @@ -28,22 +28,22 @@ Initially this used to be a single line script. But for portability and extensib ``` Usage: ytfzf - -h Show this help text - -t Show thumbnails (requires ueberzug) - Doesn't work with -H -D - -D Use external menu(default dmenu) instead of fzf - -H Choose from history - -x Delete history - -m Audio only (for music) - -d Download to current directory - -f Show available formats before proceeding - -a Auto play the first result, no selector - -r Auto play a random result, no selector - -n To specify number of videos to select with -a or -r - -l Loop: prompt selector again after video ends - -s After the video ends make another search - -L Prints the selected URL only, helpful for scripting - Use - instead of for stdin + -h, --help Show this help text + -t, --thumbnails Show thumbnails (requires ueberzug) + Doesn't work with -H -D + -D, --ext-menu Use external menu(default dmenu) instead of fzf + -H, --choose-from-history Choose from history + -x, --clear-history Delete history + -m, --audio-only Audio only (for music) + -d, --download Download to current directory + -f Show available formats before proceeding + -a, --auto-play Auto play the first result, no selector + -r --random-play Auto play a random result, no selector + -n, --link-count= To specify number of videos to select with -a or -r + -l, --loop Loop: prompt selector again after video ends + -s After the video ends make another search + -L, --link-only Prints the selected URL only, helpful for scripting + Use - instead of for stdin ``` * To use dmenu with a custom width diff --git a/ytfzf b/ytfzf index e903b96..cdfa7e4 100755 --- a/ytfzf +++ b/ytfzf @@ -56,7 +56,7 @@ printf " -d, --download Download to current directory\ printf " -f Show available formats before proceeding\n"; printf " -a, --auto-play Auto play the first result, no selector\n"; printf " -r --random-play Auto play a random result, no selector\n"; -printf " -n, --link-count= To specify number of videos to select with -a or -r\n"; +printf " -n, --link-count= To specify number of videos to select with -a or -r\n"; printf " -l, --loop Loop: prompt selector again after video ends\n"; printf " -s After the video ends make another search \n"; printf " -L, --link-only Prints the selected URL only, helpful for scripting\n"; @@ -414,13 +414,15 @@ show_format=0 link_count=1 #OPT - parse_opt () { + #the first arg is the option opt=$1 + #second arg is the optarg defaults to empty OPTARG="${2-}" case ${opt} in #Long options -) + #if the option has a short version it calls this function with the opt as the shortopt case "${OPTARG}" in help) parse_opt "h" ;; -- cgit v1.2.3 From 77383a459635ee748d5312e17810f7159414930a Mon Sep 17 00:00:00 2001 From: Euro20179 Date: Tue, 2 Mar 2021 15:56:09 -0800 Subject: code prettification --- ytfzf | 70 +++++++++++++++++++++++++++++++++---------------------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/ytfzf b/ytfzf index cdfa7e4..d1fb370 100755 --- a/ytfzf +++ b/ytfzf @@ -417,8 +417,8 @@ link_count=1 parse_opt () { #the first arg is the option opt=$1 - #second arg is the optarg defaults to empty - OPTARG="${2-}" + #second arg is the optarg + OPTARG="$2" case ${opt} in #Long options -) @@ -452,52 +452,52 @@ parse_opt () { random-auto-play) parse_opt "r" ;; - *) parse_opt "UNKNOWN" ;; + *) + printf "Illegal option --$OPTARG\n" + usageinfo + exit 2; + ;; esac ;; #Short options h) helpinfo - exit; - ;; - D) is_ext_menu="1" - ;; - m) YTFZF_PREF="bestaudio" - ;; + exit ;; + + D) is_ext_menu="1" ;; + + m) YTFZF_PREF="bestaudio" ;; + d) YTFZF_PLAYER="youtube-dl" - YTFZF_PLAYER_FORMAT="youtube-dl -f " - ;; - f) show_format=1 - ;; - H) get_history - ;; + YTFZF_PLAYER_FORMAT="youtube-dl -f " ;; + f) show_format=1 ;; + + H) get_history ;; + x) [ -e "$history_file" ] && rm "$history_file" && touch "$history_file" && printf "History has been cleared\n" - exit; - ;; - a) auto_select=1 - ;; - r) - random_select=1 - ;; + exit ;; + a) auto_select=1 ;; + + r) random_select=1 ;; + s) search_again=1 - YTFZF_LOOP=1 - ;; - l) YTFZF_LOOP=1 - ;; - t) show_thumbnails=1 - ;; - L) show_link_only=1 - ;; - n) link_count="$OPTARG" - ;; + YTFZF_LOOP=1 ;; + + l) YTFZF_LOOP=1 ;; + + t) show_thumbnails=1 ;; + + L) show_link_only=1 ;; + + n) link_count="$OPTARG" ;; + U) [ -p "$FIFO" ] && preview_img "$OPTARG"; exit; + # This option is reserved for the script, to show image previews # Not to be used by explicitly ;; *) - printf "${OPT-Option} not found!\n" usageinfo - exit 2; - ;; + exit 2 ;; esac } -- cgit v1.2.3 From e6b5bfc163ca1cca6b212356e1a4fc3d97bd0ea2 Mon Sep 17 00:00:00 2001 From: Euro20179 Date: Tue, 2 Mar 2021 15:57:22 -0800 Subject: more code prettification --- ytfzf | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ytfzf b/ytfzf index d1fb370..e6a70de 100755 --- a/ytfzf +++ b/ytfzf @@ -457,8 +457,7 @@ parse_opt () { usageinfo exit 2; ;; - esac - ;; + esac ;; #Short options h) helpinfo exit ;; @@ -469,12 +468,14 @@ parse_opt () { d) YTFZF_PLAYER="youtube-dl" YTFZF_PLAYER_FORMAT="youtube-dl -f " ;; + f) show_format=1 ;; H) get_history ;; x) [ -e "$history_file" ] && rm "$history_file" && touch "$history_file" && printf "History has been cleared\n" exit ;; + a) auto_select=1 ;; r) random_select=1 ;; @@ -491,10 +492,10 @@ parse_opt () { n) link_count="$OPTARG" ;; U) [ -p "$FIFO" ] && preview_img "$OPTARG"; exit; - # This option is reserved for the script, to show image previews # Not to be used by explicitly ;; + *) usageinfo exit 2 ;; -- cgit v1.2.3 From a8432b89785757e2ca7ae6c2bf8a98e4dd671c80 Mon Sep 17 00:00:00 2001 From: Euro20179 Date: Tue, 2 Mar 2021 16:00:31 -0800 Subject: overwrite history file vs remaking it --- ytfzf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ytfzf b/ytfzf index e6a70de..966412a 100755 --- a/ytfzf +++ b/ytfzf @@ -473,7 +473,7 @@ parse_opt () { H) get_history ;; - x) [ -e "$history_file" ] && rm "$history_file" && touch "$history_file" && printf "History has been cleared\n" + x) [ -e "$history_file" ] && (echo "" > "$history_file") && printf "History has been cleared\n" exit ;; a) auto_select=1 ;; -- cgit v1.2.3 From 145d93394ca1918ee9637d54e88516e7ad298ee1 Mon Sep 17 00:00:00 2001 From: Euro20179 Date: Tue, 2 Mar 2021 16:27:18 -0800 Subject: fixed awful texteditor whitespace in helpinfo --- ytfzf | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/ytfzf b/ytfzf index 966412a..5b52028 100755 --- a/ytfzf +++ b/ytfzf @@ -45,20 +45,20 @@ dep_ck "jq" "youtube-dl" "$YTFZF_PLAYER"; helpinfo () { printf "Usage: %bytfzf [OPTIONS] %b%b\n" "\033[1;32m" "\033[1;33m" "\033[0m"; printf " OPTIONS:\n" -printf " -h, --help Show this help text\n"; -printf " -t, --thumbnails Show thumbnails (requires ueberzug)\n"; -printf " Doesn't work with -H -D\n"; -printf " -D, --ext-menu Use external menu(default dmenu) instead of fzf \n"; -printf " -H, --choose-from-history Choose from history \n"; -printf " -x, --clear-history Delete history\n"; +printf " -h, --help Show this help text\n"; +printf " -t, --thumbnails Show thumbnails (requires ueberzug)\n"; +printf " Doesn't work with -H -D\n"; +printf " -D, --ext-menu Use external menu(default dmenu) instead of fzf \n"; +printf " -H, --choose-from-history Choose from history \n"; +printf " -x, --clear-history Delete history\n"; printf " -m, --audio-only Audio only (for music)\n"; printf " -d, --download Download to current directory\n"; -printf " -f Show available formats before proceeding\n"; +printf " -f Show available formats before proceeding\n"; printf " -a, --auto-play Auto play the first result, no selector\n"; printf " -r --random-play Auto play a random result, no selector\n"; -printf " -n, --link-count= To specify number of videos to select with -a or -r\n"; -printf " -l, --loop Loop: prompt selector again after video ends\n"; -printf " -s After the video ends make another search \n"; +printf " -n, --link-count= To specify number of videos to select with -a or -r\n"; +printf " -l, --loop Loop: prompt selector again after video ends\n"; +printf " -s After the video ends make another search \n"; printf " -L, --link-only Prints the selected URL only, helpful for scripting\n"; printf " Use - instead of for stdin\n"; printf "\n" -- cgit v1.2.3