Skip to content

Commit 8c93f4e

Browse files
authored
Migrate cli from typer to cyclopts (#117)
2 parents 7eb275d + cca5053 commit 8c93f4e

File tree

3 files changed

+154
-67
lines changed

3 files changed

+154
-67
lines changed

poetry.lock

+136-40
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ sshgen = "sshgen.cli:app"
1717
[tool.poetry.dependencies]
1818
python = "^3.12"
1919
ruamel-yaml = ">=0.17.31,<0.19.0"
20-
typer = "^0.9.0"
20+
cyclopts = "^2.4.2"
2121

2222
[tool.poetry.group.dev.dependencies]
2323
ruff = "*"

sshgen/cli.py

+17-26
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
#!/usr/bin/env python
2-
from typing import Annotated, Optional
2+
from typing import Annotated
33

4-
import typer
4+
from cyclopts import App, Group
5+
from cyclopts import Parameter
56

67
from sshgen import __app_name__, __version__
78
from sshgen.logger import init_logger
89
from sshgen.models.loglevel import LogLevel
910
from sshgen.utils.app import AppUtils
1011
from sshgen.utils.file import FileUtils
1112

12-
app = typer.Typer(no_args_is_help=True)
13+
app = App(version=f"{__app_name__} v{__version__}", version_flags=["--version"], help_flags=["--help"])
14+
app.meta.group_parameters = Group("Debug Output")
1315

1416

15-
@app.command("generate")
17+
@app.command(name="generate")
1618
def generate_hosts_file(
17-
hosts_file: Annotated[str, typer.Option("--hosts-file", "-h")] = "./hosts.yml",
18-
output: Annotated[str, typer.Option("--output", "-o")] = "./config",
19+
hosts_file: Annotated[str, Parameter(name=["--hosts-file", "-h"], allow_leading_hyphen=True)] = "./hosts.yml",
20+
output: Annotated[str, Parameter(["--output", "-o"])] = "./config",
1921
) -> None:
2022
"""
2123
Command to generate SSH configuration file.
24+
2225
By default, it uses file hosts.yml placed in your working directory and outputs to the file named "config".
26+
2327
Example usage: sshgen generate -o my_ssh_config
2428
"""
2529

@@ -30,37 +34,24 @@ def generate_hosts_file(
3034
sshgen.generate_ssh_config()
3135

3236

33-
def _version_callback(value: bool) -> None:
34-
if value:
35-
typer.echo(f"{__app_name__} v{__version__}")
36-
raise typer.Exit()
37-
38-
39-
# noinspection PyUnusedLocal
40-
@app.callback()
37+
@app.meta.default()
4138
def main(
39+
*tokens: Annotated[str, Parameter(show=False, allow_leading_hyphen=True)],
4240
verbose: Annotated[
43-
Optional[bool],
44-
typer.Option(
45-
"--verbose",
46-
is_eager=True,
47-
envvar=["SSHGEN_VERBOSE", "SSHGEN_DEBUG"],
48-
help="Switch log level to DEBUG, default is INFO.",
49-
),
41+
bool, Parameter(name="--verbose", show_default=False, negative="", env_var=["SSHGEN_VERBOSE", "SSHGEN_DEBUG"])
5042
] = False,
51-
version: Annotated[
52-
Optional[bool],
53-
typer.Option("-v", "--version", is_eager=True, callback=_version_callback),
54-
] = None,
5543
) -> None:
5644
"""
5745
sshgen generates SSH configuration file based on an Ansible hosts file.
5846
"""
47+
5948
if verbose:
6049
init_logger(level=LogLevel.DEBUG)
6150
else:
6251
init_logger(level=LogLevel.INFO)
6352

53+
app(tokens)
54+
6455

6556
if __name__ == "__main__":
66-
app()
57+
app.meta()

0 commit comments

Comments
 (0)