Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
make completion allow global options before subcommand
  • Loading branch information
Carl Meyer committed Mar 9, 2010
1 parent 107835e commit ca9acb2
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions pip/__init__.py
Expand Up @@ -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):
Expand All @@ -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]
Expand All @@ -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):
Expand Down

0 comments on commit ca9acb2

Please sign in to comment.