本文整理匯總了Python中argparse._StoreConstAction方法的典型用法代碼示例。如果您正苦於以下問題:Python argparse._StoreConstAction方法的具體用法?Python argparse._StoreConstAction怎麽用?Python argparse._StoreConstAction使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類argparse
的用法示例。
在下文中一共展示了argparse._StoreConstAction方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _get_help_string
# 需要導入模塊: import argparse [as 別名]
# 或者: from argparse import _StoreConstAction [as 別名]
def _get_help_string(self, action):
if isinstance(action, argparse._StoreConstAction):
return action.help
else:
return super()._get_help_string(action)
示例2: main
# 需要導入模塊: import argparse [as 別名]
# 或者: from argparse import _StoreConstAction [as 別名]
def main():
if len(sys.argv) != 2:
return 1
comp_words = iter(sys.argv[1].split()[1:])
parser = get_parser()
seen = []
for word in comp_words:
if word in parser.choices:
parser = parser[word]
continue
actions = {name: action for action in parser._actions for name in action.option_strings}
first, _, rest = word.partition('=')
is_one_word_store_action = rest and first in actions
if is_one_word_store_action:
word = first
seen.append(word)
try:
action = actions[word]
except KeyError:
break
if isinstance(action, argparse._StoreAction) and not isinstance(action, argparse._StoreConstAction):
if not is_one_word_store_action:
try:
next(comp_words)
except StopIteration:
break
options = list(parser.choices)
options = [format_option(option, argument_required=False) for option in options]
options.extend(get_options(parser))
options = [option for option in options if option.rstrip('= ') not in seen]
print('\n'.join(options))
return 0
示例3: convert_setting_to_command_line_arg
# 需要導入模塊: import argparse [as 別名]
# 或者: from argparse import _StoreConstAction [as 別名]
def convert_setting_to_command_line_arg(self, action, key, value):
args = []
if action is None:
command_line_key = \
self.get_command_line_key_for_unknown_config_file_setting(key)
else:
command_line_key = action.option_strings[-1]
if isinstance(action, argparse._StoreTrueAction):
if value is True:
args.append(command_line_key)
elif isinstance(action, argparse._StoreFalseAction):
if value is False:
args.append(command_line_key)
elif isinstance(action, argparse._StoreConstAction):
if value == action.const:
args.append(command_line_key)
elif isinstance(action, argparse._CountAction):
for _ in range(value):
args.append(command_line_key)
elif action is not None and value == action.default:
pass
elif isinstance(value, list):
args.append(command_line_key)
args.extend([str(e) for e in value])
else:
args.append(command_line_key)
args.append(str(value))
return args
示例4: format_cli
# 需要導入模塊: import argparse [as 別名]
# 或者: from argparse import _StoreConstAction [as 別名]
def format_cli (self, report):
usage = self.app.devices[report.fields.get('device')]
task = self.app.actions.commands['add'].usages.commands[usage.name].method.commands[report.fields['use']]
line = [ 'openaps', 'use', usage.name, report.fields.get('use') ]
params = [ ]
config = task.method.from_ini(dict(**report.fields))
for act in task.method.parser._actions:
def accrue (switch):
if switch.startswith('-'):
params.insert(0, switch)
else:
params.append(switch)
# if act.dest in report.fields:
if act.dest in config:
if act.option_strings:
if report.fields.get(act.dest):
if type(act) in [argparse._StoreTrueAction, argparse._StoreFalseAction ]:
expected = act.const
expected = act.default
found = config.get(act.dest)
if type(act) is argparse._StoreFalseAction:
expected = True
found = found
if expected != found:
accrue(act.option_strings[0])
elif type(act) in [argparse._StoreConstAction, ]:
expected = act.default
found = config.get(act.dest)
if expected != found:
accrue(act.option_strings[0])
elif type(act) in [argparse._AppendAction, ]:
if config.get(act.dest) != act.default:
for item in config.get(act.dest):
accrue(act.option_strings[0] + ' ' + item + '')
pass
elif type(act) in [argparse._StoreAction, ]:
if config.get(act.dest) != act.default:
accrue(act.option_strings[0] + ' "' + report.fields.get(act.dest) + '"')
else:
accrue(act.option_strings[0] + ' "' + report.fields.get(act.dest) + '"')
else:
accrue(report.fields.get(act.dest))
return ' '.join(line + params)
示例5: _add_to_defaults
# 需要導入模塊: import argparse [as 別名]
# 或者: from argparse import _StoreConstAction [as 別名]
def _add_to_defaults(cls, a_parser, defaults, a_args=None, a_namespace=None, a_parsed_args=None):
actions = [
a for a in a_parser._actions
if isinstance(a, _StoreAction) or isinstance(a, _StoreConstAction)
]
args_dict = {}
try:
if isinstance(a_parsed_args, dict):
args_dict = a_parsed_args
else:
if a_parsed_args:
args_dict = a_parsed_args.__dict__
else:
args_dict = call_original_argparser(a_parser, args=a_args, namespace=a_namespace).__dict__
defaults_ = {
a.dest: args_dict.get(a.dest) if (args_dict.get(a.dest) is not None) else ''
for a in actions
}
except Exception:
# don't crash us if we failed parsing the inputs
defaults_ = {
a.dest: a.default if a.default is not None else ''
for a in actions
}
full_args_dict = copy(defaults)
full_args_dict.update(args_dict)
defaults.update(defaults_)
# deal with sub parsers
sub_parsers = [
a for a in a_parser._actions
if isinstance(a, _SubParsersAction)
]
for sub_parser in sub_parsers:
if sub_parser.dest and sub_parser.dest != SUPPRESS:
defaults[sub_parser.dest] = full_args_dict.get(sub_parser.dest) or ''
for choice in sub_parser.choices.values():
# recursively parse
defaults = cls._add_to_defaults(
a_parser=choice,
defaults=defaults,
a_parsed_args=a_parsed_args or full_args_dict
)
return defaults