]> jfr.im git - yt-dlp.git/blame - devscripts/zsh-completion.py
[cleanup] Fix misc bugs (#8968)
[yt-dlp.git] / devscripts / zsh-completion.py
CommitLineData
cc52de43 1#!/usr/bin/env python3
54007a45 2
3# Allow direct execution
3b700f8d 4import os
3b700f8d
XC
5import sys
6
e5a998f3 7sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
8
54007a45 9
7a5c1cfe 10import yt_dlp
3b700f8d 11
359d6d86 12ZSH_COMPLETION_FILE = "completions/zsh/_yt-dlp"
3b700f8d
XC
13ZSH_COMPLETION_TEMPLATE = "devscripts/zsh-completion.in"
14
15
16def 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
582be358 48
c1714454 49parser = yt_dlp.parseOpts(ignore_config_files=True)[0]
3b700f8d 50build_completion(parser)