Python argparse._AppendAction() Examples

The following are code examples for showing how to use argparse._AppendAction(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don't like. You can also save this page to your account.

Example 1
Project: pipelines   Author: gis-rpd   File: configargparse.py    (license) View Source Project 4 votes vote down vote up
def convert_item_to_command_line_arg(self, action, key, value):
        """Converts a config file or env var key + value to a list of
        commandline args to append to the commandline.

        Args:
            action: The argparse Action object for this setting, or None if this
                config file setting doesn't correspond to any defined
                configargparse arg.
            key: string (config file key or env var name)
            value: parsed value of type string or list
        """
        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]

        # handle boolean value
        if action is not None and isinstance(action, ACTION_TYPES_THAT_DONT_NEED_A_VALUE):
            if value.lower() in ("true", "yes"):
                args.append( command_line_key )
            elif value.lower() in ("false", "no"):
                # don't append when set to "false" / "no"
                pass
            else:
                self.error("Unexpected value for %s: '%s'. Expecting 'true', "
                           "'false', 'yes', or 'no'" % (key, value))
        elif isinstance(value, list):
            if action is None or isinstance(action, argparse._AppendAction):
                for list_elem in value:
                    args.append( command_line_key )
                    args.append( str(list_elem) )
            elif (isinstance(action, argparse._StoreAction) and action.nargs in ('+', '*')) or (
                isinstance(action.nargs, int) and action.nargs > 1):
                args.append( command_line_key )
                for list_elem in value:
                    args.append( str(list_elem) )
            else:
                self.error(("%s can't be set to a list '%s' unless its action type is changed "
                            "to 'append' or nargs is set to '*', '+', or > 1") % (key, value))
        elif isinstance(value, str):
            args.append( command_line_key )
            args.append( value )
        else:
            raise ValueError("Unexpected value type %s for value: %s" % (
                type(value), value))

        return args