From b6c954f2a96b2e1bc54a34bc491508d345f48b0d Mon Sep 17 00:00:00 2001 From: ddayan Date: Tue, 26 Mar 2024 01:24:54 +0000 Subject: [PATCH 1/2] Show version number in help menu and --version option. --- src/communex/__init__.py | 4 ++++ src/communex/cli/root.py | 17 +++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/communex/__init__.py b/src/communex/__init__.py index 4f3e758..ce1e46f 100644 --- a/src/communex/__init__.py +++ b/src/communex/__init__.py @@ -9,3 +9,7 @@ .. include:: ../../README.md """ + +import importlib.metadata + +__version__ = importlib.metadata.version(__package__) diff --git a/src/communex/cli/root.py b/src/communex/cli/root.py index 9ac0941..891dd26 100644 --- a/src/communex/cli/root.py +++ b/src/communex/cli/root.py @@ -1,5 +1,8 @@ from typing import Optional import typer +from typing_extensions import Annotated + +from communex import __version__ from .balance import balance_app from .key import key_app @@ -18,10 +21,20 @@ app.add_typer(subnet_app, name="subnet", help="Subnet operations") +def _version_callback(value: bool): + if value: + print(f"CommuneX {__version__}") + raise typer.Exit() + @app.callback() -def main(json: Optional[bool] = False): +def main(json: Optional[bool] = False, + version: Annotated[ + Optional[bool], typer.Option("--version", callback=_version_callback) + ] = None, +): """ - CommuneX CLI. + CommuneX CLI Version: {0} This command line interface is under development and subject to change. """ +main.__doc__ = main.__doc__.format(__version__) From 360a9d16ff86d19a6034847aec591c29b31693e9 Mon Sep 17 00:00:00 2001 From: ddayan Date: Tue, 26 Mar 2024 16:11:02 +0000 Subject: [PATCH 2/2] refactoring --- src/communex/cli/root.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/communex/cli/root.py b/src/communex/cli/root.py index 891dd26..9eb45a0 100644 --- a/src/communex/cli/root.py +++ b/src/communex/cli/root.py @@ -1,6 +1,5 @@ -from typing import Optional import typer -from typing_extensions import Annotated +from typing import Optional, Annotated from communex import __version__ @@ -33,8 +32,9 @@ def main(json: Optional[bool] = False, ] = None, ): """ - CommuneX CLI Version: {0} + CommuneX CLI {version} This command line interface is under development and subject to change. """ -main.__doc__ = main.__doc__.format(__version__) + +main.__doc__ = main.__doc__.format(version=__version__)