utils.rst
This file
Path
docs/utils.rst
49 lines · 1.5 KiB
Last changed by
Updated docs for streamsArmin Ronacherbefore adoptionfff6b28
History of this fileTake it
Updated docs for streamsArmin Ronacherbefore adoptionfff6b28
lines 1–49why, one step back1Utilities2=========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.91011Priting 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!')252627Standard Streams28----------------2930For command line utilities it's very important to get access to input and31output streams reliably. Python generally provides access to these32streams through ``sys.stdout`` and friends, but unfortunately there are33API differences between 2.x and 3.x. Especially in regards to how these34streams respond to unicode and binary data there are wide differences.3536Because of this click provides the :func:`get_binary_stream` and37:func:`get_text_stream` which produce consistent results with different38Python versions and for widely misconfigured terminals.3940The end result is that these functions will always reutrn a functional41stream object (except in very odd cases on Python 3, see42:ref:`python3-limitations`).4344Example::4546 import click4748 stdin_text = click.get_text_stream('stdin')49 stdout_binary = click.get_binary_stream('stdout')