]> jfr.im git - yt-dlp.git/blob - devscripts/zsh-completion.py
[cleanup] Fix misc bugs (#8968)
[yt-dlp.git] / devscripts / zsh-completion.py
1 #!/usr/bin/env python3
2
3 # Allow direct execution
4 import os
5 import sys
6
7 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
8
9
10 import yt_dlp
11
12 ZSH_COMPLETION_FILE = "completions/zsh/_yt-dlp"
13 ZSH_COMPLETION_TEMPLATE = "devscripts/zsh-completion.in"
14
15
16 def build_completion(opt_parser):
17 opts = [opt for group in opt_parser.option_groups
18 for opt in group.option_list]
19 opts_file = [opt for opt in opts if opt.metavar == "FILE"]
20 opts_dir = [opt for opt in opts if opt.metavar == "DIR"]
21
22 fileopts = []
23 for opt in opts_file:
24 if opt._short_opts:
25 fileopts.extend(opt._short_opts)
26 if opt._long_opts:
27 fileopts.extend(opt._long_opts)
28
29 diropts = []
30 for opt in opts_dir:
31 if opt._short_opts:
32 diropts.extend(opt._short_opts)
33 if opt._long_opts:
34 diropts.extend(opt._long_opts)
35
36 flags = [opt.get_opt_string() for opt in opts]
37
38 with open(ZSH_COMPLETION_TEMPLATE) as f:
39 template = f.read()
40
41 template = template.replace("{{fileopts}}", "|".join(fileopts))
42 template = template.replace("{{diropts}}", "|".join(diropts))
43 template = template.replace("{{flags}}", " ".join(flags))
44
45 with open(ZSH_COMPLETION_FILE, "w") as f:
46 f.write(template)
47
48
49 parser = yt_dlp.parseOpts(ignore_config_files=True)[0]
50 build_completion(parser)