make_bash_completion.py: Support Python 3.4

This commit is contained in:
Tatsuhiro Tsujikawa 2015-06-07 13:01:02 +09:00
parent e857e99df8
commit aa6699435a
1 changed files with 9 additions and 6 deletions

View File

@ -1,7 +1,10 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from __future__ import print_function
import subprocess import subprocess
from StringIO import StringIO import io
import re import re
import sys import sys
import os.path import os.path
@ -14,10 +17,10 @@ class Option:
def get_all_options(cmd): def get_all_options(cmd):
opt_pattern = re.compile(r' (?:(-.), )?(--[^\s\[=]+)(\[)?') opt_pattern = re.compile(r' (?:(-.), )?(--[^\s\[=]+)(\[)?')
proc = subprocess.Popen([cmd, "--help"], stdout=subprocess.PIPE) proc = subprocess.Popen([cmd, "--help"], stdout=subprocess.PIPE)
stdoutdata, stderrdata = proc.communicate() stdoutdata, _ = proc.communicate()
cur_option = None cur_option = None
opts = {} opts = {}
for line in StringIO(stdoutdata): for line in io.StringIO(stdoutdata.decode('utf-8')):
match = opt_pattern.match(line) match = opt_pattern.match(line)
if not match: if not match:
continue continue
@ -45,7 +48,7 @@ _{name}()
-*) -*)
COMPREPLY=( $( compgen -W '\ COMPREPLY=( $( compgen -W '\
''') ''')
for opt in opts.itervalues(): for opt in opts.values():
out.write(opt.long_opt) out.write(opt.long_opt)
out.write(' ') out.write(' ')
@ -66,8 +69,8 @@ complete -F _{name} {name}
if __name__ == '__main__': if __name__ == '__main__':
if len(sys.argv) < 2: if len(sys.argv) < 2:
print "Generates bash_completion using `/path/to/cmd --help'" print("Generates bash_completion using `/path/to/cmd --help'")
print "Usage: make_bash_completion.py /path/to/cmd" print("Usage: make_bash_completion.py /path/to/cmd")
exit(1) exit(1)
name = os.path.basename(sys.argv[1]) name = os.path.basename(sys.argv[1])
opts = get_all_options(sys.argv[1]) opts = get_all_options(sys.argv[1])