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.insert(0, 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. complete_cmd = ['complete', '--command', 'youtube-dl', '--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 = youtube_dl.parseOpts()[0]
  38. build_completion(parser)