|
8 | 8 | # |
9 | 9 | # SPDX-License-Identifier: MIT |
10 | 10 | # |
11 | | -from typer import Typer |
| 11 | +from typing import Annotated |
| 12 | +from typer import Typer, Option |
12 | 13 | from .info import PackageInfo |
13 | 14 | from . import utils, console |
14 | 15 |
|
15 | 16 |
|
16 | 17 | 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 |
20 | 21 | )) |
21 | 22 |
|
22 | 23 |
|
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 | +)] |
26 | 32 |
|
27 | 33 |
|
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") |
33 | 46 |
|
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('') |
0 commit comments