我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用optparse.OptionGroup()。
def __init__(self, isolated=False): parser_kw = { 'usage': self.usage, 'prog': '%s %s' % (get_prog(), self.name), 'formatter': UpdatingDefaultsHelpFormatter(), 'add_help_option': False, 'name': self.name, 'description': self.__doc__, 'isolated': isolated, } 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 _add_mouse_options(parser, profile): group = OptionGroup(parser, "%s Options" % profile["name"]) command_names = sorted(profile["commands"].keys()) for command_name in command_names: if command_name == "save": continue command = profile["commands"][command_name] adder_name = "_add_%s_option" % command["value_type"] if adder_name not in globals(): raise Exception("Unable to create a CLI option for value type '%s'" % command["value_type"]) # noqa globals()[adder_name](group, command_name, command) group.add_option( "-r", "--reset", help="Reset all options to their factory values", action="store_true" ) parser.add_option_group(group)
def define_options(self, option_parser): opt_group = OptionGroup(option_parser, 'Compaction Setter Options') opt_group.add_option( '--dry-run', action="store_true", help="If set, will not set any topic configs, but will still run as if it is" ) opt_group.add_option( '--config-path', dest='config_path', type='str', default='/nail/srv/configs/data_pipeline_tools.yaml', help='Config path for CompactionSetter (default: %default)' ) opt_group.add_option( '--whitelist-topic', type='str', help='A single topic for which log compaction should be turned on' ) return opt_group
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 _add_plugin_options(self, available_plugins): """Recovers the list of command line options implemented by the available plugins and adds them to the command line parser. """ for plugin_class in available_plugins: plugin_desc = plugin_class.get_interface() # Add the current plugin's commands to the parser group = OptionGroup(self._parser, plugin_desc.title, plugin_desc.description) for cmd in plugin_desc.get_commands(): group.add_option(cmd) # Add the current plugin's options to the parser for option in plugin_desc.get_options(): group.add_option(option) self._parser.add_option_group(group) # Todo: Move formatting stuff to another file
def workaround_optparse_bug9161(): op = optparse.OptionParser() og = optparse.OptionGroup(op, 'foo') try: og.add_option('-t') except TypeError: real_add_option = optparse.OptionGroup.add_option def _compat_add_option(self, *args, **kwargs): enc = lambda v: ( v.encode('ascii', 'replace') if isinstance(v, compat_str) else v) bargs = [enc(a) for a in args] bkwargs = dict( (k, enc(v)) for k, v in kwargs.items()) return real_add_option(self, *bargs, **bkwargs) optparse.OptionGroup.add_option = _compat_add_option
def add_standard_options(self): group = optparse.OptionGroup(self.parser, 'Standard Options') # add standard options that all commands get group.add_option('-D', '--debug', action='store_true', help='Turn on all debugging output') group.add_option('--debugger', action='store_true', default=False, help='Enable interactive debugger on error') group.add_option('-U', '--url', action='store', help='Override service URL with value provided') group.add_option('--region', action='store', help='Name of the region to connect to') group.add_option('-I', '--access-key-id', action='store', help='Override access key value') group.add_option('-S', '--secret-key', action='store', help='Override secret key value') group.add_option('--version', action='store_true', help='Display version string') if self.Filters: self.group.add_option('--help-filters', action='store_true', help='Display list of available filters') self.group.add_option('--filter', action='append', metavar=' name=value', help='A filter for limiting the results') self.parser.add_option_group(group)
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 add_options(self, parser): """ Populate option parse with options available for this command """ group = OptionGroup(parser, "Global Options") group.add_option("--logfile", metavar="FILE", help="log file. if omitted stderr will be used") group.add_option("-L", "--loglevel", metavar="LEVEL", default=None, help="log level (default: %s)" % self.settings['LOG_LEVEL']) group.add_option("--nolog", action="store_true", help="disable logging completely") group.add_option("--profile", metavar="FILE", default=None, help="write python cProfile stats to FILE") group.add_option("--lsprof", metavar="FILE", default=None, help="write lsprof profiling stats to FILE") group.add_option("--pidfile", metavar="FILE", help="write process ID to FILE") group.add_option("-s", "--set", action="append", default=[], metavar="NAME=VALUE", help="set/override setting (may be repeated)") group.add_option("--pdb", action="store_true", help="enable pdb on failure") parser.add_option_group(group)