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.4 KiB

  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. ZSH_COMPLETION_FILE = "youtube-dl.zsh"
  9. ZSH_COMPLETION_TEMPLATE = "devscripts/zsh-completion.in"
  10. def build_completion(opt_parser):
  11. opts = [opt for group in opt_parser.option_groups
  12. for opt in group.option_list]
  13. opts_file = [opt for opt in opts if opt.metavar == "FILE"]
  14. opts_dir = [opt for opt in opts if opt.metavar == "DIR"]
  15. fileopts = []
  16. for opt in opts_file:
  17. if opt._short_opts:
  18. fileopts.extend(opt._short_opts)
  19. if opt._long_opts:
  20. fileopts.extend(opt._long_opts)
  21. diropts = []
  22. for opt in opts_dir:
  23. if opt._short_opts:
  24. diropts.extend(opt._short_opts)
  25. if opt._long_opts:
  26. diropts.extend(opt._long_opts)
  27. flags = [opt.get_opt_string() for opt in opts]
  28. with open(ZSH_COMPLETION_TEMPLATE) as f:
  29. template = f.read()
  30. template = template.replace("{{fileopts}}", "|".join(fileopts))
  31. template = template.replace("{{diropts}}", "|".join(diropts))
  32. template = template.replace("{{flags}}", " ".join(flags))
  33. with open(ZSH_COMPLETION_FILE, "w") as f:
  34. f.write(template)
  35. parser = youtube_dl.parseOpts()[0]
  36. build_completion(parser)