make_bash_completion.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #!/usr/bin/env python
  2. import subprocess
  3. from StringIO import StringIO
  4. import re
  5. import sys
  6. class Option:
  7. def __init__(self, long_opt, short_opt, optional):
  8. self.long_opt = long_opt
  9. self.short_opt = short_opt
  10. self.optional = optional
  11. self.values = []
  12. def get_all_options(cmd):
  13. opt_pattern = re.compile(r' (?:(-.), )?(--[^\s\[=]+)(\[)?')
  14. values_pattern = re.compile(r'\s+Possible Values: (.+)')
  15. proc = subprocess.Popen([cmd, "--help=#all"], stdout=subprocess.PIPE)
  16. stdoutdata, stderrdata = proc.communicate()
  17. cur_option = None
  18. opts = {}
  19. for line in StringIO(stdoutdata):
  20. match = opt_pattern.match(line)
  21. if match:
  22. long_opt = match.group(2)
  23. short_opt = match.group(1)
  24. optional = match.group(3) == '['
  25. if cur_option:
  26. opts[cur_option.long_opt] = cur_option
  27. cur_option = Option(long_opt, short_opt, optional)
  28. else:
  29. match = values_pattern.match(line)
  30. if match:
  31. cur_option.values = match.group(1).split(', ')
  32. if cur_option:
  33. opts[cur_option.long_opt] = cur_option
  34. # for opt in opts.itervalues():
  35. # print opt.short_opt, opt.long_opt, opt.optional, opt.values
  36. return opts
  37. def output_value_case(out, key, values):
  38. out.write("""\
  39. {0})
  40. COMPREPLY=( $( compgen -W '{1}' -- "$cur" ) )
  41. return 0
  42. ;;
  43. """.format(key, " ".join(values)))
  44. def output_value_case_file_comp(out, key, exts):
  45. out.write("""\
  46. {0})
  47. _filedir '@({1})'
  48. return 0
  49. ;;
  50. """.format(key, '|'.join(exts)))
  51. def output_value_case_dir_comp(out, key):
  52. out.write("""\
  53. {0})
  54. _filedir -d
  55. return 0
  56. ;;
  57. """.format(key))
  58. def output_case(out, opts):
  59. out.write("""\
  60. _aria2c()
  61. {
  62. local cur prev split=false
  63. COMPREPLY=()
  64. COMP_WORDBREAKS=${COMP_WORDBREAKS//=}
  65. cmd=${COMP_WORDS[0]}
  66. _get_comp_words_by_ref cur prev
  67. """)
  68. bool_opts = []
  69. nonbool_opts = []
  70. for opt in opts.itervalues():
  71. if opt.values == ['true', 'false']:
  72. bool_opts.append(opt)
  73. else:
  74. nonbool_opts.append(opt)
  75. out.write("""\
  76. case $prev in
  77. """)
  78. # Complete pre-defined option arguments
  79. for long_opt in ['--ftp-type',
  80. '--proxy-method',
  81. '--metalink-preferred-protocol',
  82. '--bt-min-crypto-level',
  83. '--follow-metalink',
  84. '--file-allocation',
  85. '--log-level',
  86. '--uri-selector',
  87. '--event-poll',
  88. '--follow-torrent',
  89. '--stream-piece-selector',
  90. '--download-result',
  91. '--min-tls-version',
  92. '--console-log-level']:
  93. opt = opts[long_opt]
  94. output_value_case(out, opt.long_opt, opt.values)
  95. # Complete directory
  96. dir_opts = []
  97. for opt in opts.itervalues():
  98. if opt.values and opt.values[0] == '/path/to/directory':
  99. dir_opts.append(opt)
  100. # Complete file
  101. output_value_case_dir_comp(out,'|'.join([opt.long_opt for opt in dir_opts]))
  102. # Complete specific file type
  103. output_value_case_file_comp(out, '--torrent-file', ['torrent'])
  104. output_value_case_file_comp(out, '--metalink-file', ['meta4', 'metalink'])
  105. out.write("""\
  106. esac
  107. """)
  108. # Complete option name.
  109. out.write("""\
  110. case $cur in
  111. -*)
  112. COMPREPLY=( $( compgen -W '\
  113. """)
  114. bool_values = [ 'true', 'false' ]
  115. for opt in opts.itervalues():
  116. out.write(opt.long_opt)
  117. out.write(' ')
  118. # Options which takes optional argument needs "=" between
  119. # option name and value, so we complete them including "=" and
  120. # value here.
  121. if opt.optional:
  122. if bool_values == opt.values:
  123. # Because boolean option takes true when argument is
  124. # omitted, we just complete additional 'false' option
  125. # only.
  126. out.write('='.join([opt.long_opt, 'false']))
  127. out.write(' ')
  128. else:
  129. for value in opt.values:
  130. out.write('='.join([opt.long_opt, value]))
  131. out.write(' ')
  132. out.write("""\
  133. ' -- "$cur" ) )
  134. ;;
  135. """)
  136. # If no option found for completion then complete with files.
  137. out.write("""\
  138. *)
  139. _filedir '@(torrent|meta4|metalink|text|txt|list|lst)'
  140. [ ${#COMPREPLY[@]} -eq 0 ] && _filedir
  141. return 0
  142. esac
  143. return 0
  144. }
  145. complete -F _aria2c aria2c
  146. """)
  147. if __name__ == '__main__':
  148. if len(sys.argv) < 2:
  149. print "Generates aria2c(1) bash_completion using `aria2c --help=#all'"
  150. print "Usage: make_bash_completion.py /path/to/aria2c"
  151. exit(1)
  152. opts = get_all_options(sys.argv[1])
  153. output_case(sys.stdout, opts)