fish-completion.py 1.6 KB

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