diff --git a/src/wristpy/core/cli.py b/src/wristpy/core/cli.py index f165fe9a..0ceba9f1 100644 --- a/src/wristpy/core/cli.py +++ b/src/wristpy/core/cli.py @@ -55,6 +55,13 @@ class NonwearAlgorithms(str, Enum): detach = "detach" +def version_check(version: bool) -> None: + """Print the current version of wristpy and exit.""" + if version: + typer.echo(f"Wristpy version: {config.get_version()}") + raise typer.Exit() + + @app.command() def main( input: pathlib.Path = typer.Argument( @@ -123,16 +130,17 @@ def main( "Defaults to INFO if not included.", ), version: bool = typer.Option( - False, "-V", "--version", help="Show the version and exit." + False, + "-V", + "--version", + help="Print the current version of wristpy and exit.", + is_eager=True, + callback=version_check, ), ) -> None: """Run wristpy orchestrator with command line arguments.""" from wristpy.core import orchestrator - if version: - typer.echo(f"Wristpy version: {config.get_version()}") - raise typer.Exit() - log_level = logging.INFO if verbosity: log_level = logging.DEBUG diff --git a/tests/unit/test_cli.py b/tests/unit/test_cli.py index 48d23eab..0e901bdf 100644 --- a/tests/unit/test_cli.py +++ b/tests/unit/test_cli.py @@ -206,3 +206,30 @@ def test_main_verbosity( verbosity=logging.DEBUG, output_filetype=None, ) + + +def test_main_version( + create_typer_cli_runner: testing.CliRunner, +) -> None: + """Test cli version output.""" + result = create_typer_cli_runner.invoke(cli.app, ["--version"]) + + assert result.exit_code == 0 + assert "Wristpy version" in result.output + + +def test_main_version_with_options( + create_typer_cli_runner: testing.CliRunner, + sample_data_gt3x: pathlib.Path, + mocker: pytest_mock.MockerFixture, +) -> None: + """Test other arguments and options are ignored when --version is passed.""" + mock_run = mocker.patch.object(orchestrator, "run") + + result = create_typer_cli_runner.invoke( + cli.app, [str(sample_data_gt3x), "-e", "5", "--version"] + ) + + assert result.exit_code == 0 + assert "Wristpy version" in result.output + mock_run.assert_not_called()