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
--outputvalue asking for stdout instead of a file on disk.
- exception click_extra.output.MissingExtraError(extra, *, subject, package='click-extra')[source]¶
Bases:
ClickException,ImportErrorA 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 status1, with no traceback.It is an
ImportErroras 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 installcommand that installs the extra, in the wording every other optional-dependency error of the package uses.
- click_extra.output.is_stdout(path)[source]¶
Return
Truewhen path is the stdout sentinel-.Guards against accidentally creating a file literally named
-in the current directory.- Return type:
- 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
UnicodeEncodeErrora non-ASCII payload triggers on Windows, where the console defaults tocp1252. For a real path, missing parent directories are created first, absorbing themkdir -pa caller would otherwise need.Note
A
sys.stdoutthat something replaced is returned as-is: Click’s test runner, the Sphinx{click:run}directive that live-renders CLI output in the docs, acontextlib.redirect_stdout()block. The replacement is where the output must go, in the encoding its owner chose. Its descriptor can point somewhere else: underCliRunner(capture="fd"), Click sends descriptors1and2to the capture, butsys.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.
- click_extra.output.force_unicode_output()[source]¶
Switch
sys.stdoutandsys.stderrto UTF-8 when they do not speak it.Windows gives a redirected stream the encoding of its legacy code page,
cp1252on 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 aUnicodeEncodeErrorthe moment its output is piped or redirected, which is how automation runs a CLI. A narrowPYTHONIOENCODING, likeascii, 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
backslashreplaceerrors instead: an escaped character in the output is ugly, but it loses nothing and does not crash. A stream with noreconfigure(), like anio.StringIOcapture, 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: