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 Armin Ronacher · parent f48761a · Browse files · 17 files·+193 added−193 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
17 files+193 added−193 removed
- other:+193 added−193 removed
Provenance
- Commit
67523af- Tree
c0e0636- Author
Kevin Yap <me@kevinyap.ca>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
f48761a
17 files changed+193 added−193 removed
modified:docs/advanced.rst+15 added-15 removed
21 unmodified lines22 functionality.23 24 As explained in :ref:`custom-multi-commands`, a multi command can provide25 two methods: :meth:`~MultiCommand.list_commands` and26-:meth:`~MultiCommand.get_command`. In this particular case you only need26+:meth:`~MultiCommand.get_command`. In this particular case, you only need27 to override the latter as you generally don't want to enumerate the28-aliases on the help page to avoid confusion.28+aliases on the help page in order to avoid confusion.29 30 This following example implements a subclass of :class:`Group` that31-accepts a prefix for a command. If there is a command called32-``push`` it would accept ``pus`` as an alias if it's unique:31+accepts a prefix for a command. If there were a command called32+``push``, it would accept ``pus`` as an alias if it was unique:33 34 .. click:example::35 36 class AliasedGroup(click.Group):29 unmodified lines66 Invoking Other Commands67 -----------------------68 69 Sometimes, it might be interesting to invoke one command from another70-command. This is a pattern that is generally discouraged with click but71-possible nonetheless. For this you can use the :func:`Context.invoke`70+command. This is a pattern that is generally discouraged with click, but71+possible nonetheless. For this, you can use the :func:`Context.invoke`72 or :func:`Context.forward` methods.73 74 They work similarly, but the difference is that :func:`Context.invoke` merely75 invokes another command with the arguments you provide as a caller,30 unmodified lines106 107 Callback Evaluation Order108 -------------------------109 110-Click works a bit different than some other command line parsers in that110+Click works a bit differently than some other command line parsers in that111 it attempts to reconcile the order of arguments as defined by the112 programmer with the order of arguments as defined by the user before113 invoking any callbacks.114 115-This is an important concept to understand when implementing complex116-patterns ported from optparse or other systems to click. A parameter117-callback invocation in optparse happens as part of the parsing step115+This is an important concept to understand when porting complex116+patterns to click from optparse or other systems. A parameter117+callback invocation in optparse happens as part of the parsing step,118 whereas a callback invocation in click happens after the parsing.119 120-The main difference is that in optparse callbacks are invoked with the raw120+The main difference is that in optparse, callbacks are invoked with the raw121 value as it happens, whereas a callback in click is invoked after the122 value has been fully converted.123 124 Generally, the order of invocation is driven by the order in which the user13 unmodified lines138 comes first on the command line will win and exit the program.139 140 Repeated parameters:141 If an option or argument is split up on the command line into multiple142- places because it's repeated (for instance ``--exclude foo --include143- baz --exclude bar``), the callback will fire based on the position of142+ places because it is repeated -- for instance, ``--exclude foo --include143+ baz --exclude bar`` -- the callback will fire based on the position of144 the first option. In this case, the callback will fire for145 ``exclude`` and it will be passed both options (``foo`` and146 ``bar``), then the callback for ``include`` will fire with ``baz``147 only.9 unmodified lines157 undefined values do not fire the callback. Missing parameters fire158 their callbacks at the very end which makes it possible for them to159 default to values from a parameter that came before.160 161-Most of the time you don't need to be concerned about any of this stuff,162-but it's important to know how it works for some advanced cases.161+Most of the time you do not need to be concerned about any of this,162+but it is important to know how it works for some advanced cases.modified:docs/arguments.rst+29 added-29 removed
1 unmodified line2 =========3 4 .. currentmodule:: click5 6-Arguments work similar to options but are positional. They also only6+Arguments work similarly to options, but are positional. They also only7 support a subset of the features of options due to their syntactical nature.8-Click also will not attempt to document arguments for you and wants you to9-document them manually to avoid ugly looking help pages.8+Click will also not attempt to document arguments for you and wants you to9+document them manually in order to avoid ugly help pages.10 11 Basic Arguments12 ---------------13 14 The most basic option is a simple string argument of one value. If no15-type is provided the type of the default value is used. If no default16-value is provided the type is assumed to be :data:`STRING`.15+type is provided, the type of the default value is used, and if no default16+value is provided, the type is assumed to be :data:`STRING`.17 18 Example:19 20 .. click:example::13 unmodified lines34 ------------------35 36 The second most common version is variadic arguments where a specific (or37 unlimited) number of arguments is accepted. This can be controlled with38-the ``nargs`` parameter. If it's set to ``-1`` then an unlimited number38+the ``nargs`` parameter. If it is set to ``-1``, then an unlimited number39 of arguments is accepted.40 41 The value is then passed as a tuple. Note that only one argument can be42-set to ``nargs=-1`` as this will eat up all arguments.42+set to ``nargs=-1``, as it will eat up all arguments.43 44 Example:45 46 .. click:example::12 unmodified lines59 invoke(copy, args=['foo.txt', 'bar.txt', 'my_folder'])60 61 Note that this is not how you would write this application. The reason62 for this is that in this particular example the arguments are defined as63-strings. File names however are not strings! They might be (on certain64-operating systems) but not necessarily on others. For better ways see the65-next sections.63+strings. Filenames, however, are not strings! They might be on certain64+operating systems, but not necessarily on all. For better ways to write65+this, see the next sections.66 67 .. admonition:: Note on Non-Empty Variadic Arguments68 69 If you come from ``argparse`` you might be missing support for setting12 unmodified lines82 --------------83 84 Since all the examples have already worked with filenames, it makes sense85 to explain how to deal with files properly. Command line tools are more86-fun if they work with files the Unix way which is to accept ``-`` as a86+fun if they work with files the Unix way, which is to accept ``-`` as a87 special file that refers to stdin/stdout.88 89 Click supports this through the :class:`click.File` type which90 intelligently handles files for you. It also deals with Unicode and bytes26 unmodified lines117 -------------------118 119 In the previous example the files were opened immediately. But what if120 we just want the filename? The naive way is to use the default string121-argument type. However remember that click is unicode based so the string122-will be a unicode value always. Filenames unfortunately however can be123-unicode or bytes depending on which operating system you're on. As such124-the type is insufficient.121+argument type. However, remember that click is Unicode-based, so the string122+will always be a Unicode value. Unfortunately, filenames can be Unicode or123+bytes depending on which operating system is being used. As such, the type124+is insufficient.125 126-Instead, you should be using the :class:`Path` type which handles this126+Instead, you should be using the :class:`Path` type, which handles this127 complexity for you. Not only will it return either bytes or Unicode128 depending on what makes more sense, but it will also be able to do some129 basic checks for you such as existence checks.130 20 unmodified lines151 152 File Opening Safety153 -------------------154 155-The :class:`FileType` type has one problem it needs to deal with and that155+The :class:`FileType` type has one problem it needs to deal with, and that156 is to decide when to open a file. The default behavior is to be157 "intelligent" about it. What this means is that it will open stdin/stdout158-and files opened for reading immediately. This will directly give the159-user feedback when a file cannot be opened, but it will only open files160-for writing the first time an IO operation is performed by wrapping the161-file automatically in a special wrapper.158+and files opened for reading immediately. This will give the user direct159+feedback when a file cannot be opened, but it will only open files160+for writing the first time an IO operation is performed by automatically161+wrapping the file in a special wrapper.162 163 This behavior can be forced by passing ``lazy=True`` or ``lazy=False`` to164-the constructor. If the file is opened lazily, it will fail on first IO164+the constructor. If the file is opened lazily, it will fail its first IO165 operation by raising an :exc:`FileError`.166 167 Since files opened for writing will typically immediately empty the file,168 the lazy mode should only be disabled if the developer is absolutely sure6 unmodified lines175 necessary for manually prompting with the :func:`prompt` function as you176 do not know if a stream like stdout was opened (which was already open177 before) or a real file that needs closing.178 179-Starting with click 2.0 it's also possible to open files in atomic mode by179+Starting with click 2.0, it is also possible to open files in atomic mode by180 passing ``atomic=True``. In atomic mode, all writes go into a separate181-file in the same folder and upon completion the file will be moved over to181+file in the same folder, and upon completion, the file will be moved over to182 the original location. This is useful if a file regularly read by other183 users is modified.184 185 Environment Variables186 ---------------------187 188-Like options, arguments can also get values from an environment variable.188+Like options, arguments can also grab values from an environment variable.189 Unlike options, however, this is only supported for explicitly named190 environment variables.191 192 Example usage:16 unmodified lines209 210 In that case, it can also be a list of different environment variables211 where the first one is picked.212 213-Generally this feature is not recommended because it can cause a lot of214-confusion to the user.213+Generally, this feature is not recommended because it can cause the user214+a lot of confusion.215 216-Arguments-Like Options216+Argument-Like Options217 ------------------------------218 219-Sometimes you want to process arguments that look like options. For219+Sometimes, you want to process arguments that look like options. For220 instance, imagine you have a file named ``-foo.txt``. If you pass this as221 an argument in this manner, click will treat it as an option.222 223 To solve this, click does what any POSIX style command line script does,19 unmodified linesmodified:docs/bashcomplete.rst+10 added-10 removed
8 unmodified lines9 10 Limitations11 -----------12 13-Bash completion is only available if a script has been installed properly13+Bash completion is only available if a script has been installed properly,14 and not executed through the ``python`` command. For information about15-how to do that see :ref:`setuptools-integration`. Also, click currently16-only supports completion for bash.15+how to do that, see :ref:`setuptools-integration`. Also, click currently16+only supports completion for Bash.17 18-Currently, bash completion is an internal feature that is not customizable.18+Currently, Bash completion is an internal feature that is not customizable.19 This might be relaxed in future versions.20 21 What it Completes22 -----------------23 24-Generally, the bash completion support will complete subcommands and24+Generally, the Bash completion support will complete subcommands and25 parameters. Subcommands are always listed whereas parameters only if at26 least a dash has been provided. Example::27 28 $ repo <TAB><TAB>3 unmodified lines32 33 Activation34 ----------35 36-In order to activate Bash completion you need to inform Bash that37-completion is available for your script and how. Any click application36+In order to activate Bash completion, you need to inform Bash that37+completion is available for your script, and how. Any click application38 automatically provides support for that. The general way this works is39 through a magic environment variable called ``_<PROG_NAME>_COMPLETE``,40 where ``<PROG_NAME>`` is your application executable name in uppercase41 with dashes replaced by underscores.6 unmodified lines48 is what you would need to put into your ``.bashrc``::49 50 eval "$(_FOO_BAR_COMPLETE=source foo-bar)"51 52-From this point onwards your script will have Bash completion enabled.52+From this point onwards, your script will have Bash completion enabled.53 54 Activation Script55 -----------------56 57 The above activation example will always invoke your application on58 startup. This might be slowing down the shell activation time59-significantly if you have many applications. Alternatively you could also60-ship a file with the contents of that which is what Git and other systems59+significantly if you have many applications. Alternatively, you could also60+ship a file with the contents of that, which is what Git and other systems61 are doing.62 63 This can be easily accomplished::64 5 unmodified linesmodified:docs/commands.rst+7 added-7 removed
9 unmodified lines10 Callback Invocation11 -------------------12 13 For a regular command, the callback is executed whenever the command runs.14-If the script is the only command it will always fire (unless a14+If the script is the only command, it will always fire (unless a15 parameter callback prevents it). This for instance happens if someone16 passes ``--help`` to the script).17 18 For groups and multi commands, the situation looks different. In this case,94 unmodified lines113 return update_wrapper(new_func, f)114 115 The :meth:`Context.invoke` command will automatically invoke the function116 in the correct way, so the function will either be called with ``f(ctx,117-obj)`` or ``f(obj)`` depending on if it itself is decorated with117+obj)`` or ``f(obj)`` depending on whether or not it itself is decorated with118 :func:`with_context`.119 120 This is a very powerful context that can be used to build very complex121-nested applications. See :ref:`complex-guide` for more information.121+nested applications; see :ref:`complex-guide` for more information.122 123 124 Group Invocation Without Command125 --------------------------------84 unmodified lines210 ----------------------211 212 In addition to implementing custom multi commands, it can also be213 interesting to merge multiple together into one script. While this is214-generally not as recommended as nesting one below the other, the merging214+generally not as recommended as it nests one below the other, the merging215 approach can be useful in some circumstances for a nicer shell experience.216 217-A default implementation for such a merging system is the217+The default implementation for such a merging system is the218 :class:`CommandCollection` class. It accepts a list of other multi219 commands and makes the commands available on the same level.220 221 Example usage:43 unmodified lines265 266 This is useful if you plug in some commands from another package but267 you're not satisfied with the defaults.268 269-The default map can be nested arbitrarily for each subcommand and be269+The default map can be nested arbitrarily for each subcommand and270 provided when the script is invoked. Alternatively, it can also be271-overriden at any point by commands. For instance a top-level command could271+overriden at any point by commands. For instance, a top-level command could272 load the defaults from a configuration file.273 274 Example usage:275 27 unmodified linesmodified:docs/complex.rst+17 added-18 removed
5 unmodified lines6 .. currentmodule:: click7 8 Click is designed to assist with the creation of complex and simple CLI tools9 alike. However, the power of its design is the ability to arbitrarily nest10-systems together. For instance, in case you have ever used Django, you will11-have realized that it provides a command line utility, but so does Celery (for12-instance). When using Celery with Django there are two tools that need13-to interact with each other and be cross configured.10+systems together. For instance, if you have ever used Django, you will11+have realized that it provides a command line utility, but so does Celery.12+When using Celery with Django, there are two tools that need to interact with13+each other and be cross-configured.14 15-In a theoretical world of two separate click command line utilities they16-can solve this problem by nesting one inside the other. For instance, the17-web framework could also load the commands for the message queue18-framework.15+In a theoretical world of two separate click command line utilities, they16+could solve this problem by nesting one inside the other. For instance, the17+web framework could also load the commands for the message queue framework.18 19 Basic Concepts20 --------------21 23-To understand how this works you need to understand two concepts: contexts22+To understand how this works, you need to understand two concepts: contexts23 and the calling convention.24 25 Contexts26 ````````3 unmodified lines30 parameters, what command created it, which resources need to be cleaned up31 at the end of the function, and so forth. It can also optionally hold an32 application-defined object.33 35-Context objects build a linked list until they hit top one. Each context34+Context objects build a linked list until they hit the top one. Each context35 is linked to the parent context. This allows a command to work below36 another command and store its own information there without having to be38-afraid of messing up the state of the parent command.37+afraid of altering up the state of the parent command.38 39 Because the parent data is available, however, it is possible to navigate to40 it if needed.41 43-Most of the time you don't see the context object, but when writing more42+Most of the time, you do not see the context object, but when writing more43 complex applications it comes in handy. This brings us to the next point.44 45 Calling Convention46 ``````````````````11 unmodified lines58 59 Building a Git Clone60 --------------------61 63-In this example we want to build a command line tool that resembles a62+In this example, we want to build a command line tool that resembles a63 version control system. Systems like Git usually provide one64 over-arching command that already accepts some parameters and65 configuration, and then have extra subcommands that do other things.66 3 unmodified lines70 At the top level, we need a group that can hold all our commands. In this71 case, we use the basic :func:`click.group` which allows us to register72 other click commands below it.73 75-For this command we also want to accept some parameters that configure the74+For this command, we also want to accept some parameters that configure the75 state of our tool:76 77 .. click:example::78 16 unmodified lines95 ctx.obj = Repo(repo_home, debug)96 97 98 So let's understand what this does. We create a group command which can100-have subcommands. When it is invoked it will create an instance of a99+have subcommands. When it is invoked, it will create an instance of a100 ``Repo`` class. This holds the state for our command line tool. In this102-case it just remembers some parameters, but at this point it could also101+case, it just remembers some parameters, but at this point it could also102 start loading configuration files and so on.103 104 This state object is then remembered as :attr:`~Context.obj` on the105 context. This is a special attribute where commands are supposed to36 unmodified lines142 ````````````````````143 144 While not relevant for the particular program we want to build, there is145 also quite good support for interleaving systems. Imagine for instance that147-there would be this super cool plugin for our version control system that148-needs a lot of configuration and wants to store its own configuration as146+there was a super cool plugin for our version control system that needed a147+lot of configuration and wanted to store its own configuration as148 :attr:`~Context.obj`. If we would then attach another command below that,149 we would all of a sudden get the plugin configuration instead of our repo150 object.151 68 unmodified linesmodified:docs/documentation.rst+2 added-2 removed
27 unmodified lines28 .. click:run::29 30 invoke(hello, args=['--help'])31 32-Arguments cannot be documented this way. This is to follow general33-convention of Unix tools using arguments for only the most necessary32+Arguments cannot be documented this way. This is to follow the general33+convention of Unix tools of 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 Rewrapping91 unmodified linesmodified:docs/index.rst+1 added-1 removed
1 Welcome to the Click Documentation2 ==================================3 4-click is a Python package for creating beautiful command line interfaces4+Click is a Python package for creating beautiful command line interfaces5 in a composable way with as little amount of code as necessary. It's the6 "Command Line Interface Creation Kit". It's highly configurable but comes7 with good defaults out of the box.8 88 unmodified linesmodified:docs/options.rst+27 added-27 removed
10 unmodified lines11 -------------------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 default15-value is used. If no default value is provided the type is assumed to be15+value is used. If no default value is provided, the type is assumed to be16 :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::34 unmodified lines54 55 Multiple Options56 ----------------57 58-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:58+Similarly to ``nargs``, there is also the case of wanting to support a59+parameter being provided multiple times to and have all values recorded --60+not just the last one. For instance, ``git commit -m foo -m bar`` would61+record two lines for the commit message: ``foo`` and ``bar``. This can be62+accomplished with the ``multiple`` flag:63 64 Example:65 66 .. click:example::11 unmodified lines78 79 Counting80 --------81 82-In some very rare circumstances, it's interesting to use the repetition82+In some very rare circumstances, it is 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::60 unmodified lines147 .. click:run::148 149 invoke(info, args=['--shout'])150 151-Note that if a slash is contained in your option already (for instance if152-you use Windows-style parameters where ``/`` is the prefix character you153-can alternatively split the parameters through ``;`` instead):151+Note that if a slash is contained in your option already (for instance, if152+you use Windows-style parameters where ``/`` is the prefix character), you153+can alternatively split the parameters through ``;`` instead:154 155 .. click:example::156 157 @click.command()38 unmodified lines196 197 Choice Options198 --------------199 200-Sometimes you want to have a parameter be a choice of a list of values.200+Sometimes, you want to have a parameter be a choice of a list of values.201 In that case you can use :class:`Choice` type. It can be instantiated202 with a list of valid values.203 204 Example:19 unmodified lines224 225 Prompting226 ---------227 228-Sometimes, you want parameters that can be provided from the command line,229-but if not, ask for user input. This can be implemented with click by230-defining a prompt string.228+In some cases, you want parameters that can be provided from the command line,229+but if not provided, ask for user input instead. This can be implemented with230+click by defining a prompt string.231 232 Example:233 234 .. click:example::58 unmodified lines293 294 Callbacks and Eager Options295 ---------------------------296 297-Sometimes you want a parameter to completely change the execution flow.297+Sometimes, you want a parameter to completely change the execution flow.298 For instance, this is the case when you want to have a ``--version``299 parameter that prints out the version and then exits the application.300 301-In such cases you need two concepts: eager parameters and a callback. An302-eager parameter is a parameter that is handled before others and a301+In such cases, you need two concepts: eager parameters and a callback. An302+eager parameter is a parameter that is handled before others, and a303 callback is what executes after the parameter is handled. The eagerness304 is necessary so that an earlier required parameter does not produce an305-error message. For instance if ``--version`` was not eager and a305+error message. For instance, if ``--version`` was not eager and a306 parameter ``--foo`` was required and defined before, you would need to307-specify it for ``--version`` to work. For more information see307+specify it for ``--version`` to work. For more information, see308 :ref:`callback-evaluation-order`.309 310 A callback is a function that is invoked with two parameters: the current311 :class:`Context` and the value. The context provides some useful features15 unmodified lines327 expose_value=False, is_eager=True)328 def hello():329 click.echo('Hello World!')330 331-The `expose_value` parameter prevents the now pretty pointless ``version``332-parameter to be passed to the callback. If that was not specified, a331+The `expose_value` parameter prevents the pretty pointless ``version``332+parameter from being passed to the callback. If that was not specified, a333 boolean would be passed to the `hello` script. The `resilient_parsing`334 flag is set on the context if click wants to parse the command line335 without and destructive behavior that would change the execution flow. In336 this case, because we would exit the program, we instead do nothing.153 unmodified lines490 491 Other Prefix Characters492 -----------------------493 494-Click can deal with alternative prefix characters, other than ``-`` for494+Click can deal with alternative prefix characters other than ``-`` for495 options. This is for instance useful if you want to handle slashes as496 parameters ``/`` or something similar. Note that this is strongly497 discouraged in general because click wants developers to stay close to498 POSIX semantics. However in certain situations this can be useful:32 unmodified lines531 532 Range Options533 -------------534 535-A special mention should go to the :class:`IntRange` type which works very536-similar to the :data:`INT` type but restricts the value to fall into a535+A special mention should go to the :class:`IntRange` type, which works very536+similarly to the :data:`INT` type, but restricts the value to fall into a537 specific range (inclusive on both edges). It has two modes:538 539 - the default mode (non-clamping mode) where a value that falls outside540 of the range will cause an error.29 unmodified lines570 ------------------------571 572 .. versionchanged:: 2.0573 574-If you want to apply custom validation logic you can do this in the574+If you want to apply custom validation logic, you can do this in the575 parameter callbacks. These callbacks can both modify values as well as576 raise errors if the validation does not work.577 578-In click 1.0 you can only raise the :exc:`UsageError` but starting with579-click 2.0 you can also raise the :exc:`BadParameter` error which has the578+In click 1.0, you can only raise the :exc:`UsageError` but starting with579+click 2.0, you can also raise the :exc:`BadParameter` error, which has the580 added advantage that it will automatically format the error message to581 also contain the parameter name.582 583 Example:25 unmodified linesmodified:docs/parameters.rst+13 added-13 removed
2 unmodified lines3 4 .. currentmodule:: click5 6 Click supports two types of parameters for scripts: options and arguments.7-There is generally some confusion among authors of command line scripts8-when to use which so here is a quick overview of the differences. As the9-name indicates an option is optional. While arguments can be optional10-within reason they are much more restricted in how they can be optional.7+There is generally some confusion among authors of command line scripts of8+when to use which, so here is a quick overview of the differences. As its9+name indicates, an option is optional. While arguments can be optional10+within reason, they are much more restricted in how optional they can be.11 12 To help you decide between options and arguments, the recommendation is13 to use arguments exclusively for things like going to subcommands or input14-filenames / URLs and have everything else be an option instead.14+filenames / URLs, and have everything else be an option instead.15 16 Differences17 -----------18 56 unmodified lines75 ---------------76 77 Parameters (both options and arguments) accept a number of positional78 arguments which are the parameter declarations. Each string with a79-single dash is added as short argument, each string starting with a double80-dash as long one. If a string is added without any dashes it's the79+single dash is added as short argument; each string starting with a double80+dash as long one. If a string is added without any dashes, it becomes the81 internal parameter name which is also used as variable name.82 83-If a parameter is not given a name without dashes a name is generated83+If a parameter is not given a name without dashes, a name is generated84 automatically by taking the longest argument and converting all dashes to85-underscores. So for an option with ``('-f', '--foo-bar')`` the parameter86-name is `foo_bar`. For an option with ``('-x',)`` the parameter is `x`.87-For an option with ``('-f', '--filename', 'dest')`` the parameter is85+underscores. For an option with ``('-f', '--foo-bar')``, the parameter86+name is `foo_bar`. For an option with ``('-x',)``, the parameter is `x`.87+For an option with ``('-f', '--filename', 'dest')``, the parameter is88 called `dest`.89 90 Implementing Custom Types91 -------------------------92 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 object94+class. Types can be invoked with or without context and parameter object,95 which is why they need to be able to deal with this.96 97 The following code implements an integer type that accepts hex and octal98-numbers in addition to normal integers and converts them into regular98+numbers in addition to normal integers, and converts them into regular99 integers::100 101 import click102 18 unmodified linesmodified:docs/prompts.rst+4 added-4 removed
1 unmodified line2 ==================3 4 .. currentmodule:: click5 6-Click supports prompts in two different places. One is automated prompts7-when the parameter handling happens. The second option is to ask for6+Click supports prompts in two different places. The first is automated7+prompts when the parameter handling happens, and the second is to ask for8 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 asks10+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).13 14 Option Prompts15 --------------32 unmodified linesmodified:docs/python3.rst+9 added-9 removed
14 unmodified lines15 16 Python 3 Limitations17 --------------------18 19-At the moment click suffers from a few problems on Python 3:19+At the moment, click suffers from a few problems with Python 3:20 21-* The command line in Unix traditionally is in bytes and not unicode.21+* The command line in Unix traditionally is in bytes, and not Unicode.22 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 2 unmodified lines28 surrogate escapes. This will not be fixed in click itself!29 30 For more information see :ref:`python3-surrogates`.31 32-* Standard input and output on Python 3 is opened in unicode mode by32+* Standard input and output on Python 3 is opened in Unicode mode by33 default. Click has to reopen the stream in binary mode in certain34 situations. Because there is no standardized way to do this, this35 might not always work. Primarily this can become a problem when36 testing command-line applications.26 unmodified lines63 * ``sys.stdin``, ``sys.stdout``, and ``sys.stderr`` are opened in binary64 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.67-* ``sys.argv`` is always bytes-based. Click will pass bytes to all67+* ``sys.argv`` is always byte-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 APIs6 unmodified lines78 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.82+ 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 string86 will contain error bytes encoded as surrogate escapes.8 unmodified lines95 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 Unicode100-handling itself which means there are differences in error behavior.99+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",103-but requires extra care on Python 3. The reason for this is that on103+while on Python 3, it requires extra care. The reason for this is that on104 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 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 know41 unmodified linesmodified:docs/quickstart.rst+1 added-1 removed
124 unmodified lines125 multiple subcommands that can be attached with :meth:`Group.add_command`.126 127 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:129+script can instead be written like this:130 131 .. click:example::132 133 @click.group()30 unmodified linesmodified:docs/setuptools.rst+4 added-4 removed
1 unmodified line2 3 Usage with Setuptools4 =====================5 6-When writing command line utilities it's recommended to write them as6+When writing command line utilities, it's recommended to write them as7 modules that are distributed with setuptools instead of using UNIX8-shebangs. There are a bunch of reasons for this.8+shebangs. There are many reasons for this.9 10 The first one is that setuptools automatically generates executable11 wrappers for Windows so your command line utilities work on Windows too.12 4 unmodified lines17 18 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.21+To bundle your script with setuptools, all you need is the script in a Python22+package and a ``setup.py`` file.23 24 Imagine this directory structure::25 26 yourpackage/55 unmodified linesmodified:docs/testing.rst+5 added-5 removed
5 unmodified lines6 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 changing11-the whole interpreter state for simplicity and they are not in any way12-thread safe!10+These tools should really only be used for testing as they change11+the entire interpreter state for simplicity and are not in any way12+thread-safe!13 14 Basic Testing15 -------------16 1 unmodified line18 :class:`CliRunner` which can invoke commands as command line scripts. The19 :meth:`CliRunner.invoke` method runs the command line script in isolation20 and captures the output as both bytes and binary data.21 22-The return value is a :class:`Result` object which has the captured output23-data, exit code and optional exception attached.22+The return value is a :class:`Result` object, which has the captured output23+data, exit code, and optional exception attached.24 25 Example::26 27 import click63 unmodified linesmodified:docs/upgrading.rst+4 added-4 removed
10 unmodified lines11 Upgrading to 2.012 ----------------13 14 Click 2.0 has one breaking change which is the signature for parameter15-callbacks. Before 2.0 the callback was invoked with ``(ctx, value)``15+callbacks. Before 2.0, the callback was invoked with ``(ctx, value)``16 whereas now it's ``(ctx, param, value)``. This change was necessary as it17 otherwise made reusing callbacks too complicated.18 19 To ease the transition click will still accept old callbacks. Starting20 with click 3.0 it will start to issue a warning to stderr to encourage you21 to upgrade.22 23-In case you want to support both click 1.0 and click 2.0 you can make a23+In case you want to support both click 1.0 and click 2.0, you can make a24 simple decorator that adjusts the signatures::25 26 import click27 from functools import update_wrapper10 unmodified lines38 @compatcallback39 def callback(ctx, param, value):40 return value.upper()41 42-Note that because click 1.0 did not pass a parameter the `param` argument43-here would be `None` so a compatibility callback could not use that42+Note that because click 1.0 did not pass a parameter, the `param` argument43+here would be `None`, so a compatibility callback could not use that44 argument.modified:docs/utils.rst+41 added-41 removed
9 unmodified lines10 11 Printing to Stdout12 ------------------13 14-The most obvious helper is the :func:`echo` function which in many ways14+The most obvious helper is the :func:`echo` function, which in many ways15 works like the Python print statement or function. The main difference is16-that it works the same on Python 2 and 3 and it intelligently detects17-misconfigured output streams and will never fail (except on Python 3, for16+that it works the same on Python 2 and 3, it intelligently detects17+misconfigured output streams, and will never fail (except on Python 3; for18 more information see :ref:`python3-limitations`).19 20 Example::21 1 unmodified line23 24 click.echo('Hello World!')25 26 Most importantly, it can print both Unicode and binary data, unlike the27-builtin ``print`` function on Python 3 which cannot output any bytes. It27+builtin ``print`` function on Python 3, which cannot output any bytes. It28 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)5 unmodified lines37 -----------38 39 .. versionadded:: 2.040 41-Starting with click 2.0 the :func:`echo` function gained extra42-functionality to deal with ANSI colors and styles. Note that on Windows41+Starting with click 2.0, the :func:`echo` function gained extra42+functionality to deal with ANSI colors and styles. Note that on Windows,43 this functionality is only available if `colorama`_ is installed. If it44-is installed then ANSI codes are intelligently handled.44+is installed, then ANSI codes are intelligently handled.45 46 Primarily this means that:47 48 - click's echo function will automatically strip ANSI color codes if the2 unmodified lines51 Windows and translate ANSI codes to terminal API calls. This means52 that colors will work on Windows the same way they do on other53 operating systems.54 55-Note for colorama support: click will automatically detect when `colorama`55+Note for `colorama` support: click will automatically detect when `colorama`56 is available and use it. Do *not* call ``colorama.init()``!57 58-To install colorama run this command::58+To install `colorama`, run this command::59 60 $ pip install colorama61 62 For styling a string the :func:`style` function can be used::18 unmodified lines81 -------------82 83 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.85+:func:`echo_via_pager` function which works similarly to the :func:`echo`86+function, but always writes to stdout and, if possible, through a pager.87 88 Example:89 90 .. click:example::8 unmodified lines99 ---------------100 101 .. 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: it103+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:106 107 ::108 5 unmodified lines114 --------------------------------115 116 .. versionadded:: 2.0117 118-Normally when reading input from the terminal you would use reading from118+Normally, when reading input from the terminal, you would read 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 want121-to do that and instead read individual characters as they are being122-written.121+to do that and instead read individual characters as they are being written.122 124-For this click provides the :func:`getchar` function which reads a single123+For thi,s click provides the :func:`getchar` function which reads a single124 character from the terminal buffer and returns it as a Unicode character.125 126 Note that this function will always read from the terminal, even if stdin127 is instead a pipe.11 unmodified lines139 click.echo('Abort!')140 else:141 click.echo('Invalid input :(')142 144-Note that this reads raw input which means that things like arrow keys143+Note that this reads raw input, which means that things like arrow keys144 will show up in the platform's native escape format. The only characters145 translated are ``^C`` and ``^D`` which are converted into keyboard146 interrupt and end of file exceptions. This is done because otherwise it's147 too easy to forget about that and to create scripts that cannot be4 unmodified lines152 ---------------------153 154 .. versionadded:: 2.0155 157-Sometimes it's useful to pause until the user presses any key on the158-keyboard. This is especially useful on Windows where ``cmd.exe`` by159-default will close the window at the end of the command execution instead156+Sometimes, it's useful to pause until the user presses any key on the157+keyboard. This is especially useful on Windows where ``cmd.exe`` will158+close the window at the end of the command execution by default, instead159 of waiting.160 162-In click this can be accomplished by the :func:`pause` function. This161+In click, this can be accomplished with the :func:`pause` function. This162 function will print a quick message to the terminal (which can be164-customized) and wait for the user to press a key. In addition to that165-it will also become a noop if the script is not run interactively.163+customized) and wait for the user to press a key. In addition to that,164+it will also become a NOP (no operation instruction) if the script is not165+run interactively.166 167 Example::168 169 import click7 unmodified lines177 178 Click supports launching editors automatically through :func:`edit`. This179 is very useful for asking users for multi-line input. It will180 automatically open the user's defined editor or fall back to a sensible181-default. If the user closes the editor without saving the return value181+default. If the user closes the editor without saving, the return value182 will be `None` otherwise the entered text.183 184 Example usage::185 20 unmodified lines206 .. versionadded:: 2.0207 208 Click supports launching applications through :func:`launch`. This can be209 used to open the default application assocated with a URL or filetype.210-This for instance can be used to launch web browsers, picture viewers, etc.211-In addition to that, it can also launch the file manager and automatically210+This can be used to launch web browsers or picture viewers, for instance.211+In addition to this, it can also launch the file manager and automatically212 select the provided file.213 214 Example usage::215 4 unmodified lines220 Printing Filenames221 ------------------222 223 Because filenames might not be Unicode, formatting them can be a bit224-tricky. Generally this is easier on Python 2 than on 3 as you can just225-write the bytes to stdout with the print function, but at least on Python226-3 you will need to always operate in Unicode.224+tricky. Generally, this is easier on Python 2 than on 3, as you can just225+write the bytes to stdout with the print function, but on Python 3, you will226+always need to operate in Unicode.227 228 The way this works with click is through the :func:`format_filename`229-function. It does a best effort conversion of the filename to Unicode and229+function. It does a best-effort conversion of the filename to Unicode and230 will never fail. This makes it possible to use these filenames in the231 context of a full Unicode string.232 233 Example::5 unmodified lines239 ----------------240 241 For command line utilities, it's very important to get access to input and242 output streams reliably. Python generally provides access to these243-streams through ``sys.stdout`` and friends, but unfortunately there are244-API differences between 2.x and 3.x. Especially in regards to how these245-streams respond to Unicode and binary data there are wide differences.243+streams through ``sys.stdout`` and friends, but unfortunately, there are244+API differences between 2.x and 3.x, especially with regards to how these245+streams respond to Unicode and binary data.246 247 Because of this, click provides the :func:`get_binary_stream` and248-:func:`get_text_stream` which produce consistent results with different249-Python versions and for widely misconfigured terminals.248+:func:`get_text_stream` functions, which produce consistent results with249+different Python versions and for a wide variety pf terminal configurations.250 251 The end result is that these functions will always return a functional252-stream object (except in very odd cases on Python 3, see252+stream object (except in very odd cases on Python 3; see253 :ref:`python3-limitations`).254 255 Example::256 7 unmodified lines264 ---------------------------265 266 .. versionadded:: 2.0267 268-Very often you want to open a configuration file that belongs to your268+Very often, you want to open a configuration file that belongs to your269 application. However, different operating systems store these configuration270 files in different locations depending on their standards. Click provides271 a :func:`get_app_dir` function which returns the most appropriate location272 for per-user config files for your application depending on the OS.21 unmodified lines294 ---------------------295 296 .. versionadded:: 2.0297 298-Sometimes you have command line scripts that need to process a lot of data298+Sometimes, you have command line scripts that need to process a lot of data,299 but you want to quickly show the user some progress about how long that300 will take. Click supports simple progress bar rendering for that through301 the :func:`progressbar` function.302 3 unmodified lines306 307 for user in all_the_users_to_process:308 modify_the_user(user)309 310-To hook this up with an automatically updating progress bar all you need310+To hook this up with an automatically updating progress bar, all you need311 to do is to change the code to this::312 313 import click314 2 unmodified lines317 modify_the_user(user)318 319 Click will then automatically print a progress bar to the terminal and320 calculate the remaining time for you. The calculation of remaining time321-requires that the iterable has a length. If it does not have a length,321+requires that the iterable has a length. If it does not have a length322 but you know the length, you can explicitly provide it::323 324 with click.progressbar(all_the_users_to_process,325 length=number_of_users) as bar:11 unmodified linesmodified:docs/why.rst+4 added-3 removed
87 unmodified lines88 support any kind of automatic subcommand enumeration based on what's89 available or it does not enforce subcommands to work in a consistent way.90 91 This is fine, but it's different from how click wants to work. Click aims92-to support fully composable command line user interfaces by doing this:92+to support fully composable command line user interfaces by doing the93+following:94 95 - Click does not just parse, it also dispatches to the appropriate code.96 - Click has a strong concept of an invocation context that allows97 subcommands to respond to data from the parent command.11 unmodified lines109 will automatically benefit from this.110 111 The aim of click is to make composable systems, whereas the aim of docopt112 is to build the most beautiful and hand-crafted command line interfaces.112-These two goals are conflicting with each other in subtle ways. Click113+These two goals conflict with one another in subtle ways. Click114 actively prevents people from implementing certain patterns in order to115 achieve unified command line interfaces. You have very little input on116 reformatting your help pages for instance.117 12 unmodified lines130 There are always tradeoffs that need to be made and in case of131 ``argparse`` these tradeoffs have been critical enough, that a system like132 click cannot even be implemented on top of it.133 133-In this particular case click attempts to stay with a handful of accepted134+In this particular case, click attempts to stay with a handful of accepted135 paradigms for building command line interfaces that can be well documented136 and tested.137 138 14 unmodified lines