Clean up parameters intro. (#2585)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Rowlando13 · committed by GitHub · parents 3ecc65b, 2d52254 · Browse files · 3 files·+52 added−55 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+52 added−55 removed
- other:+52 added−55 removed
Provenance
- Commit
9aeb586- Tree
cc38f09- Author
Rowlando13 <67291205+Rowlando13@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent 1
3ecc65b- Parent 2
2d52254
3 files changed+52 added−55 removed
modified:docs/arguments.rst+3 added
181 unmodified lines182 file in the same folder, and upon completion, the file will be moved over to183 the original location. This is useful if a file regularly read by other184 users is modified.185 186+187+.. _environment-variables:188+189 Environment Variables190 ---------------------191 192 Like options, arguments can also grab values from an environment variable.74 unmodified linesmodified:docs/documentation.rst+1 added-1 removed
34 unmodified lines35 36 .. _documenting-arguments:37 38 Documenting Arguments39-~~~~~~~~~~~~~~~~~~~~~39+----------------------40 41 :func:`click.argument` does not take a ``help`` parameter. This is to42 follow the general convention of Unix tools of using arguments for only43 the most necessary things, and to document them in the command help text224 unmodified linesmodified:docs/parameters.rst+48 added-54 removed
1 unmodified line2 ==========3 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 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.6+Click supports only two types of parameters for scripts (by design): options and arguments.7 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.8+Options9+----------------10 16-Differences17------------11+* Are optional.12+* Recommended to use for everything except subcommands, urls, or files.13+* Can take a fixed number of arguments. The default is 1. They may be specified multiple times using :ref:`multiple-options`.14+* Are fully documented by the help page.15+* Have automatic prompting for missing input.16+* Can act as flags (boolean or otherwise).17+* Can be pulled from environment variables.18 19-Arguments can do less than options. The following features are only20-available for options:19+Arguments20+----------------21 22-* automatic prompting for missing input23-* act as flags (boolean or otherwise)24-* option values can be pulled from environment variables, arguments can not25-* options are fully documented in the help page, arguments are not26- (:ref:`this is intentional <documenting-arguments>` as arguments27- might be too specific to be automatically documented)22+* Are optional with in reason, but not entirely so.23+* Recommended to use for subcommands, urls, or files.24+* Can take an arbitrary number of arguments.25+* Are not fully documented by the help page since they may be too specific to be automatically documented. For more see :ref:`documenting-arguments`.26+* Can be pulled from environment variables but only explicitly named ones. For more see :ref:`environment-variables`.27 29-On the other hand arguments, unlike options, can accept an arbitrary number30-of arguments. Options can strictly ever only accept a fixed number of31-arguments (defaults to 1), or they may be specified multiple times using32-:ref:`multiple-options`.28+.. _parameter_names:29 30+Parameter Names31+---------------32+33+Parameters (options and arguments) have a name that will be used as34+the Python argument name when calling the decorated function with35+values.36+37+.. click:example::38+39+ @click.command()40+ @click.argument('filename')41+ @click.option('-t', '--times', type=int)42+ def multi_echo(filename, times):43+ """Print value filename multiple times."""44+ for x in range(times):45+ click.echo(filename)46+47+In the above example the argument's name is ``filename``. The name must match the python arg name. To provide a different name for use in help text, see :ref:`doc-meta-variables`.48+The option's names are ``-t`` and ``--times``. More names are available for options and are covered in :ref:`options`.49+50+And what it looks like when run:51+52+.. click:run::53+54+ invoke(multi_echo, ['--times=3', 'index.txt'], prog_name='multi_echo')55+56 Parameter Types57 ---------------58 37-Parameters can be of different types. Types can be implemented with38-different behavior and some are supported out of the box:59+The supported parameter types are:60 61 ``str`` / :data:`click.STRING`:62 The default parameter type which indicates unicode strings.63 30 unmodified lines94 95 .. autoclass:: DateTime96 :noindex:97 77-Custom parameter types can be implemented by subclassing78-:class:`click.ParamType`. For simple cases, passing a Python function that79-fails with a `ValueError` is also supported, though discouraged.98+How to Implement Custom Types99+-------------------------------100 81-.. _parameter_names:101+To implement a custom type, you need to subclass the :class:`ParamType` class. For simple cases, passing a Python function that fails with a `ValueError` is also supported, though discouraged. Override the :meth:`~ParamType.convert` method to convert the value from a string to the correct type.102 83-Parameter Names84----------------85-86-Parameters (both options and arguments) have a name that will be used as87-the Python argument name when calling the decorated function with88-values.89-90-Arguments take only one positional name. To provide a different name for91-use in help text, see :ref:`doc-meta-variables`.92-93-Options can have many names that may be prefixed with one or two dashes.94-Names with one dash are parsed as short options, names with two are95-parsed as long options. If a name is not prefixed, it is used as the96-Python argument name and not parsed as an option name. Otherwise, the97-first name with a two dash prefix is used, or the first with a one dash98-prefix if there are none with two. The prefix is removed and dashes are99-converted to underscores to get the Python argument name.100-101-102-Implementing Custom Types103--------------------------104-105-To implement a custom type, you need to subclass the :class:`ParamType`106-class. Override the :meth:`~ParamType.convert` method to convert the107-value from a string to the correct type.108-103 The following code implements an integer type that accepts hex and octal104 numbers in addition to normal integers, and converts them into regular105 integers.106 31 unmodified lines