]> jfr.im git - yt-dlp.git/blame_incremental - devscripts/fish-completion.py
[extractor] Common function `_match_valid_url`
[yt-dlp.git] / devscripts / fish-completion.py
... / ...
CommitLineData
1#!/usr/bin/env python3
2from __future__ import unicode_literals
3
4import optparse
5import os
6from os.path import dirname as dirn
7import sys
8
9sys.path.insert(0, dirn(dirn((os.path.abspath(__file__)))))
10import yt_dlp
11from yt_dlp.utils import shell_quote
12
13FISH_COMPLETION_FILE = 'completions/fish/yt-dlp.fish'
14FISH_COMPLETION_TEMPLATE = 'devscripts/fish-completion.in'
15
16EXTRA_ARGS = {
17 'remux-video': ['--arguments', 'mp4 mkv', '--exclusive'],
18 'recode-video': ['--arguments', 'mp4 flv ogg webm mkv', '--exclusive'],
19
20 # Options that need a file parameter
21 'download-archive': ['--require-parameter'],
22 'cookies': ['--require-parameter'],
23 'load-info': ['--require-parameter'],
24 'batch-file': ['--require-parameter'],
25}
26
27
28def build_completion(opt_parser):
29 commands = []
30
31 for group in opt_parser.option_groups:
32 for option in group.option_list:
33 long_option = option.get_opt_string().strip('-')
34 complete_cmd = ['complete', '--command', 'yt-dlp', '--long-option', long_option]
35 if option._short_opts:
36 complete_cmd += ['--short-option', option._short_opts[0].strip('-')]
37 if option.help != optparse.SUPPRESS_HELP:
38 complete_cmd += ['--description', option.help]
39 complete_cmd.extend(EXTRA_ARGS.get(long_option, []))
40 commands.append(shell_quote(complete_cmd))
41
42 with open(FISH_COMPLETION_TEMPLATE) as f:
43 template = f.read()
44 filled_template = template.replace('{{commands}}', '\n'.join(commands))
45 with open(FISH_COMPLETION_FILE, 'w') as f:
46 f.write(filled_template)
47
48
49parser = yt_dlp.parseOpts()[0]
50build_completion(parser)