Print a message and newline to stdout or a file. This should be used instead of :func:`print` because it provides better support for different data, files, and environments. Compared to :func:`print`, this does the following: - Ensures that the output encoding is not misconfigure
(
message: object = None,
file: t.IO[t.Any] | None = None,
nl: bool = True,
err: bool = False,
color: bool | None = None,
)
| 244 | |
| 245 | |
| 246 | def echo( |
| 247 | message: object = None, |
| 248 | file: t.IO[t.Any] | None = None, |
| 249 | nl: bool = True, |
| 250 | err: bool = False, |
| 251 | color: bool | None = None, |
| 252 | ) -> None: |
| 253 | class="st">"""Print a message and newline to stdout or a file. This should be |
| 254 | used instead of :func:`print` because it provides better support |
| 255 | for different data, files, and environments. |
| 256 | |
| 257 | Compared to :func:`print`, this does the following: |
| 258 | |
| 259 | - Ensures that the output encoding is not misconfigured on Linux. |
| 260 | - Supports Unicode in the Windows console. |
| 261 | - Supports writing to binary outputs, and supports writing bytes |
| 262 | to text outputs. |
| 263 | - Supports colors and styles on Windows. |
| 264 | - Removes ANSI color and style codes if the output does not look |
| 265 | like an interactive terminal. |
| 266 | - Always flushes the output. |
| 267 | |
| 268 | :param message: The string or bytes to output. Other objects are |
| 269 | converted to strings. |
| 270 | :param file: The file to write to. Defaults to ``stdout``. |
| 271 | :param err: Write to ``stderr`` instead of ``stdout``. |
| 272 | :param nl: Print a newline after the message. Enabled by default. |
| 273 | :param color: Force showing or hiding colors and other styles. By |
| 274 | default Click will remove color if the output does not look like |
| 275 | an interactive terminal. |
| 276 | |
| 277 | .. versionchanged:: 6.0 |
| 278 | Support Unicode output on the Windows console. Click does not |
| 279 | modify ``sys.stdout``, so ``sys.stdout.write()`` and ``print()`` |
| 280 | will still not support Unicode. |
| 281 | |
| 282 | .. versionchanged:: 4.0 |
| 283 | Added the ``color`` parameter. |
| 284 | |
| 285 | .. versionadded:: 3.0 |
| 286 | Added the ``err`` parameter. |
| 287 | |
| 288 | .. versionchanged:: 2.0 |
| 289 | Support colors on Windows if colorama is installed. |
| 290 | class="st">""" |
| 291 | if file is None: |
| 292 | if err: |
| 293 | file = _default_text_stderr() |
| 294 | else: |
| 295 | file = _default_text_stdout() |
| 296 | |
| 297 | class="cm"># There are no standard streams attached to write to. For example, |
| 298 | class="cm"># pythonw on Windows. |
| 299 | if file is None: |
| 300 | return |
| 301 | |
| 302 | match message: |
| 303 | case str() | bytes() | bytearray(): |
no test coverage detected