3

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)
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Igor Rivin
  • 4,632
  • 2
  • 23
  • 35

1 Answers1

2

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

Omry Yadan
  • 31,280
  • 18
  • 64
  • 87
  • 1
    Thanks, Omry. The reason I ask is that I want to migrate "gradually" from command line args to hydra, instead of jumping whole hog into hydra world. I guess a kludge would be to have a main_aux which get the config from hydra and a main which processes the remaining command line args and calls main_aux. May be a little too disgusting, though. – Igor Rivin May 23 '22 at 17:48
  • One thing you can try is to generate a Hydra like config using the Compose API from your legacy entry point, and pass the cfg object to the rest of the app to consume. You just need to extract the parameters from the config object at the top of the function. – Omry Yadan May 23 '22 at 17:57