Skip to content
Browse files

make completion allow global options before subcommand

  • Loading branch information...
1 parent 107835e commit ca9acb2626b3ec88b94577014d7dd4f59d1794ce @carljm carljm committed Mar 9, 2010
Showing with 17 additions and 11 deletions.
  1. +17 −11 pip/__init__.py
View
28 pip/__init__.py
@@ -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):

0 comments on commit ca9acb2

Please sign in to comment.
Something went wrong with that request. Please try again.