我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用pip.commands.commands()。
def run(self, options, args): from pip.commands import commands, get_similar_commands try: # 'pip help' with no args is handled by pip.__init__.parseopt() cmd_name = args[0] # the command we need help for except IndexError: return SUCCESS if cmd_name not in commands: guess = get_similar_commands(cmd_name) msg = ['unknown command "%s"' % cmd_name] if guess: msg.append('maybe you meant "%s"' % guess) raise CommandError(' - '.join(msg)) command = commands[cmd_name]() command.parser.print_help() return SUCCESS
def main(initial_args=None): if initial_args is None: initial_args = sys.argv[1:] autocomplete() try: cmd_name, cmd_args = parseopts(initial_args) except PipError: e = sys.exc_info()[1] sys.stderr.write("ERROR: %s" % e) sys.stderr.write(os.linesep) sys.exit(1) command = commands[cmd_name]() return command.main(cmd_args)
def run(self, options, args): from pip.commands import commands, get_similar_commands try: # 'pip help' with no args is handled by pip.__init__.parseopt() cmd_name = args[0] # the command we need help for except IndexError: return SUCCESS if cmd_name not in commands: guess = get_similar_commands(cmd_name) msg = ['unknown command "%s"' % cmd_name] if guess: msg.append('maybe you meant "%s"' % guess) raise CommandError(' - '.join(msg)) command = commands[cmd_name](self.main_parser) # instantiate command.parser.print_help() return SUCCESS
def main(initial_args=None): if initial_args is None: initial_args = sys.argv[1:] autocomplete() try: cmd_name, options, args, parser = parseopts(initial_args) except PipError: e = sys.exc_info()[1] sys.stderr.write("ERROR: %s" % e) sys.stderr.write(os.linesep) sys.exit(1) command = commands[cmd_name](parser) # see baseparser.Command return command.main(args[1:], options)
def parseopts(args): parser = create_main_parser() # Note: parser calls disable_interspersed_args(), so the result of this call # is to split the initial args into the general options before the # subcommand and everything else. # For example: # args: ['--timeout=5', 'install', '--user', 'INITools'] # general_options: ['--timeout==5'] # args_else: ['install', '--user', 'INITools'] general_options, args_else = parser.parse_args(args) # --version if general_options.version: sys.stdout.write(parser.version) sys.stdout.write(os.linesep) sys.exit() # pip || pip help -> print_help() if not args_else or (args_else[0] == 'help' and len(args_else) == 1): parser.print_help() sys.exit() # the subcommand name cmd_name = args_else[0].lower() #all the args without the subcommand cmd_args = args[:] cmd_args.remove(args_else[0].lower()) if cmd_name not in commands: guess = get_similar_commands(cmd_name) msg = ['unknown command "%s"' % cmd_name] if guess: msg.append('maybe you meant "%s"' % guess) raise CommandError(' - '.join(msg)) return cmd_name, cmd_args
def parseopts(args): parser = create_main_parser() parser.main = True # so the help formatter knows # create command listing command_summaries = get_summaries() description = [''] + ['%-27s %s' % (i, j) for i, j in command_summaries] parser.description = '\n'.join(description) options, args = parser.parse_args(args) if options.version: sys.stdout.write(parser.version) sys.stdout.write(os.linesep) sys.exit() # pip || pip help || pip --help -> print_help() if not args or (args[0] == 'help' and len(args) == 1): parser.print_help() sys.exit() if not args: msg = ('You must give a command ' '(use "pip --help" to see a list of commands)') raise CommandError(msg) command = args[0].lower() if command not in commands: guess = get_similar_commands(command) msg = ['unknown command "%s"' % command] if guess: msg.append('maybe you meant "%s"' % guess) raise CommandError(' - '.join(msg)) return command, options, args, parser