More documentation fixes
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kevin Yap · committed by Kevin Yap · parent d541802 · Browse files · 10 files·+144 added−146 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
10 files+144 added−146 removed
- other:+144 added−146 removed
Provenance
- Commit
98910c1- Tree
21863d0- Author
Kevin Yap <me@kevinyap.ca>- Committer
Kevin Yap <me@kevinyap.ca>- Parent
d541802
10 files changed+144 added−146 removed
modified:docs/documentation.rst+15 added-15 removed
1 Documenting Scripts2 ===================3 4 Click makes it very easy to document your command line tools. First of5-all it automatically generates help pages for you. While these are6-currently not customizable in layout, all the texts can be changed.5+all, it automatically generates help pages for you. While these are6+currently not customizable in layout, all of the text can be changed.7 8 Help Texts9 ----------10 11-Commands and options accept help arguments. In case of commands the doc11+Commands and options accept help arguments. In the case of commands, the doc12 string of the function is automatically used if provided.13 14 Simple example:15 13 unmodified lines29 30 invoke(hello, args=['--help'])31 32 Arguments cannot be documented this way. This is to follow general33-convention of Unix tools to use arguments only for the most necessary33+convention of Unix tools using arguments for only the most necessary34 things and to document them in the introduction text by referring to them35 by name.36 37 Preventing Rewrapping38 ---------------------39 40 The default behavior of click is to rewrap text to work correctly for the41-width of the terminal. In some circumstances this can become a problem.41+width of the terminal. In some circumstances, this can become a problem.42 The main issue is showing code examples where newlines are significant.43 44-Rewrapping can be disabled on a per-paragraph case by adding a line with45-just the ``\b`` escape marker in it. This line will be removed from the44+Rewrapping can be disabled on a per-paragraph basis by adding a line with45+solely the ``\b`` escape marker in it. This line will be removed from the46 help text and rewrapping will be disabled.47 48 Example:49 23 unmodified lines73 74 invoke(cli, args=['--help'])75 76 77-Meta Vars78----------77+Meta Variables78+--------------79 80 Options and parameters accept a ``metavar`` argument that can change the81 meta variable in the help page. The default version is the parameter name82-in uppercase with underscores and sometimes annotated differently if83-optional. This can be customized at all levels:82+in uppercase with underscores, but can be annotated differently if83+desired. This can be customized at all levels:84 85 .. click:example::86 87 @click.command(options_metavar='<options>')9 unmodified lines97 98 .. click:run::99 100 invoke(hello, args=['--help'])101- 101 102+103 Command Short Help104 ------------------105 106-For commands a short help is generated. By default it's the first part107-(until the first dot) of the help message of the command unless it's too108-long. This can also be overridden:106+For commands, a short help snippet is generated. By default, it's the first107+sentence of the help message of the command, unless it's too long. This can108+also be overridden:109 110 .. click:example::111 112 @click.group()16 unmodified linesmodified:docs/options.rst+38 added-39 removed
2 unmodified lines3 4 .. currentmodule:: click5 6 Adding options to commands can be accomplished by the :func:`option`7-decorator. Since options can come in various different versions there7+decorator. Since options can come in various different versions, there8 are a ton of parameters to configure their behavior.9 10 Basic Value Options11 -------------------12 13 The most basic option is a value option. These options accept one14-argument which is a value. If no type is provided the type of the default14+argument which is a value. If no type is provided, the type of the default15 value is used. If no default value is provided the type is assumed to be16-:data:`STRING`. The name of the parameter is by default the first long17-option defined, otherwise the first short one.16+:data:`STRING`. By default, the name of the parameter is the first long17+option defined; otherwise the first short one is used.18 19 .. click:example::20 21 @click.command()12 unmodified lines34 35 Multi Value Options36 -------------------37 38-Sometimes you have options that take more than one argument. For options38+Sometimes, you have options that take more than one argument. For options,39 only a fixed number of arguments is supported. This can be configured by40 the ``nargs`` parameter. The values are then stored as a tuple.41 42 .. click:example::11 unmodified lines54 55 Multiple Options56 ----------------57 58-Similar to ``nargs`` there is also the case where sometimes you want to58+Similar to ``nargs`` there is also the case where you sometimes want to59 support a parameter to be provided multiple times to and have all values60 recorded and not just the last one. For instance ``git commit -m foo -m61 bar`` would record two lines for the commit message. ``foo`` and ``bar``.62 This can be accomplished with the ``multiple`` flag:15 unmodified lines78 79 Counting80 --------81 82-In some very rare circumstances it's interesting to use the repeating of83-options to count an integer up. This for instance can be used for84-verbosity flags:82+In some very rare circumstances, it's interesting to use the repetition83+of options to count an integer up. This can be used for verbosity flags,84+for instance:85 86 .. click:example::87 88 @click.command()11 unmodified lines100 -------------101 102 Boolean flags are options that can be enabled or disabled. This can be103 accomplished by defining two flags in one go separated by a slash (``/``)104-for enabling or disabling the option. (If a slash is in an option string104+for enabling or disabling the option. (If a slash is in an option string,105 click automatically knows that it's a boolean flag and will pass106-``is_flag=True`` implicitly). Click always wants you to provide an enable106+``is_flag=True`` implicitly.) Click always wants you to provide an enable107 and disable flag so that you can change the default later.108 109 Example:110 15 unmodified lines126 127 invoke(info, args=['--shout'])128 invoke(info, args=['--no-shout'])129 130-If you really don't want an off-switch you can just define one and130+If you really don't want an off-switch, you can just define one and131 manually inform click that something is a flag:132 133 .. click:example::134 15 unmodified lines150 151 Feature Switches152 ----------------153 154-In addition to boolean flags there are also feature switches. These are155-implemented by setting multiple options to the same parameter name and by156-defining a flag value. Note that by providing the ``flag_value`` parameter154+In addition to boolean flags, there are also feature switches. These are155+implemented by setting multiple options to the same parameter name and156+defining a flag value. Note that by providing the ``flag_value`` parameter,157 click will implicitly set ``is_flag=True``.158 159-To set a default flag assign a value of `True` to the flag that should be159+To set a default flag, assign a value of `True` to the flag that should be160 the default.161 162 .. click:example::163 46 unmodified lines210 211 Prompting212 ---------213 214-Sometimes you want parameters that can either be provided from the command215-line or if not, you want to ask for user input. This can be implemented216-with click by defining a prompt string.214+Sometimes, you want parameters that can be provided from the command line,215+but if not, ask for user input. This can be implemented with click by216+defining a prompt string.217 218 Example:219 220 .. click:example::9 unmodified lines230 231 invoke(hello, args=['--name=John'])232 invoke(hello, input=['John'])233 234-If you are not happy with the default prompt string you can ask for234+If you are not happy with the default prompt string, you can ask for235 a different one:236 237 .. click:example::238 27 unmodified lines266 .. click:run::267 268 invoke(encrypt, input=['secret', 'secret'])269 270-Because this combination of parameters is quite common this can also be270+Because this combination of parameters is quite common, this can also be271 replaced with the :func:`password_option` decorator:272 273 .. click:example::274 5 unmodified lines280 Callbacks and Eager Options281 ---------------------------282 283 Sometimes you want a parameter to completely change the execution flow.284-This for instance is the case when you want to have a ``--version``284+For instance, this is the case when you want to have a ``--version``285 parameter that prints out the version and then exits the application.286 287 In such cases you need two concepts: eager parameters and a callback. An288 eager parameter is a parameter that is handled before others and a25 unmodified lines314 def hello():315 click.echo('Hello World!')316 317 The `expose_value` parameter prevents the now pretty pointless ``version``318-parameter to be passed to the callback. If that was not specified a318+parameter to be passed to the callback. If that was not specified, a319 boolean would be passed to the `hello` script. The `resilient_parsing`320 flag is set on the context if click wants to parse the command line321 without and destructive behavior that would change the execution flow. In322-this case because we would exit the program, we instead do nothing in that323-case.322+this case, because we would exit the program, we instead do nothing.323 324 What it looks like:325 326 .. click:run::8 unmodified lines335 336 Yes Parameters337 --------------338 340-For dangerous operations it's very useful to be able to ask a user for339+For dangerous operations, it's very useful to be able to ask a user for340 confirmation. This can be done by adding a boolean ``--yes`` flag and341 asking for confirmation if the user did not provide it and to fail in a342 callback:343 16 unmodified lines360 361 invoke(dropdb, input=['n'])362 invoke(dropdb, args=['--yes'])363 365-Because this combination of parameters is quite common this can also be364+Because this combination of parameters is quite common, this can also be365 replaced with the :func:`confirmation_option` decorator:366 367 .. click:example::368 11 unmodified lines380 ---------------------------------381 382 A very useful feature of click is the ability to accept parameters from383 environment variables in addition to regular parameters. This allows385-tools to be automated much easier. For instance you might want to pass386-the config file as ``--config`` parameter but also support exporting a387-``TOOL_CONFIG=hello.cfg`` key / value pair for a nicer development384+tools to be automated much easier. For instance, you might want to pass385+a configuration file with a ``--config`` parameter but also support exporting386+a ``TOOL_CONFIG=hello.cfg`` key-value pair for a nicer development387 experience.388 389 This is supported by click in two ways. One is to automatically build390 environment variables which is supported for options only. To enable this392-feature the ``auto_envvar_prefix`` parameter needs to be passed to the393-script that is invoked. Each command and parameter is then added as394-underscore-separated variable in uppercase. So if you have a subcommand391+feature, the ``auto_envvar_prefix`` parameter needs to be passed to the392+script that is invoked. Each command and parameter is then added as an393+uppercase underscore-separated variable. If you have a subcommand394 called ``foo`` taking an option called ``bar`` and the prefix is396-``MY_TOOL`` then the variable is ``MY_TOOL_FOO_BAR``.395+``MY_TOOL``, then the variable is ``MY_TOOL_FOO_BAR``.396 397 Example usage:398 399 .. click:example::49 unmodified lines449 450 The default implementation for all types is to split on whitespace. The451 exceptions to this rule are the :class:`File` and :class:`Path` types452 which both split according to the operating system's path splitting rules.454-On UNIX systems like Linux and OS X the splitting happens for those on455-every colon (``:``) and for Windows on a semicolon (``;``).453+On UNIX systems like Linux and OS X, the splitting happens for those on454+every colon (``:``), and for Windows, on every semicolon (``;``).455 456 Example usage:457 458 .. click:example::49 unmodified lines508 A special mention should go to the :class:`IntRange` type which works very509 similar to the :data:`INT` type but restricts the value to fall into a510 specific range (inclusive on both edges). It has two modes:511 513-- the default mode (non clamping mode) where a value that falls outside512+- the default mode (non-clamping mode) where a value that falls outside513 of the range will cause an error.514 - an optional clamping mode where a value that falls outside of the515 range will be clamped. This means that a range of ``0-5`` would516 return ``5`` for the value ``10`` or ``0`` for the value ``-1`` (for18 unmodified lines535 536 invoke(repeat, args=['--count=1000', '--digit=5'])537 invoke(repeat, args=['--count=1000', '--digit=12'])538 540-If you pass ``None`` for any of the edges it means that the range is open539+If you pass ``None`` for any of the edges, it means that the range is open540 at that side.541 542 Callbacks for Validation543 ------------------------4 unmodified lines548 parameter callbacks. These callbacks can both modify values as well as549 raise errors if the validation does not work.550 551 In click 1.0 you can only raise the :exc:`UsageError` but starting with553-click 2.0 you can also raie the :exc:`BadParameter` error which has the552+click 2.0 you can also raise the :exc:`BadParameter` error which has the553 added advantage that it will automatically format the error message to554 also contain the parameter name.555 556 Example:25 unmodified linesmodified:docs/parameters.rst+6 added-6 removed
8 unmodified lines9 name indicates an option is optional. While arguments can be optional10 within reason they are much more restricted in how they can be optional.11 12 To help you decide between options and arguments, the recommendation is13-to use arguments exclusively for things like going to subcommands or input 13+to use arguments exclusively for things like going to subcommands or input14 filenames / URLs and have everything else be an option instead.15 16 Differences17 -----------49 unmodified lines67 .. autoclass:: IntRange68 :noindex:69 70 Custom parameter types can be implemented by subclassing71-:class:`click.ParamType`. For simple cases passing a python function that72-fails with a `ValueError` is also supported though discouraged.71+:class:`click.ParamType`. For simple cases, passing a Python function that72+fails with a `ValueError` is also supported, though discouraged.73 74 Parameter Names75 ---------------76 12 unmodified lines89 90 Implementing Custom Types91 -------------------------92 93-To implement a custom type you need to subclass the :class:`ParamType`93+To implement a custom type, you need to subclass the :class:`ParamType`94 class. Types can be invoked with or without context and parameter object95 which is why they need to be able to deal with this.96 97-The following code implements an integer type that accepts hex, octal97+The following code implements an integer type that accepts hex and octal98 numbers in addition to normal integers and converts them into regular99 integers::100 101 import click12 unmodified lines114 self.fail('%s is not a valid integer' % value, param, ctx)115 116 BASED_INT = BasedIntParamType()117 118-As you can see a subclass needs to implement the :meth:`ParamType.convert`118+As you can see, a subclass needs to implement the :meth:`ParamType.convert`119 method and optionally provide the :attr:`ParamType.name` attribute. The120 latter can be used for documentation purposes.modified:docs/prompts.rst+9 added-9 removed
7 unmodified lines8 prompts at a later point independently.9 10 This can be accomplished with the :func:`prompt` function which asks for11 valid input according to a type or the :func:`confirm` function which asks12-for confirmation (yes / no).12+for confirmation (yes/no).13 14 Option Prompts15 --------------16 17 Option prompts are integrated into the option interface. See18-:ref:`option-prompting` for more information. Internally it18+:ref:`option-prompting` for more information. Internally, it19 automatically calls into :func:`prompt` or :func:`confirm` as necessary.20 21 Input Prompts22 -------------23 24-To manually ask for user input you can use the :func:`prompt` function.25-By default it accepts any unicode string but you can ask for any other24+To manually ask for user input, you can use the :func:`prompt` function.25+By default, it accepts any Unicode string, but you can ask for any other26 type. For instance you can ask for a valid integer::27 28 value = click.prompt('Please enter a valid integer', type=int)29 30-The type is also automatically detected if a default is provided. The31-following for instance will only accept floats::30+The type is also automatically detected if a default is provided. For31+instance, the following will only accept floats::32 33 value = click.prompt('Please enter a number', default=42.0)34 35 Confirmation Prompts36 --------------------37 38-To ask if a user wants to continue with an action the :func:`confirm`39-function comes in handy. By default it returns the result of the prompt40-as boolean value::38+To ask if a user wants to continue with an action, the :func:`confirm`39+function comes in handy. By default, it returns the result of the prompt40+as a boolean value::41 42 if click.confirm('Do you want to continue?'):43 click.echo('Well done!')44 3 unmodified linesmodified:docs/python3.rst+23 added-23 removed
21 unmodified lines22 While there are encoding hints for all of this, there are generally23 some situations where this can break. The most common one is SSH24 connections to machines with different locales.25 26- Misconfigured environments can currently cause a wide range of unicode26+ Misconfigured environments can currently cause a wide range of Unicode27 problems on Python 3 due to the lack of support for roundtripping28 surrogate escapes. This will not be fixed in click itself!29 30 For more information see :ref:`python3-surrogates`.16 unmodified lines47 sys.stdin = io.TextIOWrapper(in_stream, encoding='utf-8')48 out_stream = io.BytesIO()49 sys.stdout = io.TextIOWrapper(out_stream, encoding='utf-8')50 51- Remember that in that case you need to use ``out_stream.getvalue()``51+ Remember that in that case, you need to use ``out_stream.getvalue()``52 and not ``sys.stdout.getvalue()`` if you want to access the buffer53 contents as the wrapper will not forward that method.54 55-Python 2 / 3 Differences55+Python 2 and 3 Differences56 ------------------------57 58 Click attempts to minimize the differences between Python 2 and Python 359 by following the best practices for both languages.60 61-On Python 2 the following is true:61+On Python 2, the following is true:62 63 * ``sys.stdin``, ``sys.stdout``, and ``sys.stderr`` are opened in binary64- mode but under some circumstances they support unicode output. Click64+ mode, but under some circumstances they support Unicode output. Click65 attempts to not subvert this but provides support for forcing streams66- to be unicode-based.66+ to be Unicode-based.67 * ``sys.argv`` is always bytes-based. Click will pass bytes to all68 input types and convert as necessary. The :class:`STRING` type69 automatically will decode properly the input value into a string by70 trying the most appropriate encodings.71-* When dealing with files, click will never go through the unicode APIs71+* When dealing with files, click will never go through the Unicode APIs72 and will instead use the operating system's byte APIs to open the73 files.74 75-On Python 3 the following is true:75+On Python 3, the following is true:76 77 * ``sys.stdin``, ``sys.stdout`` and ``sys.stderr`` are by default78 text-based. When click needs a binary stream, it attempts to discover79 the underlying binary stream. See :ref:`python3-limitations` for how80 this works.81-* ``sys.argv`` is always unicode-based. This also means that the native82- type for input values to the types in click is unicode and not bytes.81+* ``sys.argv`` is always Unicode-based. This also means that the native82+ type for input values to the types in click is Unicode and not bytes.83 84 This causes problems when the terminal is incorrectly set and Python85- does not figure out the encoding. In that case the unicode string85+ does not figure out the encoding. In that case, the Unicode string86 will contain error bytes encoded as surrogate escapes.87-* When dealing with files, click will always use the unicode file system87+* When dealing with files, click will always use the Unicode file system88 API calls by using the operating system's reported or guessed89 filesystem encoding. Surrogates are supported for filenames, so it90 should be possible to open files through the :class:`File` type even91 if the environment is misconfigured.2 unmodified lines94 95 Python 3 Surrogate Handling96 ---------------------------97 98-Click on Python 3 does all the unicode handling in the standard library99-and is subject to its behavior. On Python 2 click does all the unicode98+Click on Python 3 does all the Unicode handling in the standard library99+and is subject to its behavior. On Python 2 click does all the Unicode100 handling itself which means there are differences in error behavior.101 102-The most glaring difference is that on Python 2 unicode will "just work"102+The most glaring difference is that on Python 2, Unicode will "just work",103 but requires extra care on Python 3. The reason for this is that on104-Python 3 the encoding detection is done in the interpreter and on Linux104+Python 3, the encoding detection is done in the interpreter and on Linux105 and certain other operating systems its encoding handling is problematic.106 107-The biggest source of frustration there is that click scripts invoked by107+The biggest source of frustration is that click scripts invoked by108 init systems (sysvinit, upstart, systemd, etc.), deployment tools (salt,109-puppet) or cron jobs (cron) will refuse to work unless a unicode locale is109+puppet) or cron jobs (cron) will refuse to work unless a Unicode locale is110 exported.111 112 If click encounters such an environment it will prevent further execution113 to force you to set a locale. This is done because click cannot know114 about the state of the system once it's invoked and restore the values115-before Python's unicode handling kicked in.115+before Python's Unicode handling kicked in.116 117 If you see something like this error on Python 3::118 119 Traceback (most recent call last):3 unmodified lines123 to Python 2 or consult for http://click.pocoo.org/python3/124 mitigation steps.125 126 You are dealing with an environment where Python 3 thinks you are127-restricted to ASCII data. The solution for these problems is different127+restricted to ASCII data. The solution to these problems is different128 depending on which locale your computer is running in.129 130-For instance if you have a German linux machine you can fix the problem130+For instance if you have a German Linux machine you can fix the problem131 by exporting the locale to ``de_DE.utf-8``::132 133 export LC_ALL=de_DE.utf-8134 export LANG=de_DE.utf-8135 136 If you are on a US machine, ``en_EN.utf-8`` is the encoding of choice. On137-some newer linux systems you can also try ``C.UTF-8`` as locale::137+some newer Linux systems you can also try ``C.UTF-8`` as locale::138 139 export LC_ALL=C.UTF-8140 export LANG=C.UTF-8141 7 unmodified lines149 <http://bugs.python.org/issue19977>`_150 * `Python 3 raises Unicode errors in the C locale151 <http://bugs.python.org/issue19846>`_152 * `LC_CTYPE=C: pydoc leaves terminal in an unusable state153- <http://bugs.python.org/issue21398>`_ (this is relevant to Click153+ <http://bugs.python.org/issue21398>`_ (this is relevant to click154 because the pager support is provided by the stdlib pydoc module)modified:docs/quickstart.rst+10 added-11 removed
38 unmodified lines39 40 Basic Concepts41 --------------42 43-Click is based on declaring commands through decorators. Internally there44-is a non-decorator interface for advanced use cases but it's discouraged43+Click is based on declaring commands through decorators. Internally, there44+is a non-decorator interface for advanced use cases, but it's discouraged45 for high-level usage.46 47 A function becomes a click command line tool by decorating it through48-:func:`click.command`. In the most simple version just decorating a49-function with this decorator will make it into a callable script:48+:func:`click.command`. At its simplest, just decorating a function49+with this decorator will make it into a callable script:50 51 .. click:example::52 53 import click33 unmodified lines87 What this means is that the :func:`echo` function applies some error88 correction in case the terminal is misconfigured instead of dying with an89 :exc:`UnicodeError`.90 91-As an added benefit, starting with click 2.0 the echo function also91+As an added benefit, starting with click 2.0, the echo function also92 has good support for ANSI colors. It will automatically strip ANSI codes93 if the output stream is a file and if colorama is supported, ANSI colors94 will also work on Windows. See :ref:`ansi-colors` for more information.95 23 unmodified lines119 120 cli.add_command(initdb)121 cli.add_command(dropdb)122 123-As you can see the :func:`group` decorator works like the :func:`command`124-decorator but creates a :class:`Group` object instead which can be given125-multiple subcommands that can be attached with126-:meth:`Group.add_command`.123+As you can see, the :func:`group` decorator works like the :func:`command`124+decorator, but creates a :class:`Group` object instead which can be given125+multiple subcommands that can be attached with :meth:`Group.add_command`.126 128-For simple scripts it's also possible to automatically attach and create a127+For simple scripts, it's also possible to automatically attach and create a128 command by using the :meth:`Group.command` decorator instead. The above129 script can be written like this then:130 131 .. click:example::12 unmodified lines144 145 Adding Parameters146 -----------------147 149-To add parameters the :func:`option` and :func:`argument` decorators:148+To add parameters, use the :func:`option` and :func:`argument` decorators:149 150 .. click:example::151 152 @click.command()11 unmodified linesmodified:docs/setuptools.rst+3 added-3 removed
17 unmodified lines18 Introduction19 ------------20 21 All you need to do to bundle your scripts with setuptools is your script22-in a python package and a ``setup.py`` file.22+in a Python package and a ``setup.py`` file.23 24-Imagine this layout::24+Imagine this directory structure::25 26 yourpackage/27 cli.py28 setup.py26 unmodified lines55 yourscript=yourpackage.cli:cli56 ''',57 )58 59-The magic is in the ``entry_points`` parameter. Below ``console_scripts``59+The magic is in the ``entry_points`` parameter. Below ``console_scripts``,60 each line identifies one console script. The first part before the equals61 sign (``=``) is the name of the script that should be generated, the second62 part is the import path followed by a colon (``:``) with the click63 command.18 unmodified linesmodified:docs/testing.rst+3 added-3 removed
1 unmodified line2 ==========================3 4 .. currentmodule:: click.testing5 6-For basic testing click provides the :mod:`click.testing` module which6+For basic testing, click provides the :mod:`click.testing` module which7 provides test functionality that helps you invoke command line8 applications and check their behavior.9 10 These tools really only should be used for testing as they are changing29 unmodified lines40 41 File System Isolation42 ---------------------43 44-For basic command line tools that want to operate with the file system the44+For basic command line tools that want to operate with the file system, the45 :meth:`CliRunner.isolated_filesystem` method comes in useful which sets up46 an empty folder and changes the current working directory to.47 48 Example::19 unmodified lines68 Input Streams69 -------------70 71 The test wrapper can also be used to provide input data for the input72-stream (stdin). This is very useful for testing prompts for instance::72+stream (stdin). This is very useful for testing prompts, for instance::73 74 import click75 from click.testing import CliRunner76 14 unmodified linesmodified:docs/utils.rst+23 added-23 removed
21 unmodified lines22 import click23 24 click.echo('Hello World!')25 26-Most importantly it can print both unicode and binary data, unlike the26+Most importantly, it can print both Unicode and binary data, unlike the27 builtin ``print`` function on Python 3 which cannot output any bytes. It28-will however emit a trailing newline by default which needs to be28+will, however, emit a trailing newline by default which needs to be29 supressed by passing ``nl=False``::30 31 click.echo(b'\xe2\x98\x83', nl=False)32 46 unmodified lines79 80 Pager Support81 -------------82 83-In some situations you might want to show long texts on the terminal and83+In some situations, you might want to show long texts on the terminal and84 let a user scroll through it. This can be achieved by using the85 :func:`echo_via_pager` function which works similar to the :func:`echo`86 function but always writes to stdout and, if possible, through a pager.87 13 unmodified lines101 .. versionadded:: 2.0102 103 To clear the terminal screen you can use the :func:`clear` function that104 is provided starting with Click 2.0. It does what the name suggests: it105-clears the entire visible screen in a platform agnostic way:105+clears the entire visible screen in a platform-agnostic way:106 107 ::108 109 import click5 unmodified lines115 116 .. versionadded:: 2.0117 118 Normally when reading input from the terminal you would use reading from119-standard input. However this is buffered input and will not show up until120-the line has been terminated. In certain circumstances you might not want119+standard input. However, this is buffered input and will not show up until120+the line has been terminated. In certain circumstances, you might not want121 to do that and instead read individual characters as they are being122 written.123 124 For this click provides the :func:`getchar` function which reads a single125-character from the terminal buffer and returns it as a unicode character.125+character from the terminal buffer and returns it as a Unicode character.126 127 Note that this function will always read from the terminal, even if stdin128 is instead a pipe.129 39 unmodified lines169 message = click.edit('\n\n' + MARKER)170 if message is not None:171 return message.split(MARKER, 1)[0].rstrip('\n')172 173-Alternatively the function can also be used to launch editors for files by174-a specific filename. In that case the return value is always `None`.173+Alternatively, the function can also be used to launch editors for files by174+a specific filename. In this case, the return value is always `None`.175 176 Example usage::177 178 import click5 unmodified lines184 185 .. versionadded:: 2.0186 187 Click supports launching applications through :func:`launch`. This can be188-used to open the default application assocated with a URL or file type.189-This for instance can be used to both launch web browsers as well as190-picture viewers etc. In addition to that it can also launch the file191-manager and automatically select the provided file.188+used to open the default application assocated with a URL or filetype.189+This for instance can be used to launch web browsers, picture viewers, etc.190+In addition to that, it can also launch the file manager and automatically191+select the provided file.192 193 Example usage::194 195 click.launch('http://click.pocoo.org/')2 unmodified lines198 199 Printing Filenames200 ------------------201 202-Because filenames might not be unicode, formatting them can be a bit202+Because filenames might not be Unicode, formatting them can be a bit203 tricky. Generally this is easier on Python 2 than on 3 as you can just204 write the bytes to stdout with the print function, but at least on Python205-3 you will need to always operate in unicode.205+3 you will need to always operate in Unicode.206 207 The way this works with click is through the :func:`format_filename`208 function. It does a best effort conversion of the filename to Unicode and209 will never fail. This makes it possible to use these filenames in the210-context of a full unicode string.210+context of a full Unicode string.211 212 Example::213 214 click.echo('Path: %s' % click.format_filename(b'foo.txt'))1 unmodified line216 217 Standard Streams218 ----------------219 220-For command line utilities it's very important to get access to input and220+For command line utilities, it's very important to get access to input and221 output streams reliably. Python generally provides access to these222 streams through ``sys.stdout`` and friends, but unfortunately there are223 API differences between 2.x and 3.x. Especially in regards to how these224-streams respond to unicode and binary data there are wide differences.224+streams respond to Unicode and binary data there are wide differences.225 226-Because of this click provides the :func:`get_binary_stream` and226+Because of this, click provides the :func:`get_binary_stream` and227 :func:`get_text_stream` which produce consistent results with different228 Python versions and for widely misconfigured terminals.229 230 The end result is that these functions will always return a functional12 unmodified lines243 ---------------------------244 245 .. versionadded:: 2.0246 247-Very often you want to open a config file that belongs to your248-application. However different operating systems store these config249-files by their policy in different places. Click provides a250-:func:`get_app_dir` function which returns the most appropriate location247+Very often you want to open a configuration file that belongs to your248+application. However, different operating systems store these configuration249+files in different locations depending on their standards. Click provides250+a :func:`get_app_dir` function which returns the most appropriate location251 for per-user config files for your application depending on the OS.252 253 Example usage::254 61 unmodified linesmodified:docs/why.rst+14 added-14 removed
1 Why Click?2 ==========3 4-There are so many libraries out there for writing command line utilities,4+There are so many libraries out there for writing command line utilities;5 why does click exist?6 7 This question is easy to answer: because there is not a single command8 line utility for Python out there which ticks the following boxes:9 unmodified lines18 There are many alternatives to click and you can have a look at them if19 you enjoy them better. The obvious ones are ``optparse`` and ``argparse``20 from the standard library.21 22-click is actually implemented as a wrapper around a mild fork of22+Click is actually implemented as a wrapper around a mild fork of23 ``optparse`` and does not implement any parsing itself. The reason it's24 not based on ``argparse`` is that ``argparse`` does not allow proper25 nesting of commands by design and has some deficiencies when it comes to26 POSIX compliant argument handling.23 unmodified lines50 with. The reason however click is not using argparse is that it has some51 problematic behaviors that make handling arbitrary command line interfaces52 hard:53 54-* argparse has builtin magic behavior to guess if something is an54+* argparse has built-in magic behavior to guess if something is an55 argument or an option. This becomes a problem when dealing with56 incomplete command lines as it's not possible to know without having a57 full understanding of the command line how the parser is going to58 behave. This goes against click's ambitions of dispatching to13 unmodified lines72 73 Docopt for instance acts by parsing your help pages and then parsing74 according to those rules. The side effect of this is that docopt is quite75 rigid in how it handles the command line interface. The upside of docopt76-is that it gives you strong control over your help page, the downside is77-that due to that it cannot rewrap your output for the current terminal76+is that it gives you strong control over your help page; the downside is77+that due to this it cannot rewrap your output for the current terminal78 width and it makes translations hard. On top of that docopt is restricted79 to basic parsing. It does not handle argument dispatching and callback80 invocation or types. This means there is a lot of code that needs to be81 written in addition to the basic help page to handle the parsing results.82 83-Most of all however it makes composability hard. While docopt does83+Most of all, however, it makes composability hard. While docopt does84 support dispatching to subcommands, it for instance does not directly85 support any kind of automatic subcommand enumeration based on what's86 available or it does not enforce subcommands to work in a consistent way.87 88 This is fine, but it's different from how click wants to work. Click aims89 to support fully composable command line user interfaces by doing this:90 91-- click does not just parse, it also dispatches to the appropriate code.92-- click has a strong concept of an invocation context that allows91+- Click does not just parse, it also dispatches to the appropriate code.92+- Click has a strong concept of an invocation context that allows93 subcommands to respond to data from the parent command.94-- click has strong information available for all parameters and commands94+- Click has strong information available for all parameters and commands95 so that it can generate unified help pages for the full CLI and to96 assist the user in converting the input data as necessary.97-- click has strong understanding of what types are and can give the user97+- Click has a strong understanding of what types are and can give the user98 consistent error messages if something goes wrong. A subcommand99 written by a different developer will not suddenly die with a100 different error messsage because it's manually handled.101-- click has enough meta information available for its whole program101+- Click has enough meta information available for its whole program102 that it can evolve over time to improve the user experience without103- forcing developers to adjust their programs. For instance if click103+ forcing developers to adjust their programs. For instance, if click104 decides to change how help pages are formatted, all click programs105 will automatically benefit from this.106 107 The aim of click is to make composable systems, whereas the aim of docopt13 unmodified lines121 it hard to achieve a consistent command line experience.122 123 The best example for this is optparse's ``callback`` functionality for124 accepting arbitrary number of arguments. Due to syntactical ambiguities125-on the command line there is no way to implement fully variadic arguments.125+on the command line, there is no way to implement fully variadic arguments.126 There are always tradeoffs that need to be made and in case of127 ``argparse`` these tradeoffs have been critical enough, that a system like128 click cannot even be implemented on top of it.129 9 unmodified lines139 even optparse and argparse support automatic expansion of long arguments.140 The reason for this is that it's a liability for backwards compatibility.141 If people start relying on automatically modified parameters and someone142 adds a new parameter in the future, the script might stop working. These143-kind of problems are hard to find so click does not attempt to be magical143+kinds of problems are hard to find so click does not attempt to be magical144 about this.145 146 This sort of behavior however can be implemented on a higher level to147 support things such as explicit aliases. For more information see1 unmodified line