typer-swissknife¶
A lightweight companion library for Typer.
Three helpers that cover common CLI patterns while staying close to Typer's own behaviour:
command: register a command with aliases, an optional ASCII-art banner, and undeclared options collected into**kwargs.add_typer: add a sub-app as a command group with aliases and an optional banner.get_ascii_help_text/render_ascii_art: build banner help text yourself.
A first taste¶
import typer
from typer_swissknife import add_typer, command
app = typer.Typer()
admin = typer.Typer()
@command(app, name="list", aliases=("ls",), ascii_name="List")
def list_items() -> None:
"""List all items."""
print("Listing items...")
@command(admin, aliases=("rm",))
def remove(item: str) -> None:
print(f"Removing {item}")
add_typer(
app,
admin,
name="admin",
ascii_name="Admin",
aliases=("a",),
help="Administrative commands",
)
if __name__ == "__main__":
app()
list,ls,adminandaall work.--helplists each one once, aslist (ls)andadmin (a).python app.py list --helpandpython app.py admin --helpstart with a bigLIST/ADMINbanner. The commands list keeps the plain one-line help.
Collect options you did not declare¶
import typer
from typer_swissknife import command
app = typer.Typer()
@command(app)
def report(name: str, **filters: int) -> None:
print(name, filters)
@command(app)
def version() -> None:
print("1.0")
if __name__ == "__main__":
app()
Each value is converted with the **kwargs annotation; a bad value is a normal usage error.
See Undeclared options.
Where to next¶
- Installation: one command.
- User guide: every feature, one page each.
- Recipes: complete CLIs.
- API: every public name and parameter.