Repository: kedro-org/kedro
File: kedro/framework/cli/utils.py (Python)

Code before:
def _config_file_callback(ctx, param, value):  # noqa: unused-argument
    """CLI callback that replaces command line options
    with values specified in a config file. If command line
    options are passed, they override config file values.
    """

    ctx.default_map = ctx.default_map or {}
    section = ctx.info_name

    if value:
        config = OmegaConf.to_container(OmegaConf.load(value))[section]
        for key, value in config.items():
            _validate_config_file(key)
        ctx.default_map.update(config)

    return value


def _validate_config_file(key):
    """Validate the keys provided in the config file against the accepted keys."""
    from kedro.framework.cli.project import run

    run_args = [click_arg.name for click_arg in run.params]
    run_args.remove("config")
    if key not in run_args:
        KedroCliError.VERBOSE_EXISTS = False
        message = _suggest_cli_command(key, run_args)
        raise KedroCliError(
            f"Key `{key}` in provided configuration is not valid. {message}"
        )


def _reformat_load_versions(ctx, param, value) -> dict[str, str]:
    """Reformat data structure from tuple to dictionary for `load-version`, e.g.:
    ('dataset1:time1', 'dataset2:time2') -> {"dataset1": "time1", "dataset2": "time2"}.
    """
    if param.name == "load_version":
        _deprecate_options(ctx, param, value)

    load_versions_dict = {}
    for load_version in value:
        load_version = load_version.strip()  # noqa: PLW2901
        load_version_list = load_version.split(":", 1)
        if len(load_version_list) != 2:  # noqa: PLR2004
            raise KedroCliError(
                f"Expected the form of 'load_version' to be "
                f"'dataset_name:YYYY-MM-DDThh.mm.ss.sssZ',"
                f"found {load_version} instead"
            )
        load_versions_dict[load_version_list[0]] = load_version_list[1]


Review comment (suggestion):
@noklam This is a possible "solution" for the `--verbose` messaging, but it's not ideal because it only solves it for when you run --config, but not for other CLI commands that actually don't have a verbose flag.

Code after:
def _config_file_callback(ctx, param, value):  # noqa: unused-argument
    """CLI callback that replaces command line options
    with values specified in a config file. If command line
    options are passed, they override config file values.
    """

    ctx.default_map = ctx.default_map or {}
    section = ctx.info_name

    if value:
        config = OmegaConf.to_container(OmegaConf.load(value))[section]
        for key, value in config.items():
            _validate_config_file(key)
        ctx.default_map.update(config)

    return value


def _validate_config_file(key):
    """Validate the keys provided in the config file against the accepted keys."""
    from kedro.framework.cli.project import run

    run_args = [click_arg.name for click_arg in run.params]
    run_args.remove("config")
    if key not in run_args:
        KedroCliError.VERBOSE_EXISTS = False
        message = _suggest_cli_command(key, run_args)
        raise KedroCliError(
            f"Key `{key}` in provided configuration is not valid. {message}"
        )


def _split_params(ctx, param, value):
    if isinstance(value, dict):
        return value
    dot_list = []
    for item in split_string(ctx, param, value):
        equals_idx = item.find("=")
        colon_idx = item.find(":")
        if equals_idx != -1 and colon_idx != -1 and equals_idx < colon_idx:
            # For cases where key-value pair is separated by = and the value contains a colon
            # which should not be replaced by =
            pass
        else:
            item = item.replace(":", "=", 1)  # noqa: PLW2901
        items = item.split("=", 1)
        if len(items) != 2:  # noqa: PLR2004
            ctx.fail(
                f"Invalid format of `{param.name}` option: "
                f"Item `{items[0]}` must contain "
                f"a key and a value separated by `:` or `=`."