utils.rst
This file
Path
docs/utils.rst
74 lines · 2.4 KiB
Last changed by
Merge pull request #88 from julen/doc-fixesArmin Ronacherbefore adoptione45ef4c
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
line 33why, one step back33Added filename format functionArmin Ronacherbefore adoption31043c3
lines 34–36why, one step back34Printing Filenames35------------------36Merge pull request #88 from julen/doc-fixesArmin Ronacherbefore adoptione45ef4c
lines 37–40why, one step back37Because filenames might not be unicode, formatting them can be a bit38tricky. Generally this is easier on Python 2 than on 3 as you can just39write the bytes to stdout with the print function, but at least on Python403 you will need to always operate in unicode.Added filename format functionArmin Ronacherbefore adoption31043c3
lines 41–51why, one step back4142The way this works with click is through the :func:`format_filename`43function. It does a best effort conversion of the filename to Unicode and44will never fail. This makes it possible to use these filenames in the45context of a full unicode string.4647Example::4849 click.echo('Path: %s' % click.format_filename(b'foo.txt'))5051Updated docs for streamsArmin Ronacherbefore adoptionfff6b28
lines 52–64why, one step back52Standard Streams53----------------5455For command line utilities it's very important to get access to input and56output streams reliably. Python generally provides access to these57streams through ``sys.stdout`` and friends, but unfortunately there are58API differences between 2.x and 3.x. Especially in regards to how these59streams respond to unicode and binary data there are wide differences.6061Because of this click provides the :func:`get_binary_stream` and62:func:`get_text_stream` which produce consistent results with different63Python versions and for widely misconfigured terminals.64Merge pull request #78 from julen/doc-fixesArmin Ronacherbefore adoption147bd4b
line 65why, one step back65The end result is that these functions will always return a functionalUpdated docs for streamsArmin Ronacherbefore adoptionfff6b28
lines 66–74why, one step back66stream object (except in very odd cases on Python 3, see67:ref:`python3-limitations`).6869Example::7071 import click7273 stdin_text = click.get_text_stream('stdin')74 stdout_binary = click.get_binary_stream('stdout')