click_extra.output module

Send command output to a file path or to stdout.

Helpers for the common --output option pattern, where a - value means “write to stdout” instead of creating a file literally named -, for standard streams that cannot encode what a command prints, and for the error a command reports when its output needs an optional dependency that is not installed.

click_extra.output.STDOUT_SENTINEL = '-'

Conventional --output value asking for stdout instead of a file on disk.

exception click_extra.output.MissingExtraError(extra, *, subject, package='click-extra')[source]

Bases: ClickException, ImportError

A command needs an optional dependency that is not installed.

The one failure every feature gated behind an extra raises once a user asks for it: a table, an export or a completion spec in a format whose writer is missing, a code snippet without Pygments. Click prints the message after Error: and exits with status 1, with no traceback.

It is an ImportError as well, since a failed import is what it reports: code calling such a feature outside a command catches it as one.

The message names the uv pip install command that installs the extra, in the wording every other optional-dependency error of the package uses.

Parameters:
  • extra (str) – the optional dependency group that installs what is missing.

  • subject (str) – what needs the dependency, opening the message.

  • package (str) – the distribution the extra belongs to.

click_extra.output.is_stdout(path)[source]

Return True when path is the stdout sentinel -.

Guards against accidentally creating a file literally named - in the current directory.

Return type:

bool

click_extra.output.prep_path(path)[source]

Open path for writing as UTF-8 text, or return stdout for -.

Yields a UTF-8 stream for a path and for the stdout of the process itself, sidestepping the UnicodeEncodeError a non-ASCII payload triggers on Windows, where the console defaults to cp1252. For a real path, missing parent directories are created first, absorbing the mkdir -p a caller would otherwise need.

Note

A sys.stdout that something replaced is returned as-is: Click’s test runner, the Sphinx {click:run} directive that live-renders CLI output in the docs, a contextlib.redirect_stdout() block. The replacement is where the output must go, in the encoding its owner chose. Its descriptor can point somewhere else: under CliRunner(capture="fd"), Click sends descriptors 1 and 2 to the capture, but sys.stdout.fileno() returns a copy of the original stdout descriptor, so a stream opened on it writes past the capture. A stdout with no file descriptor is returned as-is too.

Parameters:

path (Path) – The destination path, or - for stdout.

Return type:

IO[str]

Returns:

A writable text stream. The caller closes it, unless it is sys.stdout. The stream opened on the stdout descriptor uses closefd=False, so closing it leaves the real stdout open.

click_extra.output.force_unicode_output()[source]

Switch sys.stdout and sys.stderr to UTF-8 when they do not speak it.

Windows gives a redirected stream the encoding of its legacy code page, cp1252 on a Western install, which has no ✓, no ✘ and none of the box-drawing characters of a table border. A command printing one of them dies on a UnicodeEncodeError the moment its output is piped or redirected, which is how automation runs a CLI. A narrow PYTHONIOENCODING, like ascii, leads a POSIX host to the same crash.

A stream already speaking UTF-8 is left alone. A stream refusing the new encoding is asked for backslashreplace errors instead: an escaped character in the output is ugly, but it loses nothing and does not crash. A stream with no reconfigure(), like an io.StringIO capture, is skipped.

main() calls it as each run starts.

Todo

Remove once the minimum supported Python is 3.15: its UTF-8 mode, on by default (PEP 686), makes the standard streams speak UTF-8 unless the environment asks otherwise.

Return type:

None