def create_main_parser(): parser_kw = { 'usage': '\n%prog <command> [options]', 'add_help_option': False, 'formatter': UpdatingDefaultsHelpFormatter(), 'name': 'global', 'prog': get_prog(), } parser = ConfigOptionParser(**parser_kw) parser.disable_interspersed_args() pip_pkg_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) parser.version = 'pip %s from %s (python %s)' % (__version__, pip_pkg_dir, sys.version[:3]) # add the general options gen_opts = cmdoptions.make_option_group(cmdoptions.general_group, parser) parser.add_option_group(gen_opts) parser.main = True # so the help formatter knows # create command listing for description command_summaries = get_summaries() description = [''] + ['%-27s %s' % (i, j) for i, j in command_summaries] parser.description = '\n'.join(description) return parser
def main(initial_args=None): if initial_args is None: initial_args = sys.argv[1:] autocomplete() options, args = parser.parse_args(initial_args) if options.help and not args: args = ['help'] if not args: parser.error('You must give a command ' '(use "%s help" to see a list of commands)' % get_prog()) command = args[0].lower() load_command(command) if command not in command_dict: close_commands = difflib.get_close_matches(command, command_names()) if close_commands: guess = close_commands[0] if args[1:]: guess = "%s %s" % (guess, " ".join(args[1:])) else: guess = 'install %s' % command error_dict = {'arg': command, 'guess': guess, 'script': os.path.basename(sys.argv[0])} parser.error('No command by the name %(script)s %(arg)s\n ' '(maybe you meant "%(script)s %(guess)s")' % error_dict) command = command_dict[command] return command.main(args[1:], options)
def __init__(self): assert self.name self.parser = ConfigOptionParser( usage=self.usage, prog='%s %s' % (get_prog(), self.name), version=parser.version, formatter=UpdatingDefaultsHelpFormatter(), name=self.name) for option in parser.option_list: if not option.dest or option.dest == 'help': # -h, --version, etc continue self.parser.add_option(option) command_dict[self.name] = self
def __init__(self): parser_kw = { 'usage': self.usage, 'prog': '%s %s' % (get_prog(), self.name), 'formatter': UpdatingDefaultsHelpFormatter(), 'add_help_option': False, 'name': self.name, 'description': self.__doc__, } self.parser = ConfigOptionParser(**parser_kw) # Commands should add options to this option group optgroup_name = '%s Options' % self.name.capitalize() self.cmd_opts = optparse.OptionGroup(self.parser, optgroup_name) # Add the general options gen_opts = cmdoptions.make_option_group(cmdoptions.general_group, self.parser) self.parser.add_option_group(gen_opts)
def create_main_parser(): parser_kw = { 'usage': '\n%prog <command> [options]', 'add_help_option': False, 'formatter': UpdatingDefaultsHelpFormatter(), 'name': 'global', 'prog': get_prog(), } parser = ConfigOptionParser(**parser_kw) genopt = optparse.OptionGroup(parser, 'General Options') parser.disable_interspersed_args() # having a default version action just causes trouble parser.version = version for opt in standard_options: genopt.add_option(opt) parser.add_option_group(genopt) return parser
def __init__(self, main_parser): parser_kw = { 'usage': self.usage, 'prog': '%s %s' % (get_prog(), self.name), 'formatter': UpdatingDefaultsHelpFormatter(), 'add_help_option': False, 'name': self.name, 'description': self.__doc__, } self.main_parser = main_parser self.parser = ConfigOptionParser(**parser_kw) # Commands should add options to this option group optgroup_name = '%s Options' % self.name.capitalize() self.cmd_opts = optparse.OptionGroup(self.parser, optgroup_name) # Re-add all options and option groups. for group in main_parser.option_groups: self._copy_option_group(self.parser, group) # Copies all general options from the main parser. self._copy_options(self.parser, main_parser.option_list)
def run(self): cmd = commands[self.arguments[0]] prog = '%s %s' % (get_prog(), cmd.name) usage = dedent(cmd.usage.replace('%prog', prog)) node = nodes.literal_block(usage, usage) return [node]
try: pip_dist = pkg_resources.get_distribution('pip') version = '%s from %s (python %s)' % ( pip_dist, pip_dist.location, sys.version[:3]) except pkg_resources.DistributionNotFound: # when running pip.py without installing version = None parser = ConfigOptionParser( usage='%prog COMMAND [OPTIONS]', version=version, add_help_option=False, formatter=UpdatingDefaultsHelpFormatter(), name='global', prog=get_prog()) parser.add_option( '-h', '--help', dest='help', action='store_true', help='Show help') parser.add_option( # Run only if inside a virtualenv, bail if not. '--require-virtualenv', '--require-venv', dest='require_venv', action='store_true', default=False, help=optparse.SUPPRESS_HELP) parser.add_option(