]> Raphaƫl G. Git Repositories - youtubedl/blob - devscripts/zsh-completion.py
e8d71928a53b4ab26a0bc710be66a76e7b4a7f40
[youtubedl] / devscripts / zsh-completion.py
1 #!/usr/bin/env python
2 import os
3 from os.path import dirname as dirn
4 import sys
5
6 sys.path.append(dirn(dirn((os.path.abspath(__file__)))))
7 import youtube_dl
8
9 ZSH_COMPLETION_FILE = "youtube-dl.zsh"
10 ZSH_COMPLETION_TEMPLATE = "devscripts/zsh-completion.in"
11
12
13 def build_completion(opt_parser):
14 opts = [opt for group in opt_parser.option_groups
15 for opt in group.option_list]
16 opts_file = [opt for opt in opts if opt.metavar == "FILE"]
17 opts_dir = [opt for opt in opts if opt.metavar == "DIR"]
18
19 fileopts = []
20 for opt in opts_file:
21 if opt._short_opts:
22 fileopts.extend(opt._short_opts)
23 if opt._long_opts:
24 fileopts.extend(opt._long_opts)
25
26 diropts = []
27 for opt in opts_dir:
28 if opt._short_opts:
29 diropts.extend(opt._short_opts)
30 if opt._long_opts:
31 diropts.extend(opt._long_opts)
32
33 flags = [opt.get_opt_string() for opt in opts]
34
35 with open(ZSH_COMPLETION_TEMPLATE) as f:
36 template = f.read()
37
38 template = template.replace("{{fileopts}}", "|".join(fileopts))
39 template = template.replace("{{diropts}}", "|".join(diropts))
40 template = template.replace("{{flags}}", " ".join(flags))
41
42 with open(ZSH_COMPLETION_FILE, "w") as f:
43 f.write(template)
44
45 parser = youtube_dl.parseOpts()[0]
46 build_completion(parser)