diff --git a/src/blueapi/cli/cli.py b/src/blueapi/cli/cli.py index 7bafa831f..e04ad2fdc 100644 --- a/src/blueapi/cli/cli.py +++ b/src/blueapi/cli/cli.py @@ -80,11 +80,12 @@ def is_str_dict(val: Any) -> TypeGuard[TaskParameters]: invoke_without_command=True, context_settings={"auto_envvar_prefix": "BLUEAPI"} ) @click.version_option(version=__version__, prog_name="blueapi") +@click.option("-H", "--host", type=str) @click.option( "-c", "--config", type=Path, help="Path to configuration YAML file", multiple=True ) @click.pass_context -def main(ctx: click.Context, config: tuple[Path, ...]) -> None: +def main(ctx: click.Context, config: tuple[Path, ...], host: str | None): # if no command is supplied, run with the options passed # Set umask to DLS standard @@ -95,6 +96,8 @@ def main(ctx: click.Context, config: tuple[Path, ...]) -> None: config_loader.use_values_from_yaml(*config) except FileNotFoundError as fnfe: raise ClickException(f"Config file not found: {fnfe.filename}") from fnfe + if host: + config_loader.use_values({"api": {"url": host}}) loaded_config: ApplicationConfig = config_loader.load() diff --git a/tests/unit_tests/cli/test_cli.py b/tests/unit_tests/cli/test_cli.py index 93a01d0c9..4ad04b04e 100644 --- a/tests/unit_tests/cli/test_cli.py +++ b/tests/unit_tests/cli/test_cli.py @@ -1384,3 +1384,45 @@ def test_config_schema( def test_task_parameter_type(value, result): t = ParametersType() assert t.convert(value, None, None) == result + + +@responses.activate +def test_host_option(runner: CliRunner): + response = responses.add( + responses.GET, + "http://override.example.com:5678/plans", + json={"plans": []}, + status=200, + ) + + res = runner.invoke( + main, + ["--host", "http://override.example.com:5678", "controller", "plans"], + ) + assert response.call_count == 1 + assert res.exit_code == 0 + + +@responses.activate +def test_host_overrides_config(runner: CliRunner): + config_path = "tests/unit_tests/example_yaml/rest_config.yaml" + response = responses.add( + responses.GET, + "http://override.example.com:5678/plans", + json={"plans": []}, + status=200, + ) + + res = runner.invoke( + main, + [ + "--host", + "http://override.example.com:5678", + "--config", + config_path, + "controller", + "plans", + ], + ) + assert response.call_count == 1 + assert res.exit_code == 0