Typer Support¶
Experimental
This feature is still experimental. Please report any bugs or issues you run into!
You don't need to use rich-click directly to get access to a lot of rich-click's great features.
If you are a fan of Typer and you'd like to use rich-click's themes, you can patch Typer to use rich-click
and set the global config's THEME
(and other config options) to whatever you want.
All you need to do is import patch_typer()
from rich_click.patch
.
This does not need to be done at the top of the file, in fact you can do it right before calling app.run()
or typer.run(...)
:
# /// script
# dependencies = ["rich-click>=1.9", "typer>=0.15"]
# ///
from enum import Enum
import typer
from typing_extensions import Annotated
class LogLevel(str, Enum):
debug = "debug"
info = "info"
warn = "warn"
error = "error"
def main(
id: Annotated[int, typer.Argument(min=0, max=1000)],
age: Annotated[int, typer.Option(min=18)] = 20,
score: Annotated[float, typer.Option(max=100)] = 0,
log_level: Annotated[LogLevel, typer.Option(rich_help_panel="Logging")] = LogLevel.info,
color: Annotated[bool, typer.Option(rich_help_panel="Logging")] = True,
) -> None:
print(f"id={id} age={age} score={score} log_level={log_level} color={color}")
if __name__ == "__main__":
from rich_click.patch import patch_typer
import rich_click.rich_click as rc
patch_typer()
rc.THEME = "star-slim"
typer.run(main)
Output
Running the above Typer CLI works the same as you would otherwise expect, except now with a rich-click theme applied:
Patching Typer via the rich-click
CLI¶
You can also patch Typer as an end-user of any Typer CLI via the rich-click
CLI.
rich-click my-typer-cli --help
In addition to giving access to rich-click's themes, another benefit of this is being able to generate HTML and SVG help text easily. Although, do note that Typer and rich-click have some minor differences in how they render help text.
More information about usage of the rich-click
CLI is in the rich-click CLI docs, or you can run rich-click --help
to view the CLI.