yoco.py
def load_config_from_args(
    parser: _argparse.ArgumentParser,
    args: Optional[list] = None,
    search_paths: Optional[List[str]] = None,
) -> dict:
    """Parse arguments and load configs into a config dictionary.

    Strings following -- will be used as key. Dots in that string are used to access
    nested dictionaries. YAML will be used for type conversion of the value.

    Args:
        parser:
            Parser used to parse known and unknown arguments.
            This function will handle both the known args that have been added before
            and it tries to parse all other args and integrate them into the config.
        args:
            List of arguments to parse. If None, sys.argv[1:] is used.
        search_paths:
            Search paths used to resolve config files.
            See resolve_path for more information.

    Returns:
        Loaded configuration dictionary.
    """
    # parse arguments
    no_default_parser = copy.deepcopy(parser)
    for a in no_default_parser._actions:
        if a.dest != "config":
            a.default = None
    known, other_args = no_default_parser.parse_known_args(args)

    known_with_default, _ = parser.parse_known_args(args)

    config_dict = {k: v for k, v in vars(known).items() if v is not None}
    with_default_config_dict = {
        k: v for k, v in vars(known_with_default).items() if v is not None
    }
    with_default_config_dict.pop("config", None)

    config_dict = load_config(config_dict, search_paths=search_paths)
    current_key = None

    # list of unknown args (all strings) to dictionary
    # [--arg_1, val_1, --arg_2, val_2_a, val_2_b, ...]
    # -> {arg_1: val_1, arg_2: "{val_2_a} {val_2_b}, ...}
    arg_dict = {}
    for arg in other_args:
        if arg.startswith("--"):
            current_key = arg.replace("--", "")
            arg_dict[current_key] = []
        else:
            if current_key is None:
                parser.error(
                    message="General args need to start with --name {values}\n"
                )
            arg_dict[current_key].append(arg)
    arg_dict = {key: " ".join(values) for key, values in arg_dict.items()}
    # done parsing args, stored in arg_dict

    # integrate arg, value pairs into config_dict loaded before, one by one
    # args can set nested values by using dots
    # i.e., a.b.c will result in the following add_dict: {"a": {"b": {"c": value}}}
    # "config" can still be used to load files
    for arg, value in arg_dict.items():
        add_dict = {}
        current_dict = add_dict
        hierarchy = arg.split(".")
        for a in hierarchy[:-1]:
            current_dict[a] = {}
            current_dict = current_dict[a]
        # parse value using yaml (this allows setting lists, dictionaries, etc.)
        current_dict[hierarchy[-1]] = _yaml.load(value)

        if hierarchy[0] == "config":
            # handle special "config" key by loading the nested dict as root dict
            # this will practically replace "config" key with the dict from
            # the specified file
            add_dict = load_config(add_dict, search_paths=search_paths)
            # config file -> lower priority than what is already there
            config_dict = load_config(config_dict, add_dict, search_paths=search_paths)
        else:
            # integrate nested dictionary into config_dict loaded before
            # normal argument -> higher priority than what is arleady there
            config_dict = load_config(add_dict, config_dict)

    # add default values last (lowest priority) if they weren't specified so far
    config_dict = _merge_dictionaries(with_default_config_dict, config_dict)

    return config_dict
Parse arguments and load configs into a config dictionary.

    Strings following -- will be used as key. Dots in that string are used to access
    nested dictionaries. YAML will be used for type conversion of the value.

    Args:
        parser:
            Parser used to parse known and unknown arguments.
            This function will handle both the known args that have been added before
            and it tries to parse all other args and integrate them into the config.
        args:
            List of arguments to parse. If None, sys.argv[1:] is used.
        search_paths:
            Search paths used to resolve config files.
            See resolve_path for more information.

    Returns:
        Loaded configuration dictionary.
    
Parse arguments and load configs into a config dictionary.
Strings following -- will be used as key. Dots in that string are used to access
nested dictionaries. YAML will be used for type conversion of the value.
Parse arguments and load configs into a config dictionary . Strings following -- will be used as key . Dots in that string are used to access nested dictionaries . YAML will be used for type conversion of the value .
def load_config_from_args(
    parser: _argparse.ArgumentParser,
    args: Optional[list] = None,
    search_paths: Optional[List[str]] = None,
) -> dict:
    no_default_parser = copy.deepcopy(parser)
    for a in no_default_parser._actions:
        if a.dest != "config":
            a.default = None
    known, other_args = no_default_parser.parse_known_args(args)
    known_with_default, _ = parser.parse_known_args(args)
    config_dict = {k: v for k, v in vars(known).items() if v is not None}
    with_default_config_dict = {
        k: v for k, v in vars(known_with_default).items() if v is not None
    }
    with_default_config_dict.pop("config", None)
    config_dict = load_config(config_dict, search_paths=search_paths)
    current_key = None
    arg_dict = {}
    for arg in other_args:
        if arg.startswith("--"):
            current_key = arg.replace("--", "")
            arg_dict[current_key] = []
        else:
            if current_key is None:
                parser.error(
                    message="General args need to start with --name {values}\n"
                )
            arg_dict[current_key].append(arg)
    arg_dict = {key: " ".join(values) for key, values in arg_dict.items()}
    for arg, value in arg_dict.items():
        add_dict = {}
        current_dict = add_dict
        hierarchy = arg.split(".")
        for a in hierarchy[:-1]:
            current_dict[a] = {}
            current_dict = current_dict[a]
        current_dict[hierarchy[-1]] = _yaml.load(value)
        if hierarchy[0] == "config":
            add_dict = load_config(add_dict, search_paths=search_paths)
            config_dict = load_config(config_dict, add_dict, search_paths=search_paths)
        else:
            config_dict = load_config(add_dict, config_dict)
    config_dict = _merge_dictionaries(with_default_config_dict, config_dict)
    return config_dict
