本文整理汇总了Python中argparse.OPTIONAL属性的典型用法代码示例。如果您正苦于以下问题:Python argparse.OPTIONAL属性的具体用法?Python argparse.OPTIONAL怎么用?Python argparse.OPTIONAL使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类argparse
的用法示例。
在下文中一共展示了argparse.OPTIONAL属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import argparse [as 别名]
# 或者: from argparse import OPTIONAL [as 别名]
def __init__(self, option_strings, nargs=1, dest='vmnames', help=None,
**kwargs):
if help is None:
if nargs == argparse.OPTIONAL:
help = 'at most one domain name'
elif nargs == 1:
help = 'a domain name'
elif nargs == argparse.ZERO_OR_MORE:
help = 'zero or more domain names'
elif nargs == argparse.ONE_OR_MORE:
help = 'one or more domain names'
elif nargs > 1:
help = '%s domain names' % nargs
else:
raise argparse.ArgumentError(
nargs, "Passed unexpected value {!s} as {!s} nargs ".format(
nargs, dest))
super(VmNameAction, self).__init__(option_strings, dest=dest, help=help,
nargs=nargs, **kwargs)
示例2: _get_help_string
# 需要导入模块: import argparse [as 别名]
# 或者: from argparse import OPTIONAL [as 别名]
def _get_help_string(self, action):
"""Place default value in help string."""
h = action.help
if '%(default)' not in action.help:
if action.default != '' and action.default != [] and \
action.default is not None and \
not isinstance(type(action.default), bool):
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:
if '\n' in h:
lines = h.splitlines()
lines[0] += ' (default: %(default)s)'
h = '\n'.join(lines)
else:
h += ' (default: %(default)s)'
return h
示例3: test_apcustom_narg_tuple_zero_base
# 需要导入模块: import argparse [as 别名]
# 或者: from argparse import OPTIONAL [as 别名]
def test_apcustom_narg_tuple_zero_base():
parser = Cmd2ArgumentParser()
arg = parser.add_argument('arg', nargs=(0,))
assert arg.nargs == argparse.ZERO_OR_MORE
assert arg.nargs_range is None
assert "[arg [...]]" in parser.format_help()
parser = Cmd2ArgumentParser()
arg = parser.add_argument('arg', nargs=(0, 1))
assert arg.nargs == argparse.OPTIONAL
assert arg.nargs_range is None
assert "[arg]" in parser.format_help()
parser = Cmd2ArgumentParser()
arg = parser.add_argument('arg', nargs=(0, 3))
assert arg.nargs == argparse.ZERO_OR_MORE
assert arg.nargs_range == (0, 3)
assert "arg{0..3}" in parser.format_help()
# noinspection PyUnresolvedReferences
示例4: configure_app
# 需要导入模块: import argparse [as 别名]
# 或者: from argparse import OPTIONAL [as 别名]
def configure_app(app, parser):
parser.add_argument(
'glucose',
help='JSON-encoded glucose data file in reverse-chronological order'
)
parser.add_argument(
'--prediction-time',
type=int,
nargs=argparse.OPTIONAL,
help='The total length of forward trend extrapolation in minutes. Defaults to 30.'
)
parser.add_argument(
'--calibrations',
nargs=argparse.OPTIONAL,
help='JSON-encoded sensor calibrations data file in reverse-chronological order'
)
示例5: _get_help_string
# 需要导入模块: import argparse [as 别名]
# 或者: from argparse import OPTIONAL [as 别名]
def _get_help_string(self, action):
"""Place default value in help string."""
h = action.help
if '%(default)' not in action.help:
if action.default != '' and action.default != [] and action.default is not None and type(action.default) != bool:
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:
if '\n' in h:
lines = h.splitlines()
lines[0] += ' (default: %(default)s)'
h = '\n'.join(lines)
else:
h += ' (default: %(default)s)'
return h
示例6: _get_help_string
# 需要导入模块: import argparse [as 别名]
# 或者: from argparse import OPTIONAL [as 别名]
def _get_help_string(self, action):
help = action.help
if not action.default:
return help
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:
help += ' (default: %(default)s)'
return help
示例7: _match_argument
# 需要导入模块: import argparse [as 别名]
# 或者: from argparse import OPTIONAL [as 别名]
def _match_argument(self, action, arg_strings_pattern):
# - https://github.com/streamlink/streamlink/issues/971
# - https://bugs.python.org/issue9334
# match the pattern for this action to the arg strings
nargs_pattern = self._get_nargs_pattern(action)
match = argparse._re.match(nargs_pattern, arg_strings_pattern)
# if no match, see if we can emulate optparse and return the
# required number of arguments regardless of their values
if match is None:
nargs = action.nargs if action.nargs is not None else 1
if isinstance(nargs, numbers.Number) and len(arg_strings_pattern) >= nargs:
return nargs
# raise an exception if we weren't able to find a match
if match is None:
nargs_errors = {
None: argparse._('expected one argument'),
argparse.OPTIONAL: argparse._('expected at most one argument'),
argparse.ONE_OR_MORE: argparse._('expected at least one argument'),
}
default = argparse.ngettext('expected %s argument',
'expected %s arguments',
action.nargs) % action.nargs
msg = nargs_errors.get(action.nargs, default)
raise argparse.ArgumentError(action, msg)
# return the number of arguments matched
return len(match.group(1))
示例8: _get_help_string
# 需要导入模块: import argparse [as 别名]
# 或者: from argparse import OPTIONAL [as 别名]
def _get_help_string(self, action):
help = action.help
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:
help += ' (default: %(default)s)'
if (
hasattr(action, 'recommended')
and action.recommended
and action.recommended != action.default
):
help += '(recommended: %(recommended)s)'
help = help.replace(')(recommended', ', recommended')
return help
示例9: _get_help_string
# 需要导入模块: import argparse [as 别名]
# 或者: from argparse import OPTIONAL [as 别名]
def _get_help_string(self, action):
help_text = action.help
if '%(default)' not in action.help and action.default is not argparse.SUPPRESS:
if action.option_strings or action.nargs in [argparse.OPTIONAL, argparse.ZERO_OR_MORE]:
if (
(not isinstance(action.default, bool) and isinstance(action.default, int))
or (isinstance(action.default, str) and action.default)
):
help_text += ' (default: %(default)s)'
return help_text
示例10: _get_help_string
# 需要导入模块: import argparse [as 别名]
# 或者: from argparse import OPTIONAL [as 别名]
def _get_help_string(self, action):
self.default = None
if action.default not in (None, argparse.SUPPRESS):
if action.option_strings or action.nargs in (argparse.OPTIONAL, argparse.ZERO_OR_MORE):
self.default = self.__format_map.get(action.dest, lambda s: s)(action.default)
return action.help
示例11: __init__
# 需要导入模块: import argparse [as 别名]
# 或者: from argparse import OPTIONAL [as 别名]
def __init__(self, arg_action: argparse.Action) -> None:
self.action = arg_action
self.min = None
self.max = None
self.count = 0
self.is_remainder = (self.action.nargs == argparse.REMAINDER)
# Check if nargs is a range
nargs_range = getattr(self.action, ATTR_NARGS_RANGE, None)
if nargs_range is not None:
self.min = nargs_range[0]
self.max = nargs_range[1]
# Otherwise check against argparse types
elif self.action.nargs is None:
self.min = 1
self.max = 1
elif self.action.nargs == argparse.OPTIONAL:
self.min = 0
self.max = 1
elif self.action.nargs == argparse.ZERO_OR_MORE or self.action.nargs == argparse.REMAINDER:
self.min = 0
self.max = constants.INFINITY
elif self.action.nargs == argparse.ONE_OR_MORE:
self.min = 1
self.max = constants.INFINITY
else:
self.min = self.action.nargs
self.max = self.action.nargs
# noinspection PyProtectedMember
示例12: _get_help_string
# 需要导入模块: import argparse [as 别名]
# 或者: from argparse import OPTIONAL [as 别名]
def _get_help_string(self, action):
"""
Appends the default argument value to the help string for non-required args that have default values.
This implementation is loosely based off of the argparse.ArgumentDefaultsHelpFormatter.
"""
help_string = action.help
if not action.required:
if action.default not in (argparse.SUPPRESS, None):
defaulting_nargs = [argparse.OPTIONAL, argparse.ZERO_OR_MORE]
if action.option_strings or action.nargs in defaulting_nargs:
# using old string formatting style here because argparse internals use that
help_string += ' (default: %(default)s)'
return help_string
示例13: parse_qubes_app
# 需要导入模块: import argparse [as 别名]
# 或者: from argparse import OPTIONAL [as 别名]
def parse_qubes_app(self, parser, namespace):
assert hasattr(namespace, 'app')
setattr(namespace, 'domains', [])
app = namespace.app
if hasattr(namespace, 'all_domains') and namespace.all_domains:
namespace.domains = [
vm
for vm in app.domains
if not vm.klass == 'AdminVM' and
vm.name not in namespace.exclude
]
else:
if hasattr(namespace, 'exclude') and namespace.exclude:
parser.error('--exclude can only be used with --all')
if self.nargs == argparse.OPTIONAL:
vm_name = getattr(namespace, self.dest, None)
if vm_name is not None:
try:
namespace.domains += [app.domains[vm_name]]
except KeyError:
parser.error('no such domain: {!r}'.format(vm_name))
else:
for vm_name in getattr(namespace, self.dest):
try:
namespace.domains += [app.domains[vm_name]]
except KeyError:
parser.error('no such domain: {!r}'.format(vm_name))
示例14: init_info_parser
# 需要导入模块: import argparse [as 别名]
# 或者: from argparse import OPTIONAL [as 别名]
def init_info_parser(sub_parsers):
''' Add 'info' action related options '''
info_parser = sub_parsers.add_parser(
'info', aliases=('i',), help='info about volume')
info_parser.add_argument(metavar='VM:VOLUME', dest='volume',
action=qubesadmin.tools.VMVolumeAction)
info_parser.add_argument(dest='property', action='store',
nargs=argparse.OPTIONAL,
help='Show only this property instead of all of them; use '
'\'revisions\' to list available revisions')
info_parser.set_defaults(func=info_volume)
示例15: _get_help_string
# 需要导入模块: import argparse [as 别名]
# 或者: from argparse import OPTIONAL [as 别名]
def _get_help_string(self, action):
'''
This module was taken from the RawDescriptionHelpFormatter class
within argparse. It deals with the formatting of the description string
and allows properly formatted descriptions ot be printed without line
wrapping.
'''
help = action.help
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:
help += ' (default: %(default)s)'
return help