Python argparse._CountAction() Examples

The following are code examples for showing how to use argparse._CountAction(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: zargparse   Author: ctil   File: zargparse.py    MIT License 6 votes vote down vote up
def _get_flag(action: argparse.Action):
        options = action.option_strings
        help_text = action.help
        if isinstance(action, (argparse._HelpAction,
                               argparse._StoreFalseAction,
                               argparse._StoreTrueAction,
                               argparse._CountAction,
                               argparse._AppendConstAction,
                               argparse._VersionAction,
                               argparse._StoreConstAction)):
            has_argument = False
            choices = []
        else:
            has_argument = True
            choices = action.choices
        return Flag(options, help_text, has_argument, choices) 
Example 2
Project: molotov   Author: loads   File: slave.py    Apache License 2.0 5 votes vote down vote up
def run_test(**options):
    """Runs a molotov test.
    """
    parser = _parser()
    fields = {}
    cli = []
    for action in parser._actions:
        if action.dest in ("help", "scenario"):
            continue
        op_str = action.option_strings[0]
        fields[action.dest] = op_str, action.const, type(action)

    for key, value in options.items():
        if key in fields:
            opt, const, type_ = fields[key]
            is_count = type_ is argparse._CountAction
            if const or is_count:
                if is_count:
                    cli += [opt] * value
                else:
                    cli.append(opt)
            else:
                cli.append(opt)
                cli.append(str(value))

    cli.append(options.pop("scenario", "loadtest.py"))
    args = parser.parse_args(args=cli)
    print("Running: molotov %s" % " ".join(cli))
    return run(args) 
Example 3
Project: django-corenlp   Author: arunchaganty   File: config.py    Apache License 2.0 5 votes vote down vote up
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 
Example 4
Project: django-corenlp   Author: arunchaganty   File: config.py    Apache License 2.0 5 votes vote down vote up
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 
Example 5
Project: ansibullbot   Author: ansible   File: triagers.py    GNU General Public License v3.0 5 votes vote down vote up
def add_dynamic_attr(cls, klass):
    parser = klass.create_parser()
    import argparse
    for action in parser._actions:
        if isinstance(action, argparse._HelpAction):
            continue

        if isinstance(action, (argparse._StoreTrueAction, argparse._StoreTrueAction)):
            cls.locals[action.dest] = [bool]
        elif isinstance(action, argparse._CountAction):
            cls.locals[action.dest] = [int]
        elif isinstance(action, (argparse._AppendAction, argparse._AppendConstAction)):
            cls.locals[action.dest] = [list]
        else:
            cls.locals[action.dest] = [action.type] 
Example 6
Project: springrts_smf_compiler   Author: Beherith   File: argparseui.py    Creative Commons Zero v1.0 Universal 5 votes vote down vote up
def create_ui(self):
        """
        create a dialog with the options from the parser
        and an ok and cancel button
        """
        self.addDescription()
        self.actionLookupTable = { 
                    type(argparse._HelpAction(None)) : self.makeHelpActionEntry,
                    type(argparse._VersionAction(None)) : self.makeHelpActionEntry,                   
                    type(argparse._StoreAction(None,None,None)): self.makeStoreConstEntry,
                    type(argparse._StoreConstAction(None,None,None)): self.makeStoreConstEntry,
                    type(argparse._StoreTrueAction(None, None)): self.makeStoreConstEntry,
                    type(argparse._StoreFalseAction(None, None)): self.makeStoreConstEntry,
                    type(argparse._StoreAction(None, None)) : self.makeStoreActionEntry,
                    type(argparse._CountAction(None, None)) : self.makeCountActionEntry,
                    type(argparse._AppendAction(None,None)) : self.makeAppendActionEntry,
                    type(argparse._AppendConstAction(None, None, None)) : self.makeCountActionEntry
                    }
                    
        for a in self.parser._get_optional_actions():
            try:
                self.actionLookupTable[type(a)](a, optional=True)
                #print ("Introspected type: {0}\n".format(a))
            except KeyError:
                print ("Unsupported type: {0}\n".format(a))

        for a in self.parser._get_positional_actions():
            try:
                self.actionLookupTable[type(a)](a, optional=True)
                #print ("Introspected type: {0}\n".format(a))
            except KeyError:
                print ("Unsupported type: {0}\n".format(a))

            
        self.addEpilog() 
Example 7
Project: stanza   Author: stanfordnlp   File: config.py    Apache License 2.0 5 votes vote down vote up
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