def load_config_from_args ( parser : _argparse . ArgumentParser , args : Optional [ list ] = None , search_paths : Optional [ List [ str ] ] = None , ) -> dict : no_default_parser = copy . deepcopy ( parser ) for a in no_default_parser . _actions : if a . dest != "config" : a . default = None known , other_args = no_default_parser . parse_known_args ( args ) known_with_default , _ = parser . parse_known_args ( args ) config_dict = { k : v for k , v in vars ( known ) . items ( ) if v is not None } with_default_config_dict = { k : v for k , v in vars ( known_with_default ) . items ( ) if v is not None } with_default_config_dict . pop ( "config" , None ) config_dict = load_config ( config_dict , search_paths = search_paths ) current_key = None arg_dict = { } for arg in other_args : if arg . startswith ( "--" ) : current_key = arg . replace ( "--" , "" ) arg_dict [ current_key ] = [ ] else : if current_key is None : parser . error ( message = "General args need to start with --name {values}\n" ) arg_dict [ current_key ] . append ( arg ) arg_dict = { key : " " . join ( values ) for key , values in arg_dict . items ( ) } for arg , value in arg_dict . items ( ) : add_dict = { } current_dict = add_dict hierarchy = arg . split ( "." ) for a in hierarchy [ : - 1 ] : current_dict [ a ] = { } current_dict = current_dict [ a ] current_dict [ hierarchy [ - 1 ] ] = _yaml . load ( value ) if hierarchy [ 0 ] == "config" : add_dict = load_config ( add_dict , search_paths = search_paths ) config_dict = load_config ( config_dict , add_dict , search_paths = search_paths ) else : config_dict = load_config ( add_dict , config_dict ) config_dict = _merge_dictionaries ( with_default_config_dict , config_dict ) return config_dict
Parse arguments and load configs into a config dictionary.
Parse arguments and load configs into a config dictionary .
"""Parse arguments and load configs into a config dictionary.

    Strings following -- will be used as key. Dots in that string are used to access
    nested dictionaries. YAML will be used for type conversion of the value.

    Args:
        parser:
            Parser used to parse known and unknown arguments.
            This function will handle both the known args that have been added before
            and it tries to parse all other args and integrate them into the config.
        args:
            List of arguments to parse. If None, sys.argv[1:] is used.
        search_paths:
            Search paths used to resolve config files.
            See resolve_path for more information.

    Returns:
        Loaded configuration dictionary.
    """ # parse arguments # list of unknown args (all strings) to dictionary # [--arg_1, val_1, --arg_2, val_2_a, val_2_b, ...] # -> {arg_1: val_1, arg_2: "{val_2_a} {val_2_b}, ...} # done parsing args, stored in arg_dict # integrate arg, value pairs into config_dict loaded before, one by one # args can set nested values by using dots # i.e., a.b.c will result in the following add_dict: {"a": {"b": {"c": value}}} # "config" can still be used to load files # parse value using yaml (this allows setting lists, dictionaries, etc.) # handle special "config" key by loading the nested dict as root dict # this will practically replace "config" key with the dict from # the specified file # config file -> lower priority than what is already there # integrate nested dictionary into config_dict loaded before # normal argument -> higher priority than what is arleady there # add default values last (lowest priority) if they weren't specified so far
Loaded configuration dictionary.
Loaded configuration dictionary .
parser
_argparse.ArgumentParser
Parser used to parse known and unknown arguments.
This function will handle both the known args that have been added before
and it tries to parse all other args and integrate them into the config.
Parser used to parse known and unknown arguments . This function will handle both the known args that have been added before and it tries to parse all other args and integrate them into the config .
args
Optional[list]
List of arguments to parse. If None, sys.argv[1:] is used.
List of arguments to parse . If None sys . argv [ 1 : ] is used .
search_paths
Optional[List[str]]
Search paths used to resolve config files.
See resolve_path for more information.
Search paths used to resolve config files . See resolve_path for more information .