make_bash_completion.py 4.7 KB

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