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