fish-completion.py 1.6 KB

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