make_bash_completion.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. opt = opts[long_opt]
  92. output_value_case(out, opt.long_opt, opt.values)
  93. # Complete directory
  94. dir_opts = []
  95. for opt in opts.itervalues():
  96. if opt.values and opt.values[0] == '/path/to/directory':
  97. dir_opts.append(opt)
  98. # Complete file
  99. output_value_case_dir_comp(out,'|'.join([opt.long_opt for opt in dir_opts]))
  100. # Complete specific file type
  101. output_value_case_file_comp(out, '--torrent-file', ['torrent'])
  102. output_value_case_file_comp(out, '--metalink-file', ['meta4', 'metalink'])
  103. out.write("""\
  104. esac
  105. """)
  106. # Complete option name.
  107. out.write("""\
  108. case $cur in
  109. -*)
  110. COMPREPLY=( $( compgen -W '\
  111. """)
  112. bool_values = [ 'true', 'false' ]
  113. for opt in opts.itervalues():
  114. out.write(opt.long_opt)
  115. out.write(' ')
  116. # Options which takes optional argument needs "=" between
  117. # option name and value, so we complete them including "=" and
  118. # value here.
  119. if opt.optional:
  120. if bool_values == opt.values:
  121. # Because boolean option takes true when argument is
  122. # omitted, we just complete additional 'false' option
  123. # only.
  124. out.write('='.join([opt.long_opt, 'false']))
  125. out.write(' ')
  126. else:
  127. for value in opt.values:
  128. out.write('='.join([opt.long_opt, value]))
  129. out.write(' ')
  130. out.write("""\
  131. ' -- "$cur" ) )
  132. ;;
  133. """)
  134. # If no option found for completion then complete with files.
  135. out.write("""\
  136. *)
  137. _filedir '@(torrent|meta4|metalink|text|txt|list|lst)'
  138. [ ${#COMPREPLY[@]} -eq 0 ] && _filedir
  139. return 0
  140. esac
  141. return 0
  142. }
  143. complete -F _aria2c aria2c
  144. """)
  145. if __name__ == '__main__':
  146. if len(sys.argv) < 2:
  147. print "Generates aria2c(1) bash_completion using `aria2c --help=#all'"
  148. print "Usage: make_bash_completion.py /path/to/aria2c"
  149. exit(1)
  150. opts = get_all_options(sys.argv[1])
  151. output_case(sys.stdout, opts)