fish-completion.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env python
  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 youtube_dl
  9. from youtube_dl.utils import shell_quote
  10. from utils import read_file, write_file
  11. FISH_COMPLETION_FILE = 'youtube-dl.fish'
  12. FISH_COMPLETION_TEMPLATE = 'devscripts/fish-completion.in'
  13. EXTRA_ARGS = {
  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', 'youtube-dl', '--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. template = read_file(FISH_COMPLETION_TEMPLATE)
  34. filled_template = template.replace('{{commands}}', '\n'.join(commands))
  35. write_file(FISH_COMPLETION_FILE, filled_template)
  36. parser = youtube_dl.parseOpts()[0]
  37. build_completion(parser)