Python argparse 模块,SUPPRESS 实例源码
我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用argparse.SUPPRESS。
def generate_argparser():
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.ArgumentDefaultsHelpformatter,
epilog=LICENSE)
parser.add_argument('-d', '--nodedata', required=True, nargs=1,
help=("file containing paths of one or more"
"RESULT.node.score.csv files"))
parser.add_argument('-t', '--tree', type=open,
nargs=1,
help="tree file in Newick format")
parser.add_argument('-o', '--out',
help="new output files prefix")
parser.add_argument("-v", "--verbose", action="store_true")
# These args are hidden to pass through to the treedata object
parser.add_argument("-c", "--clade", help=argparse.SUPPRESS)
parser.add_argument("-s", "--startk", type=int, default=0,
help=argparse.SUPPRESS)
parser.add_argument("-p", "--stopk", help=argparse.SUPPRESS)
return parser
def _getdiscoveryArgParser(self, parent):
parser = argparse.ArgumentParser(parents=[parent])
parser.prog = '%s discover' % self.progName
parser.epilog = ('For test discovery all test modules must be '
'importable from the top level directory of the '
'project.')
parser.add_argument('-s', '--start-directory', dest='start',
help="Directory to start discovery ('.' default)")
parser.add_argument('-p', '--pattern', dest='pattern',
help="Pattern to match tests ('test*.py' default)")
parser.add_argument('-t', '--top-level-directory', dest='top',
help='Top level directory of project (defaults to '
'start directory)')
for arg in ('start', 'pattern', 'top'):
parser.add_argument(arg, nargs='?',
default=argparse.SUPPRESS,
help=argparse.SUPPRESS)
return parser
def generate_argparser():
"""Generates the argparsr ArgumentParser
"""
parser = argparse.ArgumentParser(
description=__doc__,
epilog=LICENSE)
parser.add_argument('-t',
help="input tree in newick format")
parser.add_argument('-d', '--data', type=os.path.abspath,
help=("CSV output from quartet_sampling"
" (RESULT.node.score.csv)"))
parser.add_argument("-c", help=argparse.SUPPRESS)
parser.add_argument("-v", action="store_true",
help="verbose screen output")
parser.add_argument("-s", help=argparse.SUPPRESS)
return parser
def add_dummy_redirect_argument(self, expected_name):
# type: (argparse.ArgumentParser,str) -> None
"""Adds a dummy redirect argument to the provided parser to catch typos when calling
the specified valid long-form name.
:param parser: Parser to which to add argument
:type parser: argparse.ArgumentParser
:param str expected_name: Valid long-form name for which to add dummy redirect
"""
self.add_argument(
expected_name[1:],
dest='dummy_redirect',
action='store_const',
const=expected_name[1:],
help=argparse.SUPPRESS
)
# ArgumentParser subclass confuses mypy
self.__dummy_arguments.append(expected_name[1:]) # type: ignore
def add_subparser(self, name: str, parser: argparse._SubParsersAction) -> argparse.ArgumentParser:
# pylint: disable=protected-access
description = '''Train the specified model on the specified dataset.'''
subparser = parser.add_parser(
name, description=description, help='Train a model')
subparser.add_argument('param_path',
type=str,
help='path to parameter file describing the model to be trained')
# This is necessary to preserve backward compatibility
serialization = subparser.add_mutually_exclusive_group(required=True)
serialization.add_argument('-s', '--serialization-dir',
type=str,
help='directory in which to save the model and its logs')
serialization.add_argument('--serialization_dir',
help=argparse.SUPPRESS)
subparser.set_defaults(func=train_model_from_args)
return subparser
def formatter_factory(show_defaults=True):
"""Formatter factory"""
def get_help_string(self, action):
lhelp = action.help
if isinstance(show_defaults, (list, tuple)):
if "-" + action.dest in show_defaults:
return lhelp
if '%(default)' not in action.help:
if action.default is not argparse.SUPPRESS:
defaulting_nargs = [argparse.OPTIONAL, argparse.ZERO_OR_MORE]
if action.option_strings or action.nargs in defaulting_nargs:
lhelp += ' (default: %(default)s)'
return lhelp
def default_help_string(self, action):
return action.help
if show_defaults is True:
ARPIFormatter._get_help_string = classmethod(get_help_string)
else:
ARPIFormatter._get_help_string = classmethod(default_help_string)
return ARPIFormatter
def _find_actions(self, subparsers, actions_module):
for attr in (a for a in dir(actions_module) if a.startswith('do_')):
# Replace underscores with hyphens in the commands
# displayed to the user
command = attr[3:].replace('_', '-')
callback = getattr(actions_module, attr)
desc = callback.__doc__ or ''
help = desc.strip().split('\n')[0]
arguments = getattr(callback, 'arguments', [])
subparser = subparsers.add_parser(command,
help=help,
description=desc,
add_help=False,
formatter_class=Helpformatter
)
subparser.add_argument('-h', '--help',
action='help',
help=argparse.SUPPRESS,
)
self.subcommands[command] = subparser
for (args, kwargs) in arguments:
subparser.add_argument(*args, **kwargs)
subparser.set_defaults(func=callback)
def _parser_add_opt(parser, opt):
"""Add an option to parser in two variants.
:param opt: option name (with underscores)
"""
dashed_opt = opt.replace("_", "-")
env_var = "OS_%s" % opt.upper()
arg_default = os.environ.get(env_var, "")
arg_help = "Defaults to env[%s]." % env_var
parser.add_argument(
"--os-%s" % dashed_opt,
Metavar="<%s>" % dashed_opt,
default=arg_default,
help=arg_help)
parser.add_argument(
"--os_%s" % opt,
help=argparse.SUPPRESS)
def finalizeOptions(self, availableTargets: list):
targetoption = self._parser.add_argument("targets", Metavar="TARGET", nargs=argparse.ZERO_OR_MORE,
help="The targets to build", choices=availableTargets + [[]])
if argcomplete and "_ARGCOMPLETE" in os.environ:
# if IS_FREEBSD: # FIXME: for some reason this won't work
excludes = ["-t", "--skip-dependencies"]
if sys.platform.startswith("freebsd"):
excludes += ["--freebsd-builder-copy-only", "--freebsd-builder-hostname",
"--freebsd-builder-output-path"]
visibleTargets = availableTargets.copy()
visibleTargets.remove("__run_everything__")
targetCompleter = argcomplete.completers.ChoicesCompleter(visibleTargets)
targetoption.completer = targetCompleter
# make sure we get target completion for the unparsed args too by adding another zero_or more options
# not sure why this works but it's a nice hack
unparsed = self._parser.add_argument("targets", type=list,
help=argparse.SUPPRESS, choices=availableTargets)
unparsed.completer = targetCompleter
argcomplete.autocomplete(
self._parser,
always_complete_options=None, # don't print -/-- by default
exclude=excludes, # hide these options from the output
print_suppressed=True, # also include target-specific options
)
def add_deprecated_argument(add_argument, argument_name, nargs):
"""Adds a deprecated argument with the name argument_name.
Deprecated arguments are not shown in the help. If they are used on
the command line,a warning is shown stating that the argument is
deprecated and no other action is taken.
:param callable add_argument: Function that adds arguments to an
argument parser/group.
:param str argument_name: Name of deprecated argument.
:param nargs: Value for nargs when adding the argument to argparse.
"""
if _ShowWarning not in configargparse.ACTION_TYPES_THAT_DONT_NEED_A_VALUE:
# In version 0.12.0 ACTION_TYPES_THAT_DONT_NEED_A_VALUE was
# changed from a set to a tuple.
if isinstance(configargparse.ACTION_TYPES_THAT_DONT_NEED_A_VALUE, set):
# pylint: disable=no-member
configargparse.ACTION_TYPES_THAT_DONT_NEED_A_VALUE.add(
_ShowWarning)
else:
configargparse.ACTION_TYPES_THAT_DONT_NEED_A_VALUE += (
_ShowWarning,)
add_argument(argument_name, action=_ShowWarning,
help=argparse.SUPPRESS, nargs=nargs)
def _parser_add_opt(parser,
help=argparse.SUPPRESS)
def setup_flex_args(argParser):
"""
Add flex specific arguments to other default args used by ngraph
"""
# use fixed point for flex backend
argParser.add_argument('--fixed_point',
action="store_true",
help=argparse.SUPPRESS)
# turn on flex verbosity for debug
argParser.add_argument('--flex_verbose',
help=argparse.SUPPRESS)
# collect flex data and save it to h5py File
argParser.add_argument('--collect_flex_data',
default=argparse.SUPPRESS)
def add_barmap_args(parser):
""" A collection of arguments that are used by BarMap """
ril.argparse_add_arguments(parser,
RNAsubopt=True,
barriers=True,
treekin=True,
noLP=True, temperature=True,
tmpdir=True, name=True, force=True, verbose=True,
start=True, stop=True, k0=True, tX=True, cutoff=True)
parser.add_argument("--plot_title", default='')
parser.add_argument("--pyplot",
help="Plot the simulation using matplotlib. Interpret the legend \
using the *log* output")
parser.add_argument("--xmgrace",
help="Print a plot for xmgrace. " + \
"Interpret the legend using the *log* output")
parser.add_argument("--adaptive",
help="Automatically raise suboptimal energy range if computations fail.")
parser.add_argument("--s_sortdir", default="/tmp", action="store", \
help=argparse.SUPPRESS)
return
def parser(self):
kwargs = {
"usage": self.usage,
"prog": self.prog
}
parser = argparse.ArgumentParser(**kwargs)
parser.add_argument("-v", "--version",
action="version", default=argparse.SUPPRESS,
version="%(prog)s (version " + __version__ + ")\n",
help="show program's version number and exit")
parser.add_argument("args", nargs="*", help=argparse.SUPPRESS)
keys = sorted(self.settings, key=self.settings.__getitem__)
for k in keys:
self.settings[k].add_option(parser)
return parser
def parse_hologram_send_args(parser):
# Create a subparser
parser.add_argument('--devicekey', help='Hologram device key (8 characters long)')
parser.add_argument('message', help='Message that will be sent to the cloud')
parser.add_argument('-v', action=VAction, dest='verbose', required=False)
parser.add_argument('--host', required=False, help=argparse.SUPPRESS)
parser.add_argument('-p', '--port', help=argparse.SUPPRESS)
parser.add_argument('--authtype', default='totp',
help='The authentication type used if HologramCloud is in use. Choose between \'totp\' and \'csrpsk\'')
# $ hologram send cloud ...
parse_cloud_args(parser)
# $ hologram send sms ...
parse_sms_args(parser)
# EFFECTS: Parses the send cloud options. Sets the default command_selected option
# to send_cloud.
def _make_print_cli_list_spec_action(cli_list_spec_file):
with open(cli_list_spec_file) as f:
str_cli_list_spec = f.read()
class _PrintCLIListSpecAction(argparse.Action):
def __init__(self,
option_strings,
dest=argparse.SUPPRESS,
default=argparse.SUPPRESS,
help=None):
super(_PrintCLIListSpecAction, self).__init__(
option_strings=option_strings,
dest=dest,
default=default,
nargs=0,
help=help)
def __call__(self, parser, namespace, values, option_string=None):
print str_cli_list_spec
parser.exit()
return _PrintCLIListSpecAction
def add_argument(self, action):
if action.help is not argparse.SUPPRESS:
# find all invocations
get_invocation = self._format_action_invocation
invocations = [get_invocation(action)]
current_indent = self._current_indent
for subaction in self._iter_indented_subactions(action):
# compensate for the indent that will be added
indent_chg = self._current_indent - current_indent
added_indent = 'x'*indent_chg
invocations.append(added_indent+get_invocation(subaction))
# print('inv',invocations)
# update the maximum item length
invocation_length = max([len(s) for s in invocations])
action_length = invocation_length + self._current_indent
self._action_max_length = max(self._action_max_length,
action_length)
# add the item to the list
self._add_item(self._format_action, [action])
def init_argparser_source_registry(
self, argparser, default=None, help=(
'comma separated list of registries to use for gathering '
'JavaScript sources from the given Python packages'
)):
"""
For setting up the source registry flag.
"""
argparser.add_argument(
'--source-registry', default=default,
dest=CALMJS_MODULE_REGISTRY_NAMES, action=StoreDelimitedList,
Metavar='registry_name[,registry_name[...]]',
help=help,
)
argparser.add_argument(
'--source-registries',
help=SUPPRESS,
)
def main(argv, environ):
parser = ArgumentParser(
prog='temboard-agent-adduser',
description="Add a new temboard-agent user.",
argument_default=UNDEFINED_ARGUMENT,
)
args = parser.parse_args(argv)
config = load_configuration(
specs=list_options_specs(), args=args, environ=environ,
)
# Load configuration from the configuration file.
username = ask_username(config)
password = ask_password()
hash_ = hash_password(username, password).decode('utf-8')
try:
with open(config.temboard['users'], 'a') as fd:
fd.write("%s:%s\n" % (username, hash_))
except IOError as e:
raise UserError(str(e))
else:
stdout.write("Done.\n")
def parser(self):
kwargs = {
"usage": self.usage, key=self.settings.__getitem__)
for k in keys:
self.settings[k].add_option(parser)
return parser
def parser(self):
kwargs = {
"usage": self.usage, key=self.settings.__getitem__)
for k in keys:
self.settings[k].add_option(parser)
return parser
def parser_(desc):
parser = parser_def_mgpu(desc)
checkptfile = 'cifar10_cnn_mgpu.weights.best.hdf5'
parser.add_argument(
'--checkpt', action='store',
const=checkptfile, default=SUPPRESS,
help='S|Save (overwrites) and load the model weights if available.'
'\nOptionally specify a file/filepath if the default name is '
'undesired.\n(default: {})'.format(checkptfile))
parser.add_argument('--aug', action='store_true', default=False,
help='S|Perform data augmentation on cifar10 set.\n')
parser.add_argument('--logdevp',
help='S|Log device placement in Tensorflow.\n')
args = parser.parse_args()
return args
def parser_(desc):
parser = parser_def_mgpu(desc)
remove_options(parser, ['--nccl', '--enqueue', '--syncopt', '--rdma'])
parser.add_argument(
'--checkpt',
help='S|Log device placement in Tensorflow.\n')
parser.add_argument('--datadir',
help='Data directory with Cifar10 dataset.')
args = parser.parse_args()
return args
def parser_(desc):
parser = parser_def_mgpu(desc)
remove_options(parser,
help='Data directory with Cifar10 dataset.')
args = parser.parse_args()
return args
def parser_(desc):
parser = ap.ArgumentParser(description=desc)
parser.add_argument(
'--mgpu',
const=-1, # if mgpu is specified but value not provided then -1
# if mgpu is not specified then defaults to 0 - single gpu
# mgpu = 0 if getattr(args,'mgpu',None) is None else args.mgpu
default=ap.SUPPRESS,
help='Run on multiple-GPUs using all available GPUs on a system.\n'
'If not passed does not use multiple GPU. If passed uses all GPUs.\n'
'Optionally specify a number to use that many GPUs. Another\n'
'approach is to specify CUDA_VISIBLE_DEVICES=0,1,... when calling\n'
'script and specify --mgpu to use this specified device list.\n'
'This option is only supported with TensorFlow backend.\n')
parser.add_argument('--epochs', default=5,
help='Number of epochs to run training for.')
args = parser.parse_args()
return args
def parse_args():
p = argparse.ArgumentParser()
g = p.add_argument_group('Logging options')
g.add_argument('--verbose', '-v',
action='store_const',
const='INFO',
dest='loglevel')
g.add_argument('--debug',
const='DEBUG',
dest='loglevel')
p.add_argument('--config', '-f',
default='siggen.yml')
p.add_argument('--nomidi',
action='store_true',
help=argparse.SUPPRESS)
p.add_argument('--list', '-l',
help='list available devices')
p.set_defaults(loglevel='WARN')
return p.parse_args()
def add(self, topic, *args, **kwargs):
"""Add a new command line argument.
:param str: help topic this should be listed under,can be None for
"always documented"
:param list *args: the names of this argument flag
:param dict **kwargs: varIoUs argparse settings for this argument
"""
if self.detect_defaults:
kwargs = self.modify_kwargs_for_default_detection(**kwargs)
if self.visible_topics[topic]:
if topic in self.groups:
group = self.groups[topic]
group.add_argument(*args, **kwargs)
else:
self.parser.add_argument(*args, **kwargs)
else:
kwargs["help"] = argparse.SUPPRESS
self.parser.add_argument(*args, **kwargs)
def add_deprecated_argument(add_argument,a warning is shown stating that the argument is
deprecated and no other action is taken.
:param callable add_argument: Function that adds arguments to an
argument parser/group.
:param str argument_name: Name of deprecated argument.
:param nargs: Value for nargs when adding the argument to argparse.
"""
class ShowWarning(argparse.Action):
"""Action to log a warning when an argument is used."""
def __call__(self, unused1, unused2, unused3, option_string=None):
sys.stderr.write(
"Use of {0} is deprecated.\n".format(option_string))
configargparse.ACTION_TYPES_THAT_DONT_NEED_A_VALUE.add(ShowWarning)
add_argument(argument_name, action=ShowWarning, nargs=nargs)
def _api_fields_to_parser_args(self, add_help=False):
if add_help:
parser.add_argument("--help", "-h",
help="show this help message and exit",
default=SUPPRESS, action='help')
for name, attrs in self.api_endpoint.fields.items():
if attrs['readonly']:
continue
kwargs = {'help': attrs['help_text']}
if attrs['type'] in ["related", "list"]:
kwargs['action'] = "append"
kwargs['type'] = str
elif attrs['type'] == "boolean":
kwargs['action'] = "store_true"
kwargs['default'] = False
elif attrs['type'] == "integer":
kwargs['type'] = int
parser.add_argument("--%s" % name, **kwargs)
def parser(self):
kwargs = {
"usage": self.usage, key=self.settings.__getitem__)
for k in keys:
self.settings[k].add_option(parser)
return parser
def initialize_parser():
# Top parser
parser = argparse.ArgumentParser(description="Docker Cloud CLI", prog='docker-cloud')
parser.add_argument('-v', '--version', action='version', version='%(prog)s ' + __version__)
parser.add_argument('--debug', help=argparse.SUPPRESS)
subparsers = parser.add_subparsers(title="Docker Cloud CLI commands", dest='cmd')
# Command Parsers
parsers.add_action_parser(subparsers)
parsers.add_container_parser(subparsers)
parsers.add_event_parser(subparsers)
parsers.add_exec_parser(subparsers)
parsers.add_login_parser(subparsers)
parsers.add_node_parser(subparsers)
parsers.add_nodecluster_parser(subparsers)
parsers.add_repository_parser(subparsers)
parsers.add_run_parser(subparsers)
parsers.add_service_parser(subparsers)
parsers.add_stack_parser(subparsers)
parsers.add_tag_parser(subparsers)
parsers.add_trigger_parser(subparsers)
parsers.add_up_parser(subparsers)
return parser
def parser(self):
kwargs = {
"usage": self.usage, key=self.settings.__getitem__)
for k in keys:
self.settings[k].add_option(parser)
return parser
def addCommandLineArgs(arg_parser):
"""Add logging option to an ArgumentParser."""
arg_parser.register("action", "log_levels", LogLevelAction)
arg_parser.register("action", "log_files", LogFileAction)
arg_parser.register("action", "log_help", LogHelpAction)
group = arg_parser.add_argument_group("Logging options")
group.add_argument(
"-l", "--log-level", dest="log_levels",
action="log_levels", Metavar="LOGGER:LEVEL", default=[],
help="Set log levels for individual loggers. See --help-logging for "
"complete details.")
group.add_argument(
"-L", "--log-file", dest="log_files",
action="log_files", Metavar="LOGGER:FILE",
help="Set log the output file for individual loggers. "
" See --help-logging for complete details.")
group.add_argument("--help-logging", action="log_help",
help=argparse.SUPPRESS)
def _find_actions(self, actions_module):
for attr in (a for a in dir(actions_module) if a.startswith('do_')):
# I prefer to be hypen-separated instead of underscores.
command = attr[3:].replace('_', help=help,
formatter_class=Helpformatter)
subparser.add_argument('-h', action='help',
help=argparse.SUPPRESS)
self.subcommands[command] = subparser
for (args, **kwargs)
subparser.set_defaults(func=callback)
def _parser_add_opt(parser,
help=argparse.SUPPRESS)
def _parser_add_opt(parser,
help=argparse.SUPPRESS)
def parser(self):
kwargs = {
"usage": self.usage, key=self.settings.__getitem__)
for k in keys:
self.settings[k].add_option(parser)
return parser
def get_parser(self, prog_name):
parser = super(CreateService, self).get_parser(prog_name)
parser.add_argument(
'type_or_name',
Metavar='<type>',
help=_('New service type (compute,image,identity,volume,etc)'),
)
type_or_name_group = parser.add_mutually_exclusive_group()
type_or_name_group.add_argument(
'--type',
help=argparse.SUPPRESS,
)
type_or_name_group.add_argument(
'--name',
Metavar='<name>',
help=_('New service name'),
)
parser.add_argument(
'--description',
Metavar='<description>',
help=_('New service description'),
)
return parser
def _find_actions(self, **kwargs)
subparser.set_defaults(func=callback)
def add_headers_args(parser):
headers_group = parser.add_argument_group('headers', 'headers for request')
headers_group.add_argument(
'-c', '--cookie',
action=Grouped, dest="headers.Cookie",
default=argparse.SUPPRESS, help="HTTP Cookie field"
)
headers_group.add_argument(
'-r', '--referer', dest="headers.Referer", help="HTTP Referer field"
)
headers_group.add_argument(
'-u', '--user-agent', dest="headers.User-Agent", help="HTTP User-Agent field"
)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。