You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.6 KiB

10 years ago
  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.append(dirn(dirn((os.path.abspath(__file__)))))
  8. import youtube_dl
  9. from youtube_dl.utils import shell_quote
  10. FISH_COMPLETION_FILE = 'youtube-dl.fish'
  11. FISH_COMPLETION_TEMPLATE = 'devscripts/fish-completion.in'
  12. EXTRA_ARGS = {
  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. help_msg = shell_quote([option.help])
  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. 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 = youtube_dl.parseOpts()[0]
  39. build_completion(parser)