diff --git a/README.md b/README.md index 7df9af4..1d9a17d 100644 --- a/README.md +++ b/README.md @@ -86,9 +86,10 @@ Hydraclick provides several configuration options to customize your CLI: - `config_path`: Path to the configuration directory. Passed to [`hydra.main()`](https://hydra.cc/docs/tutorials/basic/your_first_app/config_file/) - `config_name`: Name of the configuration file. Passed to [`hydra.main()`](https://hydra.cc/docs/tutorials/basic/your_first_app/config_file/) - `version_base`: Base version of the configuration. Passed to [`hydra.main()`](https://hydra.cc/docs/tutorials/basic/your_first_app/config_file/) -- `run_mode`: Mode to run the function (`"config"` or `"kwargs"`). - - `"config"`: Pass the configuration as a single `OmegaConf.DictCondig` object. - - `"kwargs"`: Resolve the `OmegaConf.DictConfig` objet into a python `dict` and pass it as keyword arguments. +- `as_kwargs`: The mode in which to run the function. If `True`, the function is run with the +configuration as keyword arguments. In this case the configuration is converted to a dictionary +before passing it to the function. If `False`, pass the configuration as a single `OmegaConf.DictConfig` object. +Defaults to `False`. - `preprocess_config`: Function to preprocess the configuration. It takes a `DictConfig` object and returns a `DictConfig` object. - `print_config`: Whether to print the configuration before execution. - `resolve`: Whether to resolve the configuration. diff --git a/src/hydraclick/core.py b/src/hydraclick/core.py index 06d8780..5dcf6b8 100644 --- a/src/hydraclick/core.py +++ b/src/hydraclick/core.py @@ -38,7 +38,7 @@ def wrap_kwargs_and_config( function: Callable, - run_mode: str = "config", # "config" | "kwargs" + as_kwargs: bool = False, print_config: bool = True, preprocess_config: Callable[[DictConfig], DictConfig] | None = None, resolve: bool = True, @@ -57,9 +57,10 @@ def wrapper(config): raise e if print_config: display_config(config, logger=_logger) - if run_mode == "config": + if not as_kwargs: return function(config) - return function(**config) + conf_dict = OmegaConf.to_container(config, resolve=resolve) + return function(**conf_dict) return wrapper @@ -165,7 +166,7 @@ def command_api( config_path: str | Path | None = None, config_name: str | None = "config", version_base: str | None = None, - run_mode: str = "config", + as_kwargs: bool = False, preprocess_config: Callable[[DictConfig], DictConfig] | None = None, print_config: bool = True, resolve: bool = True, @@ -183,8 +184,10 @@ def command_api( (without the `.yaml` or `.yml` extension). Defaults to `"config"`. version_base (str | None, optional): The base version of the configuration. \ Defaults to `None`. - run_mode (str, optional): The mode in which to run the function. Can be `"config"` \ - or `"kwargs"`. Defaults to `"config"`. + as_kwargs (bool, optional): The mode in which to run the function. \ + If `True`, the function is run with the configuration as keyword arguments. In \ + this case the configuration is converted to a dictionary before passing it to the \ + function. Defaults to `False`. preprocess_config (Callable[[DictConfig], DictConfig] | None, optional): A function \ to preprocess the configuration before passing it to the main function. \ Defaults to `None`. @@ -276,7 +279,7 @@ def click_compatible( ): nonlocal \ print_config, \ - run_mode, \ + as_kwargs, \ preprocess_config, \ resolve, \ config_path, \ @@ -287,7 +290,7 @@ def click_compatible( if show_config: print_config = False true_func = wrap_kwargs_and_config( - function, run_mode, print_config, preprocess_config, resolve + function, as_kwargs, print_config, preprocess_config, resolve ) hydra_args = build_hydra_args( hydra_help, @@ -321,7 +324,7 @@ def hydra_command( config_path: str | Path | None = None, config_name: str | None = "config", version_base: str | None = None, - run_mode: str = "config", + as_kwargs: bool = False, preprocess_config: Callable[[DictConfig], DictConfig] | None = None, print_config: bool = True, resolve: bool = True, @@ -337,8 +340,10 @@ def hydra_command( (without the `.yaml` or `.yml` extension). Defaults to `"config"`. version_base (str | None, optional): The base version of the configuration. \ Defaults to `None`. - run_mode (str, optional): The mode in which to run the function. Can be `"config"` \ - or `"kwargs"`. Defaults to `"config"`. + as_kwargs (bool, optional): The mode in which to run the function. \ + If `True`, the function is run with the configuration as keyword arguments. In \ + this case the configuration is converted to a dictionary before passing it to the \ + function. Defaults to `False`. preprocess_config (Callable[[DictConfig], DictConfig] | None, optional): A function to \ preprocess the configuration before passing it to the main function. \ Defaults to `None`. @@ -371,7 +376,7 @@ def decorator(function: Callable): config_name=config_name, version_base=version_base, use_flogging=use_flogging, - run_mode=run_mode, + as_kwargs=as_kwargs, print_config=print_config, preprocess_config=preprocess_config, resolve=resolve,