aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpystardust <notpiestardust@gmail.com>2021-03-03 13:31:36 +0530
committerpystardust <notpiestardust@gmail.com>2021-03-03 13:31:36 +0530
commit88bc6a8e8083f2812ba4ca860acb41fb8fb5177f (patch)
tree766beb66564a864cec10fed9be2d6ee536ae1823
parentadbf63a736acf4c3ad5c7f013f4a627a5090358b (diff)
parent3091086a4efdc1d0aa7acefe05f3b2dee9388345 (diff)
downloadytfzf-88bc6a8e8083f2812ba4ca860acb41fb8fb5177f.tar.gz
Merge branch 'Euro20179-option-parsing-refactor'
Added long options
-rw-r--r--README.md32
-rwxr-xr-xytfzf158
2 files changed, 119 insertions, 71 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 <search query>
- -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 <search query> Audio only (for music)
- -d <search query> Download to current directory
- -f <search query> Show available formats before proceeding
- -a <search query> Auto play the first result, no selector
- -r <search query> Auto play a random result, no selector
- -n <link count> To specify number of videos to select with -a or -r
- -l <search query> Loop: prompt selector again after video ends
- -s <search query> After the video ends make another search
- -L <search query> Prints the selected URL only, helpful for scripting
- Use - instead of <query> 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 <search-query> Audio only (for music)
+ -d, --download <search-query> Download to current directory
+ -f <search-query> Show available formats before proceeding
+ -a, --auto-play <search-query> Auto play the first result, no selector
+ -r --random-play <search-query> Auto play a random result, no selector
+ -n, --link-count= <link-count> To specify number of videos to select with -a or -r
+ -l, --loop <search-query> Loop: prompt selector again after video ends
+ -s <search-query> After the video ends make another search
+ -L, --link-only <search-query> Prints the selected URL only, helpful for scripting
+ Use - instead of <search-query> for stdin
```
* To use dmenu with a custom width
diff --git a/ytfzf b/ytfzf
index 7ef3785..9f03f94 100755
--- a/ytfzf
+++ b/ytfzf
@@ -59,26 +59,27 @@ dep_ck "jq" "youtube-dl";
helpinfo () {
printf "Usage: %bytfzf [OPTIONS] %b<search-query>%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 <search-query> Audio only (for music)\n";
-printf " -d <search-query> Download to current directory\n";
-printf " -f <search-query> Show available formats before proceeding\n";
-printf " -a <search-query> Auto play the first result, no selector\n";
-printf " -r <search-query> 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 <search-query> Loop: prompt selector again after video ends\n";
-printf " -s <search-query> After the video ends make another search \n";
-printf " -L <search-query> 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 <search-query> Audio only (for music)\n";
+printf " -d, --download <search-query> Download to current directory\n";
+printf " -f <search-query> Show available formats before proceeding\n";
+printf " -a, --auto-play <search-query> Auto play the first result, no selector\n";
+printf " -r --random-play <search-query> Auto play a random result, no selector\n";
+printf " -n, --video-count= <video-count> To specify number of videos to select with -a or -r\n";
+printf " -l, --loop <search-query> Loop: prompt selector again after video ends\n";
+printf " -s <search-query> After the video ends make another search \n";
+printf " -L, --link-only <search-query> Prints the selected URL only, helpful for scripting\n";
printf " Use - instead of <search-query> 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";
@@ -90,7 +91,7 @@ printf " YTFZF_CUR=1 For status bar bodules\n";
printf " YTFZF_EXTMENU=' dmenu -i -l 30'\n";
printf " To use rofi\n";
printf " YTFZF_EXTMENU=' rofi -dmenu -fuzzy -width 1500'\n";
-printf " YTFZF_ENABLE_FZF_DEFAULT_OPTS=0 1 : fzf will use FZF_DEFAULT_OPTS\n";
+printf " YTFZF_ENABLE_FZF_DEFAULT_OPTS=0 1 : fzf will use FZF_DEFAULT_OPTS\n";
printf "\n";
printf " For more details refer https://github.com/pystardust/ytfzf\n";
}
@@ -425,54 +426,101 @@ random_select=0
search_again=0
show_format=0
link_count=1
-# OPT
-while getopts "LhDmdfxHarltsn:U:" opt; do
+
+#OPT
+parse_opt () {
+ #the first arg is the option
+ opt=$1
+ #second arg is the optarg
+ 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" ;;
+
+ 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" ;;
+
+ video-count=*) parse_opt "n" "${OPTARG#*=}" ;;
+
+ audio-only) parse_opt "m" ;;
+
+ auto-play) parse_opt "a" ;;
+
+ random-auto-play) parse_opt "r" ;;
+
+ *)
+ 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
- ;;
- 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
- ;;
+ YTFZF_PLAYER_FORMAT="youtube-dl -f " ;;
+
+ f) show_format=1 ;;
+
+ H) get_history ;;
+
+ x) [ -e "$history_file" ] && (echo "" > "$history_file") && printf "History has been cleared\n"
+ 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
+ # Not to be used explicitly
;;
+
*)
- printf "Option not found!\n"
usageinfo
- exit 2;
- ;;
+ exit 2 ;;
esac
+}
+
+while getopts "LhDmdfxHarltsn:U:-:" opt; do
+ parse_opt "$opt" "$OPTARG"
done
shift $((OPTIND-1))
+
[ "$*" = "-" ] && is_stdin=1 || is_stdin=0
search_query="$*"
check_if_url "$search_query"
Un proyecto texto-plano.xyz