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
1Utilities2=========34.. currentmodule:: click56Besides the functionality that click provides to interface with argument7parsing and handling, it also provides a bunch of addon functionality that8is useful for writing command line utilities.91011Printing to Stdout12------------------1314The 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!')2526Most 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)323334Standard 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.4647The end result is that these functions will always return a functional48stream 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')