fix command completion

This commit is contained in:
Georgi Valkov 2012-12-14 15:35:00 +02:00
parent 4c26d11a4c
commit af64773a6e
2 changed files with 16 additions and 4 deletions

View File

@ -41,6 +41,8 @@ def autocomplete():
subcommand_name = [w for w in cwords if w in subcommands][0]
except IndexError:
subcommand_name = None
parser = create_main_parser()
# subcommand options
if subcommand_name:
# special case: 'help' subcommand has no options
@ -58,10 +60,13 @@ def autocomplete():
for dist in installed:
print(dist)
sys.exit(1)
subcommand = commands.get(subcommand_name)
subcommand = commands[subcommand_name](parser)
options += [(opt.get_opt_string(), opt.nargs) for opt in parser.option_list_all]
options += [(opt.get_opt_string(), opt.nargs)
for opt in subcommand.parser.option_list
for opt in subcommand.parser.option_list_all
if opt.help != optparse.SUPPRESS_HELP]
# filter out previously specified options from available options
prev_opts = [x.split('=')[0] for x in cwords[1:cword - 1]]
options = [(x, v) for (x, v) in options if x not in prev_opts]
@ -74,8 +79,6 @@ def autocomplete():
opt_label += '='
print(opt_label)
else:
parser = create_main_parser()
# show main parser options only when necessary
if current.startswith('-') or current.startswith('--'):
opts = [i.option_list for i in parser.option_groups]

View File

@ -95,6 +95,15 @@ class CustomOptionParser(optparse.OptionParser):
return group
@property
def option_list_all(self):
"""Get a list of all options, including those in option groups."""
res = self.option_list[:]
for i in self.option_groups:
res.extend(i.option_list)
return res
class ConfigOptionParser(CustomOptionParser):
"""Custom option parser which updates its defaults by by checking the