2015-02-15 04:05:27 +01:00
|
|
|
#!/usr/bin/env python
|
2015-06-07 06:01:02 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-02-15 04:05:27 +01:00
|
|
|
|
2015-06-07 06:01:02 +02:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
from __future__ import print_function
|
2015-02-15 04:05:27 +01:00
|
|
|
import subprocess
|
2015-06-07 06:01:02 +02:00
|
|
|
import io
|
2015-02-15 04:05:27 +01:00
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
import os.path
|
|
|
|
|
|
|
|
class Option:
|
|
|
|
def __init__(self, long_opt, short_opt):
|
|
|
|
self.long_opt = long_opt
|
|
|
|
self.short_opt = short_opt
|
|
|
|
|
|
|
|
def get_all_options(cmd):
|
|
|
|
opt_pattern = re.compile(r' (?:(-.), )?(--[^\s\[=]+)(\[)?')
|
|
|
|
proc = subprocess.Popen([cmd, "--help"], stdout=subprocess.PIPE)
|
2015-06-07 06:01:02 +02:00
|
|
|
stdoutdata, _ = proc.communicate()
|
2015-02-15 04:05:27 +01:00
|
|
|
cur_option = None
|
|
|
|
opts = {}
|
2015-06-07 06:01:02 +02:00
|
|
|
for line in io.StringIO(stdoutdata.decode('utf-8')):
|
2015-02-15 04:05:27 +01:00
|
|
|
match = opt_pattern.match(line)
|
|
|
|
if not match:
|
|
|
|
continue
|
|
|
|
long_opt = match.group(2)
|
|
|
|
short_opt = match.group(1)
|
|
|
|
opts[long_opt] = Option(long_opt, short_opt)
|
|
|
|
|
|
|
|
return opts
|
|
|
|
|
|
|
|
def output_case(out, name, opts):
|
|
|
|
out.write('''\
|
|
|
|
_{name}()
|
|
|
|
{{
|
|
|
|
local cur prev split=false
|
|
|
|
COMPREPLY=()
|
|
|
|
COMP_WORDBREAKS=${{COMP_WORDBREAKS//=}}
|
|
|
|
|
|
|
|
cmd=${{COMP_WORDS[0]}}
|
|
|
|
_get_comp_words_by_ref cur prev
|
|
|
|
'''.format(name=name))
|
|
|
|
|
|
|
|
# Complete option name.
|
|
|
|
out.write('''\
|
|
|
|
case $cur in
|
|
|
|
-*)
|
|
|
|
COMPREPLY=( $( compgen -W '\
|
|
|
|
''')
|
2015-06-07 06:01:02 +02:00
|
|
|
for opt in opts.values():
|
2015-02-15 04:05:27 +01:00
|
|
|
out.write(opt.long_opt)
|
|
|
|
out.write(' ')
|
|
|
|
|
|
|
|
out.write('''\
|
|
|
|
' -- "$cur" ) )
|
|
|
|
;;
|
|
|
|
''')
|
|
|
|
# If no option found for completion then complete with files.
|
|
|
|
out.write('''\
|
|
|
|
*)
|
|
|
|
_filedir
|
|
|
|
return 0
|
|
|
|
esac
|
|
|
|
return 0
|
|
|
|
}}
|
|
|
|
complete -F _{name} {name}
|
|
|
|
'''.format(name=name))
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if len(sys.argv) < 2:
|
2015-06-07 06:01:02 +02:00
|
|
|
print("Generates bash_completion using `/path/to/cmd --help'")
|
|
|
|
print("Usage: make_bash_completion.py /path/to/cmd")
|
2015-02-15 04:05:27 +01:00
|
|
|
exit(1)
|
|
|
|
name = os.path.basename(sys.argv[1])
|
|
|
|
opts = get_all_options(sys.argv[1])
|
|
|
|
output_case(sys.stdout, name, opts)
|