From db13bcfcd81be7c86a27b55ed70fc63749e989b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niko=20Heikkil=C3=A4?= Date: Sun, 30 Jan 2022 14:53:30 +0200 Subject: [PATCH] feat(cli): add -V|--version option --- install.sh | 1 + publicator/__init__.py | 12 +++++++++++- publicator/cli.py | 15 +++++++++++++-- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/install.sh b/install.sh index be4e183..92394f2 100755 --- a/install.sh +++ b/install.sh @@ -15,4 +15,5 @@ for wheel in dist/*.whl; do done echo "Testing executable" +publicator --version publicator --help diff --git a/publicator/__init__.py b/publicator/__init__.py index 3dc1f76..41b473f 100644 --- a/publicator/__init__.py +++ b/publicator/__init__.py @@ -1 +1,11 @@ -__version__ = "0.1.0" +from importlib import metadata + +name = "publicator" + + +def display_name() -> str: + return name.capitalize() + + +def version() -> str: + return f"{display_name()} v{metadata.version(name)}" diff --git a/publicator/cli.py b/publicator/cli.py index c90c56f..51646a4 100644 --- a/publicator/cli.py +++ b/publicator/cli.py @@ -1,13 +1,23 @@ import os from typing import Optional + import typer -from publicator import git, github, poetry, config +import publicator +from publicator import config, git, github, poetry from publicator.semver import Semver preview = os.environ.get("PUBLICATOR_PREVIEW") configuration = config.factory() -app = typer.Typer(name="publicator") +app = typer.Typer(name=publicator.name) + + +def version_callback(value: bool) -> None: + if not value: + return + + typer.secho(f"🦄 {publicator.version()}", fg=typer.colors.BRIGHT_BLUE, bold=True) + raise typer.Exit() @app.command() @@ -17,6 +27,7 @@ def cli( metavar="version", help="can be a valid semver or one of: patch, minor, major, prepatch, preminor, premajor, prerelease", ), + version: Optional[bool] = typer.Option(None, "--version", "-V", callback=version_callback, is_eager=True), repository: Optional[str] = typer.Option( default=configuration.get("repository"), metavar="name",