Release 2024.05.26
[yt-dlp.git] / devscripts / zsh-completion.py
blob267af5f6ec7fad627ef82c018ee8a1c51b6a0ce1
1 #!/usr/bin/env python3
3 # Allow direct execution
4 import os
5 import sys
7 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
10 import yt_dlp
12 ZSH_COMPLETION_FILE = "completions/zsh/_yt-dlp"
13 ZSH_COMPLETION_TEMPLATE = "devscripts/zsh-completion.in"
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"]
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)
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)
36 flags = [opt.get_opt_string() for opt in opts]
38 with open(ZSH_COMPLETION_TEMPLATE) as f:
39 template = f.read()
41 template = template.replace("{{fileopts}}", "|".join(fileopts))
42 template = template.replace("{{diropts}}", "|".join(diropts))
43 template = template.replace("{{flags}}", " ".join(flags))
45 with open(ZSH_COMPLETION_FILE, "w") as f:
46 f.write(template)
49 parser = yt_dlp.parseOpts(ignore_config_files=True)[0]
50 build_completion(parser)