Skip to content

Commit 5272865

Browse files
committed
Restored --version and --info options to qclib CLI command
1 parent 6362d99 commit 5272865

2 files changed

Lines changed: 42 additions & 20 deletions

File tree

quantcrypt/internal/cli/main.py

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,45 @@
88
#
99
# SPDX-License-Identifier: MIT
1010
#
11-
from typer import Typer
11+
from typing import Annotated
12+
from typer import Typer, Option
1213
from .info import PackageInfo
1314
from . import utils, console
1415

1516

1617
app = utils.add_commands(Typer(
17-
name="qclib",
18-
invoke_without_command=True,
19-
no_args_is_help=True
18+
name="qclib",
19+
invoke_without_command=True,
20+
no_args_is_help=True
2021
))
2122

2223

23-
@app.command(name="version", help="Prints package version to the console and exits.")
24-
def version():
25-
print(PackageInfo().Version)
24+
VersionAtd = Annotated[bool, Option(
25+
'--version', '-v', show_default=False,
26+
help="Prints package version to the console and exits."
27+
)]
28+
InfoAtd = Annotated[bool, Option(
29+
'--info', '-i', show_default=False,
30+
help="Prints project info to the console and exits."
31+
)]
2632

2733

28-
@app.command(name="info", help="Pretty-prints package info to the console and exits.")
29-
def info() -> None:
30-
title_color = "[{}]".format("#ff5fff")
31-
key_color = "[{}]".format("#87d7d7")
32-
value_color = "[{}]".format("#ffd787")
34+
@app.callback()
35+
def main(version: VersionAtd = False, info: InfoAtd = False) -> None:
36+
if version and info:
37+
a, b = [f"[bold turquoise2]--{kw}[/]" for kw in ["version", "info"]]
38+
console.raise_error(f"Cannot use {a} and {b} options simultaneously.")
39+
elif version or info:
40+
if version:
41+
print(PackageInfo().Version)
42+
else:
43+
title_color = "[{}]".format("#ff5fff")
44+
key_color = "[{}]".format("#87d7d7")
45+
value_color = "[{}]".format("#ffd787")
3346

34-
console.styled_print(f"{title_color}Package Info:")
35-
for k, v in PackageInfo().toDict().items():
36-
k = f"{key_color}{k}"
37-
v = f"{value_color}{v}"
38-
console.styled_print(f"{2 * ' '}{k}: {v}")
39-
console.styled_print('')
47+
console.styled_print(f"{title_color}Package Info:")
48+
for k, v in PackageInfo().toDict().items():
49+
k = f"{key_color}{k}"
50+
v = f"{value_color}{v}"
51+
console.styled_print(f"{2 * ' '}{k}: {v}")
52+
console.styled_print('')

tests/test_cli/test_main.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ def fixture_project() -> DotMap:
2525

2626
def test_main_version(project: DotMap):
2727
runner = CliRunner()
28-
result = runner.invoke(app, ["version"])
28+
result = runner.invoke(app, ["--version"])
29+
2930
assert result.exit_code == 0
3031
assert result.stdout.strip() == project.version
3132

3233

3334
def test_main_info(project: DotMap):
3435
runner = CliRunner()
35-
result = runner.invoke(app, ["info"])
36+
result = runner.invoke(app, ["--info"])
3637

3738
assert result.exit_code == 0
3839
assert project.name in result.stdout
@@ -43,3 +44,11 @@ def test_main_info(project: DotMap):
4344

4445
fn, ln, _ = project.authors[0].split(" ")
4546
assert f"{fn} {ln}" in result.stdout
47+
48+
49+
def test_main_invalid_options():
50+
runner = CliRunner()
51+
result = runner.invoke(app, ["--version", "--info"])
52+
53+
assert result.exit_code == 1
54+
assert "Cannot use --version and --info" in result.stdout

0 commit comments

Comments
 (0)