Search code examples
pythonfb-hydra

command line args with `hydra`


The standard mantra to pass a configuration to hydra is to decorate with @hydra.main then to define your main(cfg), and then in the main block call main with no arguments. But suppose I want to also accept non-hydra command line args, so want to pass both args and the config? Is this viewed as morally impure and discouraged? Here is an example (which does not work) of the sort of thing I mean (it errors out claiming it is missing a cfg argument).

from omegaconf import DictConfig, OmegaConf
import hydra

@hydra.main(version_base=None)
def my_app(foo, cfg: DictConfig) -> None:
    print(foo)
    print(OmegaConf.to_yaml(cfg))

if __name__ == "__main__":
    my_app(5)

Solution

  • This is not supported. In principle, you have nothing to base different values for your 5 parameter before the config is composed.

    If you want to have additional logic before composing the config, look at the Compose API as an alternative to @hydra.main() (or in addition).