Skip to content

Commit 6039a6f

Browse files
committed
Add version_callback_option helper function.
1 parent 0c89f1a commit 6039a6f

2 files changed

Lines changed: 52 additions & 2 deletions

File tree

consolekit/versions.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import platform
3333
import sys
3434
import textwrap
35-
from typing import Any, Callable, Iterable, Mapping, Union
35+
from typing import Any, Callable, Iterable, Mapping, TypeVar, Union
3636

3737
# 3rd party
3838
import click
@@ -118,3 +118,28 @@ def version_callback(ctx: click.Context, param: click.Option, value: int) -> Non
118118
ctx.exit()
119119

120120
return version_callback
121+
122+
123+
_C = TypeVar("_C", bound=click.Command)
124+
125+
126+
def version_callback_option(
127+
tool_version: str,
128+
tool_name: str,
129+
dependencies: Union[Iterable[str], Mapping[str, str]] = (),
130+
) -> Callable[[_C], _C]:
131+
"""
132+
Helper that calls :deco:`~.version_option` with :func:`~.get_version_callback` as the callback.
133+
134+
.. versionadded:: 1.10.0
135+
136+
:param tool_version: The version of the tool to show the version of.
137+
:param tool_name: The name of the tool to show the version of.
138+
:param dependencies: Either a list of dependency names,
139+
or a mapping of dependency name to a more human-readable form.
140+
"""
141+
142+
# this package
143+
from consolekit.options import version_option
144+
145+
return version_option(get_version_callback(tool_version, tool_name, dependencies))

tests/test_versions.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from consolekit import click_command
66
from consolekit.options import version_option
77
from consolekit.testing import CliRunner
8-
from consolekit.versions import get_formatted_versions, get_version_callback
8+
from consolekit.versions import get_formatted_versions, get_version_callback, version_callback_option
99

1010

1111
def test_get_formatted_versions():
@@ -64,3 +64,28 @@ def main() -> None:
6464
print(result.stdout)
6565
assert result.stdout.startswith("my-tool\n Version: 1.2.3\n click: ")
6666
assert result.exit_code == 0
67+
68+
69+
def test_version_callback_option(cli_runner: CliRunner):
70+
71+
@version_callback_option(
72+
"1.2.3",
73+
"my-tool",
74+
["click", "deprecation-alias", "domdf-python-tools", "mistletoe", "typing-extensions"],
75+
)
76+
@click_command()
77+
def main() -> None:
78+
sys.exit(1)
79+
80+
result = cli_runner.invoke(main, args="--version")
81+
assert result.stdout.rstrip() == "my-tool version 1.2.3"
82+
assert result.exit_code == 0
83+
84+
result = cli_runner.invoke(main, args=["--version", "--version"])
85+
assert result.stdout.startswith("my-tool version 1.2.3, Python 3.")
86+
assert result.exit_code == 0
87+
88+
result = cli_runner.invoke(main, args=["--version", "--version", "--version"])
89+
print(result.stdout)
90+
assert result.stdout.startswith("my-tool\n Version: 1.2.3\n click: ")
91+
assert result.exit_code == 0

0 commit comments

Comments
 (0)