]> jfr.im git - yt-dlp.git/blame - devscripts/zsh-completion.py
[cleanup] Delete unused extractors
[yt-dlp.git] / devscripts / zsh-completion.py
CommitLineData
cc52de43 1#!/usr/bin/env python3
3b700f8d 2import os
3b700f8d
XC
3import sys
4
e5a998f3 5sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
6
7a5c1cfe 7import yt_dlp
3b700f8d 8
359d6d86 9ZSH_COMPLETION_FILE = "completions/zsh/_yt-dlp"
3b700f8d
XC
10ZSH_COMPLETION_TEMPLATE = "devscripts/zsh-completion.in"
11
12
13def 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
582be358 45
7a5c1cfe 46parser = yt_dlp.parseOpts()[0]
3b700f8d 47build_completion(parser)