Command groups¶
add_typer(app, sub_app, ...) works like app.add_typer(sub_app, ...), with
aliases and ascii_name / ascii_font on top.
Aliases for a group¶
import typer
from typer_swissknife import add_typer, command
app = typer.Typer()
users = typer.Typer(help="Manage users.")
@command(users, name="list", aliases=("ls",))
def list_users() -> None:
print("alice, bob")
@command(users)
def add(name: str) -> None:
print(f"Added {name}")
add_typer(app, users, name="users", aliases=("u", "usr"))
As with commands, users, u and usr are registered as hidden groups and
users (u, usr) as the visible entry. hidden=True hides that entry too.
Where the name comes from¶
name defaults to the name set on the sub-app, so these two are the same:
add_typer(app, typer.Typer(name="users"), aliases=("u",))
add_typer(app, typer.Typer(), name="users", aliases=("u",))
Aliases need a name. With aliases but neither name nor a name on sub_app,
add_typer raises ValueError.
Settings stay where you put them¶
Every parameter you do not pass keeps the value set on the sub-app itself, in
typer.Typer(...) or @sub_app.callback(...). Passing a parameter overrides it, as with
app.add_typer().
import typer
from typer_swissknife import add_typer
app = typer.Typer()
reports = typer.Typer(no_args_is_help=True)
@reports.callback()
def reports_callback() -> None:
"""Build and send reports."""
add_typer(app, reports, name="reports", aliases=("r",))
reports and r both show the help when called without a subcommand, and the help text
is "Build and send reports." from the callback docstring.
The passed-through parameters are cls, invoke_without_command, no_args_is_help,
subcommand_metavar, chain, result_callback, context_settings, callback, help,
epilog, short_help, options_metavar, add_help_option, hidden, deprecated and
rich_help_panel.
A banner on the group help page¶
python app.py users --help starts with a USERS banner followed by the group's help,
which is resolved as Typer would resolve it (the help argument, else the callback
docstring or the sub-app's help). The commands list of the parent still shows
"Manage users.". See ASCII-art banners.