zsh-completion.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env python
  2. from __future__ import unicode_literals
  3. import os
  4. from os.path import dirname as dirn
  5. import sys
  6. sys.path.insert(0, dirn(dirn((os.path.abspath(__file__)))))
  7. import youtube_dl
  8. from utils import read_file, write_file
  9. ZSH_COMPLETION_FILE = "youtube-dl.zsh"
  10. ZSH_COMPLETION_TEMPLATE = "devscripts/zsh-completion.in"
  11. def build_completion(opt_parser):
  12. opts = [opt for group in opt_parser.option_groups
  13. for opt in group.option_list]
  14. opts_file = [opt for opt in opts if opt.metavar == "FILE"]
  15. opts_dir = [opt for opt in opts if opt.metavar == "DIR"]
  16. fileopts = []
  17. for opt in opts_file:
  18. if opt._short_opts:
  19. fileopts.extend(opt._short_opts)
  20. if opt._long_opts:
  21. fileopts.extend(opt._long_opts)
  22. diropts = []
  23. for opt in opts_dir:
  24. if opt._short_opts:
  25. diropts.extend(opt._short_opts)
  26. if opt._long_opts:
  27. diropts.extend(opt._long_opts)
  28. flags = [opt.get_opt_string() for opt in opts]
  29. template = read_file(ZSH_COMPLETION_TEMPLATE)
  30. template = template.replace("{{fileopts}}", "|".join(fileopts))
  31. template = template.replace("{{diropts}}", "|".join(diropts))
  32. template = template.replace("{{flags}}", " ".join(flags))
  33. write_file(ZSH_COMPLETION_FILE, template)
  34. parser = youtube_dl.parseOpts()[0]
  35. build_completion(parser)