bash-completion.py 942 B

12345678910111213141516171819202122232425262728293031323334
  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 youtube_dl.compat import compat_open as open
  9. from utils import read_file
  10. BASH_COMPLETION_FILE = "youtube-dl.bash-completion"
  11. BASH_COMPLETION_TEMPLATE = "devscripts/bash-completion.in"
  12. def build_completion(opt_parser):
  13. opts_flag = []
  14. for group in opt_parser.option_groups:
  15. for option in group.option_list:
  16. # for every long flag
  17. opts_flag.append(option.get_opt_string())
  18. template = read_file(BASH_COMPLETION_TEMPLATE)
  19. with open(BASH_COMPLETION_FILE, "w", encoding='utf-8') as f:
  20. # just using the special char
  21. filled_template = template.replace("{{flags}}", " ".join(opts_flag))
  22. f.write(filled_template)
  23. parser = youtube_dl.parseOpts()[0]
  24. build_completion(parser)