Skip to content
8 changes: 8 additions & 0 deletions openml/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from urllib.parse import urlparse

from openml import config
from openml.__version__ import __version__


def is_hex(string_: str) -> bool:
Expand Down Expand Up @@ -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(
Expand Down
44 changes: 44 additions & 0 deletions tests/test_openml/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# 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 stdout only
assert result.returncode == 0
assert result.stderr == ""
assert openml.__version__ in result.stdout


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 result.stderr == ""
assert openml.__version__ in result.stdout