Skip to content

Commit

Permalink
added command line flag overrides for config_path and config_name
Browse files Browse the repository at this point in the history
  • Loading branch information
omry committed May 25, 2020
1 parent 3f6ce61 commit ba96a8a
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 2 deletions.
20 changes: 19 additions & 1 deletion hydra/_internal/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@ def run_hydra(

from .hydra import Hydra

args = args_parser.parse_args()
if args.config_name is not None:
config_name = args.config_name

if args.config_path is not None:
config_path = args.config_path

calling_file, calling_module = detect_calling_file_or_module_from_task_function(
task_function
)
Expand All @@ -224,7 +231,6 @@ def run_hydra(
task_name=task_name, config_search_path=search_path, strict=strict
)
try:
args = args_parser.parse_args()
if args.help:
hydra.app_help(config_name=config_name, args_parser=args_parser, args=args)
sys.exit(0)
Expand Down Expand Up @@ -341,6 +347,18 @@ def get_args_parser() -> argparse.ArgumentParser:
help=f"Install or Uninstall shell completion:\n{_get_completion_help()}",
)

parser.add_argument(
"--config_path",
"-cp",
help="""Overrides the config_path specified in hydra.main().
The config_path is relative to the Python file declaring @hydra.main()""",
)

parser.add_argument(
"--config_name",
"-cn",
help="Overrides the config_name specified in hydra.main()",
)
return parser


Expand Down
1 change: 1 addition & 0 deletions news/386.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add command line override flags for `config_path` and `config_name`
1 change: 1 addition & 0 deletions tests/test_apps/app_with_multiple_config_dirs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dir1_cfg1: true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dir1_cfg2: true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dir2_cfg1: true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dir2_cfg2: true
13 changes: 13 additions & 0 deletions tests/test_apps/app_with_multiple_config_dirs/my_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
from omegaconf import DictConfig

import hydra


@hydra.main()
def my_app(cfg: DictConfig) -> None:
print(cfg.pretty())


if __name__ == "__main__":
my_app()
25 changes: 25 additions & 0 deletions tests/test_hydra.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,9 @@ def test_sweep_complex_defaults(
Fish - Uninstall:
python my_app.py -sc uninstall=fish | source
--config_path,-cp : Overrides the config_path specified in hydra.main().
The config_path is relative to the Python file declaring @hydra.main()
--config_name,-cn : Overrides the config_name specified in hydra.main()
Overrides : Any key=value arguments to override config values (use dots for.nested=overrides)
""",
id="overriding_help_template:$FLAGS_HELP",
Expand Down Expand Up @@ -597,6 +600,9 @@ def test_sweep_complex_defaults(
Fish - Uninstall:
python my_app.py -sc uninstall=fish | source
--config_path,-cp : Overrides the config_path specified in hydra.main().
The config_path is relative to the Python file declaring @hydra.main()
--config_name,-cn : Overrides the config_name specified in hydra.main()
Overrides : Any key=value arguments to override config values (use dots for.nested=overrides)
""",
id="overriding_hydra_help_template:$FLAGS_HELP",
Expand Down Expand Up @@ -731,3 +737,22 @@ def test_override_with_invalid_group_choice(
overrides=[f"db={override}"],
):
...


@pytest.mark.parametrize("config_path", ["dir1", "dir2"]) # type: ignore
@pytest.mark.parametrize("config_name", ["cfg1", "cfg2"]) # type: ignore
def test_config_name_and_path_overrides(
tmpdir: Path, config_path: str, config_name: str
) -> None:
cmd = [
sys.executable,
"tests/test_apps/app_with_multiple_config_dirs/my_app.py",
"hydra.run.dir=" + str(tmpdir),
f"--config_name={config_name}",
f"--config_path={config_path}",
]
print(" ".join(cmd))
result = str(subprocess.check_output(cmd).decode("utf-8")).strip()
# normalize newlines on Windows to make testing easier
result = result.replace("\r\n", "\n")
assert result == f"{config_path}_{config_name}: true"
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Since we moved all the configs into the `conf` directory, we need to tell Hydra
def my_app(cfg: DictConfig) -> None:
print(cfg.pretty())
```

You can override `config_path` from the command line using the `--config_path` flag.

Running `my_app.py` without requesting a configuration will print an empty config.
```yaml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ Note that you should omit the `.yaml` extension.
def my_app(cfg):
print(cfg.pretty())
```

You can override `config_name` from the command line using the `--config_name` flag.

`config.yaml` is loaded automatically when you run your application.
```yaml
$ python my_app.py
Expand Down

0 comments on commit ba96a8a

Please sign in to comment.