我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用argparse._StoreConstAction()。
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
def _get_help_string(self, action): help = action.help if '(default' not in help and type(action) not in (argparse._StoreConstAction, argparse._StoreTrueAction, argparse._StoreFalseAction): 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
def get_input_object(self, action, prefix): input_parameters = {} input_parameters['class'] = self.get_class(prefix) input_parameters['name'] = self.get_id(action, prefix) input_parameters['id'] = self.get_id(action, prefix) input_type = web.form.Textbox if self.get_choices(action): input_type = web.form.Dropdown input_parameters['args'] = [choice for choice in action.choices] if self.get_multiple(action): input_parameters['multiple'] = 'multiple' input_parameters['size'] = 4 elif isinstance(action, (argparse._StoreTrueAction, argparse._StoreFalseAction, argparse._StoreConstAction)): input_type = web.form.Checkbox input_parameters['checked'] = True if action.default else False input_parameters['value'] = action.const else: input_parameters['value'] = action.default if action.default else "" if isinstance(action, argparse._SubParsersAction): input_parameters['onChange'] = "javascript: update_show(this);" input_parameters['value'] = action.choices.keys()[0] if len(action.option_strings): input_parameters['default'] = action.default # if optional argument may be present with either 1 or no parameters, the default shifts # to being the no parameter's value. this is mearly to properly display actual values to the user if action.nargs == '?': input_parameters['value'] = action.const # TODO: support these actions: append, append_const, count self._actions[self.get_id(action, prefix)] = action input_object = input_type(**input_parameters) input_object.description = self.get_description(action) input_object.nargs = self.get_nargs(action) input_object.help = self.get_help(action) input_object.disposition = self.get_disposition(action) input_object.subparser = self.get_subparser(action) input_object.choices = self.get_choices(action) return input_object