Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 438
Igor Rivin Avatar asked Oct 27 '25 08:10

Igor Rivin


1 Answers

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).

like image 183
Omry Yadan Avatar answered Oct 28 '25 22:10

Omry Yadan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!