提交 ca9acb26 编写于 作者: C Carl Meyer

make completion allow global options before subcommand

上级 107835ec
......@@ -30,18 +30,17 @@ def autocomplete():
subcommands = [cmd for cmd, cls in command_dict.items() if not cls.hidden]
options = []
# subcommand
if cword == 1:
# show options of main parser only when necessary
if current.startswith('-') or current.startswith('--'):
subcommands += [opt.get_opt_string()
for opt in parser.option_list
if opt.help != optparse.SUPPRESS_HELP]
print ' '.join(filter(lambda x: x.startswith(current), subcommands))
try:
subcommand_name = [w for w in cwords if w in subcommands][0]
except IndexError:
subcommand_name = None
# subcommand options
# special case: the 'help' subcommand has no options
elif cwords[0] in subcommands and cwords[0] != 'help':
if subcommand_name:
# special case: 'help' subcommand has no options
if subcommand_name == 'help':
sys.exit(1)
# special case: list locally installed dists for uninstall command
if cwords[0] == 'uninstall' and not current.startswith('-'):
if subcommand_name == 'uninstall' and not current.startswith('-'):
installed = []
lc = current.lower()
for dist in get_installed_distributions(local_only=True):
......@@ -52,7 +51,7 @@ def autocomplete():
for dist in installed:
print dist
sys.exit(1)
subcommand = command_dict.get(cwords[0])
subcommand = command_dict.get(subcommand_name)
options += [(opt.get_opt_string(), opt.nargs)
for opt in subcommand.parser.option_list
if opt.help != optparse.SUPPRESS_HELP]
......@@ -67,6 +66,13 @@ def autocomplete():
if option[1]:
opt_label += '='
print opt_label
else:
# show options of main parser only when necessary
if current.startswith('-') or current.startswith('--'):
subcommands += [opt.get_opt_string()
for opt in parser.option_list
if opt.help != optparse.SUPPRESS_HELP]
print ' '.join(filter(lambda x: x.startswith(current), subcommands))
sys.exit(1)
def main(initial_args=None):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册