]> jfr.im git - yt-dlp.git/blob - devscripts/zsh-completion.py
[cleanup] Misc cleanup (#2173)
[yt-dlp.git] / devscripts / zsh-completion.py
1 #!/usr/bin/env python3
2 import os
3 import sys
4
5 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
6
7 import yt_dlp
8
9 ZSH_COMPLETION_FILE = "completions/zsh/_yt-dlp"
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
46 parser = yt_dlp.parseOpts()[0]
47 build_completion(parser)