How to use the jsonargparse.ActionYesNo function in jsonargparse

To help you get started, we’ve selected a few jsonargparse examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github omni-us / jsonargparse / jsonargparse.py View on Github external
"""Sets the value of env_prefix to an ActionParser and all sub ActionParsers it contains.

    Args:
        parser (ArgumentParser): The parser to which the action belongs.
        action (ActionParser): The action to set its env_prefix.
    """
    if not isinstance(action, ActionParser):
        raise ValueError('Expected action to be an ActionParser.')
    action._parser.env_prefix = parser.env_prefix
    action._parser.default_env = parser.default_env
    option_string_actions = {}
    for key, val in action._parser._option_string_actions.items():
        option_string_actions[re.sub('^--', '--'+prefix+'.', key)] = val
    action._parser._option_string_actions = option_string_actions
    for subaction in action._parser._actions:
        if isinstance(subaction, ActionYesNo):
            subaction._add_dest_prefix(prefix)
        else:
            subaction.dest = prefix+'.'+subaction.dest
            for n in range(len(subaction.option_strings)):
                subaction.option_strings[n] = re.sub('^--', '--'+prefix+'.', subaction.option_strings[n])
        if isinstance(subaction, ActionParser):
            _set_inner_parser_prefix(action._parser, prefix, subaction)
github omni-us / jsonargparse / jsonargparse.py View on Github external
def __call__(self, *args, **kwargs):
        """Sets the corresponding key to True or False depending on the option string used."""
        if len(args) == 0:
            kwargs['_yes_prefix'] = self._yes_prefix
            kwargs['_no_prefix'] = self._no_prefix
            return ActionYesNo(**kwargs)
        value = args[2][0] if isinstance(args[2], list) and len(args[2]) == 1 else args[2] if isinstance(args[2], bool) else True
        if self._no_prefix is not None and args[3].startswith('--'+self._no_prefix):
            setattr(args[1], self.dest, not value)
        else:
            setattr(args[1], self.dest, value)
github omni-us / jsonargparse / jsonargparse.py View on Github external
def add_argument(self, *args, **kwargs):
        """Adds an argument to the parser or argument group.

        All the arguments from `argparse.ArgumentParser.add_argument
        `_
        are supported.
        """
        if 'type' in kwargs and kwargs['type'] == bool:
            if 'nargs' in kwargs:
                raise ValueError('Argument with type=bool does not support nargs.')
            kwargs['nargs'] = 1
            kwargs['action'] = ActionYesNo(no_prefix=None)
        action = super().add_argument(*args, **kwargs)
        for key in meta_keys:
            if key in action.dest:
                raise ValueError('Argument with destination name "'+key+'" not allowed.')
        parser = self.parser if hasattr(self, 'parser') else self  # pylint: disable=no-member
        if action.required:
            parser.required_args.add(action.dest)  # pylint: disable=no-member
            action.required = False
        if isinstance(action, ActionConfigFile) and parser.formatter_class == DefaultHelpFormatter:  # pylint: disable=no-member
            setattr(parser.formatter_class, '_conf_file', True)  # pylint: disable=no-member
        elif isinstance(action, ActionParser):
            _set_inner_parser_prefix(self, action.dest, action)
        return action
github omni-us / jsonargparse / jsonargparse.py View on Github external
if 'dest' not in kwargs:
                kwargs['dest'] = re.sub('^--', '', opt_name).replace('-', '_')
            if self._no_prefix is not None:
                kwargs['option_strings'] += [re.sub('^--'+self._yes_prefix, '--'+self._no_prefix, opt_name)]
            if self._no_prefix is None and 'nargs' in kwargs and kwargs['nargs'] != 1:
                raise ValueError('ActionYesNo with no_prefix=None only supports nargs=1.')
            if 'nargs' in kwargs and kwargs['nargs'] in {'?', 1}:
                kwargs['metavar'] = 'true|yes|false|no'
                if kwargs['nargs'] == 1:
                    kwargs['nargs'] = None
            else:
                kwargs['nargs'] = 0
                kwargs['metavar'] = None
            if 'default' not in kwargs:
                kwargs['default'] = False
            kwargs['type'] = ActionYesNo._boolean_type
            super().__init__(**kwargs)