Skip to content

Instantly share code, notes, and snippets.

@Dineshs91
Created October 6, 2014 18:57
Show Gist options
  • Save Dineshs91/268ea78376bf8e9bbbb1 to your computer and use it in GitHub Desktop.
Save Dineshs91/268ea78376bf8e9bbbb1 to your computer and use it in GitHub Desktop.
zsh completion
#compdef youtube-dl
__youtube_dl() {
local curcontext="$curcontext" fileopts diropts cur prev
typeset -A opt_args
fileopts="-a|--batch-file|--download-archive|--cookies|--load-info"
diropts="--cache-dir"
cur=$words[CURRENT]
case $cur in
:)
_arguments '*: :(::ytfavorites ::ytrecommended ::ytsubscriptions ::ytwatchlater ::ythistory)'
;;
*)
prev=$words[CURRENT-1]
if [[ ${prev} =~ ${fileopts} ]]; then
_path_files
elif [[ ${prev} == ${diropts} ]]; then
_path_files -/
else
_arguments '*: :({{flags}})'
fi
;;
esac
}
__youtube_dl
#!/usr/bin/env python
import os
from os.path import dirname as dirn
import sys
sys.path.append(dirn(dirn((os.path.abspath(__file__)))))
import youtube_dl
BASH_COMPLETION_FILE = "_youtube-dl.zsh-completion"
BASH_COMPLETION_TEMPLATE = "devscripts/zsh-completion.in"
def build_completion(opt_parser):
opts_flag = []
for group in opt_parser.option_groups:
for option in group.option_list:
#for every long flag
opts_flag.append(option.get_opt_string())
with open(BASH_COMPLETION_TEMPLATE) as f:
template = f.read()
with open(BASH_COMPLETION_FILE, "w") as f:
#just using the special char
filled_template = template.replace("{{flags}}", " ".join(opts_flag))
f.write(filled_template)
parser = youtube_dl.parseOpts()[0]
build_completion(parser)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment