diff --git a/openml/cli.py b/openml/cli.py index d0a46e498..fa10c65ac 100644 --- a/openml/cli.py +++ b/openml/cli.py @@ -10,6 +10,7 @@ from urllib.parse import urlparse from openml import config +from openml.__version__ import __version__ def is_hex(string_: str) -> bool: @@ -331,6 +332,13 @@ def main() -> None: subroutines = {"configure": configure} parser = argparse.ArgumentParser() + # Add a global --version flag to display installed version and exit + parser.add_argument( + "--version", + action="version", + version=f"%(prog)s {__version__}", + help="Show the OpenML version and exit", + ) subparsers = parser.add_subparsers(dest="subroutine") parser_configure = subparsers.add_parser( diff --git a/tests/test_openml/test_cli.py b/tests/test_openml/test_cli.py new file mode 100644 index 000000000..763fa7e18 --- /dev/null +++ b/tests/test_openml/test_cli.py @@ -0,0 +1,42 @@ +# License: BSD 3-Clause +from __future__ import annotations + +import shutil +import subprocess +import sys + +import openml +import pytest + + +def test_cli_version_prints_package_version(): + # Invoke the CLI via module to avoid relying on console script installation + result = subprocess.run( + [sys.executable, "-m", "openml.cli", "--version"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + check=False, + ) + + # Ensure successful exit and version present in output + assert result.returncode == 0 + assert openml.__version__ in (result.stdout + result.stderr) + + +def test_console_script_version_prints_package_version(): + # Try to locate the console script; skip if not installed in PATH + console = shutil.which("openml") + if console is None: + pytest.skip("'openml' console script not found in PATH") + + result = subprocess.run( + [console, "--version"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + check=False, + ) + + assert result.returncode == 0 + assert openml.__version__ in (result.stdout + result.stderr)