Skip to content

Commit 671d5bb

Browse files
authored
feat: add env variable to disable rich CLI output (#2654)
* feat: add env variable to disable rich CLI output * lint: fix mypy
1 parent 974db61 commit 671d5bb

File tree

2 files changed

+40
-3
lines changed
  • docs/docs/en/getting-started
  • faststream/_internal/cli

2 files changed

+40
-3
lines changed

docs/docs/en/getting-started/cli.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,33 @@ You can pass any custom flags for logging configuration, it's `--log-level` or `
132132

133133
### Event Loop
134134

135-
FastStream is built on [anyio](https://github.com/agronholm/anyio) and supports using any event loop implementation (for example, [asyncio](https://docs.python.org/3/library/asyncio-eventloop.html), [uvloop](https://github.com/MagicStack/uvloop), [winloop](https://github.com/Vizonex/Winloop), and [rloop](https://github.com/gi0baro/rloop)). By default, FastStream uses behavior from anyio: use uvloop (on Unix) if it exists, with fallback to asyncio. You can set event loop factory explicitly via the `--loop` option.
135+
**FastStream** is built on [anyio](https://github.com/agronholm/anyio) and supports using any event loop implementation (for example, [asyncio](https://docs.python.org/3/library/asyncio-eventloop.html), [uvloop](https://github.com/MagicStack/uvloop), [winloop](https://github.com/Vizonex/Winloop), and [rloop](https://github.com/gi0baro/rloop)). By default, **FastStream** uses behavior from anyio: use uvloop (on Unix) if it exists, with fallback to asyncio.
136+
137+
You can set the event loop factory explicitly via the `--loop` option in the CLI:
136138

137139
```shell
138140
faststream run main:app --loop=uvloop:new_event_loop
139141
```
142+
143+
Alternatively, you can specify the event loop implementation using the `FASTSTREAM_LOOP` environment variable. For example:
144+
145+
```shell
146+
FASTSTREAM_LOOP=uvloop:new_event_loop faststream run main:app
147+
```
148+
149+
This lets you control the event loop used by **FastStream** either via command-line flags or by setting an environment variable, according to your deployment or development needs.
150+
151+
### Rich Output Mode
152+
153+
**FastStream** CLI uses [Typer](https://typer.tiangolo.com/){.external-link target="_blank"} rich formatting for its help and error messages. You can control this behavior with the `FASTSTREAM_CLI_RICH_MODE` environment variable:
154+
155+
```shell
156+
# available values: rich, markdown, or none
157+
FASTSTREAM_CLI_RICH_MODE=none faststream run main:app
158+
```
159+
160+
- `rich` *(default)* – use Typer's rich markup styling.
161+
- `markdown` – render CLI output using markdown-compatible formatting.
162+
- `none` – disable rich formatting and use plain-text output.
163+
164+
This is useful when working in terminals that do not support ANSI styling or when copying CLI output into plain-text environments.

faststream/_internal/cli/main.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import logging
2+
import os
23
import sys
34
import warnings
45
from contextlib import suppress
56
from pathlib import Path
6-
from typing import TYPE_CHECKING, Any, cast
7+
from typing import TYPE_CHECKING, Any, Literal, cast
78

89
import anyio
910
import typer
@@ -37,7 +38,18 @@
3738
if TYPE_CHECKING:
3839
from faststream._internal.broker import BrokerUsecase
3940

40-
cli = typer.Typer(pretty_exceptions_short=True)
41+
rich_mode = os.getenv("FASTSTREAM_CLI_RICH_MODE", "rich")
42+
if rich_mode == "none":
43+
rich_markup_mode: Literal["markdown", "rich"] | None = None
44+
elif rich_mode in {"md", "markdown"}:
45+
rich_markup_mode = "markdown"
46+
elif rich_mode == "rich":
47+
rich_markup_mode = "rich"
48+
else:
49+
msg = f"Invalid rich mode: {rich_mode}"
50+
raise ValueError(msg)
51+
52+
cli = typer.Typer(pretty_exceptions_short=True, rich_markup_mode=rich_markup_mode)
4153
cli.add_typer(docs_app, name="docs", help="Documentations commands")
4254

4355

0 commit comments

Comments
 (0)