utils.rst
This file
Path
docs/utils.rst
56 lines · 1.8 KiB
Last changed by
Merge pull request #80 from dirn/patch-1Armin Ronacherbefore adoptionc7846b3
History of this fileTake it
Updated docs for streamsArmin Ronacherbefore adoptionfff6b28
lines 1–5why, one step back1Utilities2=========34.. currentmodule:: click5Merge pull request #78 from julen/doc-fixesArmin Ronacherbefore adoption147bd4b
line 6why, one step back6Besides the functionality that click provides to interface with argumentUpdated docs for streamsArmin Ronacherbefore adoptionfff6b28
lines 7–10why, one step back7parsing and handling, it also provides a bunch of addon functionality that8is useful for writing command line utilities.910Merge pull request #80 from dirn/patch-1Armin Ronacherbefore adoptionc7846b3
lines 11–12why, one step back11Printing to Stdout12------------------Updated docs for streamsArmin Ronacherbefore adoptionfff6b28
lines 13–25why, one step back1314The most obvious helper is the :func:`echo` function which in many ways15works like the Python print statement or function. The main difference is16that it works the same on Python 2 and 3 and it intelligently detects17misconfigured output streams and will never fail (except on Python 3, for18more information see :ref:`python3-limitations`).1920Example::2122 import click2324 click.echo('Hello World!')25Added example on how to supress newlines.Armin Ronacherbefore adoptioned9594d
lines 26–32why, one step back26Most importantly it can print both unicode and binary data, unlike the27builtin ``print`` function on Python 3 which cannot output any bytes. It28will however emit a trailing newline by default which needs to be29supressed by passing ``nl=False``::3031 click.echo(b'\xe2\x98\x83', nl=False)32Updated docs for streamsArmin Ronacherbefore adoptionfff6b28
lines 33–46why, one step back3334Standard Streams35----------------3637For command line utilities it's very important to get access to input and38output streams reliably. Python generally provides access to these39streams through ``sys.stdout`` and friends, but unfortunately there are40API differences between 2.x and 3.x. Especially in regards to how these41streams respond to unicode and binary data there are wide differences.4243Because of this click provides the :func:`get_binary_stream` and44:func:`get_text_stream` which produce consistent results with different45Python versions and for widely misconfigured terminals.46Merge pull request #78 from julen/doc-fixesArmin Ronacherbefore adoption147bd4b
line 47why, one step back47The end result is that these functions will always return a functionalUpdated docs for streamsArmin Ronacherbefore adoptionfff6b28
lines 48–56why, one step back48stream object (except in very odd cases on Python 3, see49:ref:`python3-limitations`).5051Example::5253 import click5455 stdin_text = click.get_text_stream('stdin')56 stdout_binary = click.get_binary_stream('stdout')