Skip to content

πŸ™Œ OpenHands: AI-Driven Development

(feat) add --version to cli (#3924)

tobitegec3117e8c3902
diff --git a/openhands/__init__.py b/openhands/__init__.py
index e69de29bb2..5ef398095f 100644
--- a/openhands/__init__.py
+++ b/openhands/__init__.py
@@ -0,0 +1,28 @@
+def get_version():
+ try:
+ from importlib.metadata import PackageNotFoundError, version
+
+ try:
+ return version('openhands-ai')
+ except PackageNotFoundError:
+ pass
+ except ImportError:
+ pass
+
+ try:
+ from pkg_resources import DistributionNotFound, get_distribution
+
+ try:
+ return get_distribution('openhands-ai').version
+ except DistributionNotFound:
+ pass
+ except ImportError:
+ pass
+
+ return 'unknown'
+
+
+try:
+ __version__ = get_version()
+except Exception:
+ __version__ = 'unknown'
diff --git a/openhands/core/cli.py b/openhands/core/cli.py
index 4159363f26..209d9b7366 100644
--- a/openhands/core/cli.py
+++ b/openhands/core/cli.py
@@ -1,3 +1,4 @@
+import argparse
import asyncio
import logging
from typing import Type
@@ -5,6 +6,7 @@ from typing import Type
from termcolor import colored
import agenthub # noqa F401 (we import this to get the agents registered)
+from openhands import __version__
from openhands.controller import AgentController
from openhands.controller.agent import Agent
from openhands.core.config import (
@@ -61,8 +63,32 @@ def display_event(event: Event):
display_command_output(event.content)
+def get_parser() -> argparse.ArgumentParser:
+ """Get the parser for the command line arguments."""
+ parser = argparse.ArgumentParser(description='Run an agent with a specific task')
+
+ # Add the version argument
+ parser.add_argument(
+ '-v',
+ '--version',
+ action='version',
+ version=f'{__version__}',
+ help='Show the version number and exit',
+ )
+
+ return parser
+
+
async def main():
"""Runs the agent in CLI mode"""
+
+ parser = get_parser()
+ args = parser.parse_args()
+
+ if args.version:
+ print(f'OpenHands version: {__version__}')
+ return
+
logger.setLevel(logging.WARNING)
config = load_app_config()
sid = 'cli'
diff --git a/pyproject.toml b/pyproject.toml
index a3d94c1404..b00e21dba1 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -85,7 +85,6 @@ reportlab = "*"
[tool.coverage.run]
concurrency = ["gevent"]
-
[tool.poetry.group.runtime.dependencies]
jupyterlab = "*"
notebook = "*"
@@ -116,7 +115,6 @@ ignore = ["D1"]
[tool.ruff.lint.pydocstyle]
convention = "google"
-
[tool.poetry.group.evaluation.dependencies]
streamlit = "*"
whatthepatch = "*"
@@ -128,3 +126,10 @@ sympy = "*"
gdown = "*"
matplotlib = "*"
seaborn = "*"
+
+[tool.poetry-dynamic-versioning]
+enable = true
+style = "semver"
+
+[tool.poetry.scripts]
+openhands = "openhands.core.cli:main"