Skip to content
Myr

Clean up parameters intro.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by unknown · committed by unknown · parent ce6e066 · Browse files · 2 files+51 added−54 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+51 added−54 removed

  • other:+51 added−54 removed

2 files+51 added−54 removed

modified:docs/arguments.rst+3 added
181 unmodified lines
182file in the same folder, and upon completion, the file will be moved over to183the original location. This is useful if a file regularly read by other184users is modified.185186187.. _environment-variables:188189Environment Variables190---------------------191192Like options, arguments can also grab values from an environment variable.
74 unmodified lines
modified:docs/parameters.rst+48 added-54 removed
1 unmodified line
2==========34.. currentmodule:: click56Click supports two types of parameters for scripts: options and arguments.7There is generally some confusion among authors of command line scripts of8when to use which, so here is a quick overview of the differences. As its9name indicates, an option is optional. While arguments can be optional10within reason, they are much more restricted in how optional they can be.6Click supports only two types of parameters for scripts (by design): options and arguments.712To help you decide between options and arguments, the recommendation is13to use arguments exclusively for things like going to subcommands or input14filenames / URLs, and have everything else be an option instead.8Options9----------------1016Differences17-----------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.1819Arguments can do less than options. The following features are only20available for options:19Arguments20----------------2122* 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. How to document args is covered in :ref:`documenting args section <documenting-arguments>`.26* Can be pulled from environment variables but only explicitly named ones. See :ref:`environment-variables` for further explanation.2729On the other hand arguments, unlike options, can accept an arbitrary number30of arguments. Options can strictly ever only accept a fixed number of31arguments (defaults to 1), or they may be specified multiple times using32:ref:`multiple-options`.28.. _parameter_names:2930Parameter Names31---------------3233Parameters (both options and arguments) have a name that will be used as34the Python argument name when calling the decorated function with35values.3637.. click:example::3839 @click.command()40 @click.argument('filename')41 @click.option('-t', '--times', type=int)42 def multi_echo(filename, times):43 """Print value of SRC environment variable."""44 for x in range(times):45 click.echo(filename)4647In 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`.48The option's names are ``-t`` and ``--times``. More names are available for options and are covered in :ref:`options`.4950And what it looks like when run:5152.. click:run::5354 invoke(multi_echo, ['--times=3', 'index.txt'], prog_name='multi_echo')5556Parameter Types57---------------5837Parameters can be of different types. Types can be implemented with38different behavior and some are supported out of the box:59The supported parameter types are:6061``str`` / :data:`click.STRING`:62 The default parameter type which indicates unicode strings.63
30 unmodified lines
9495.. autoclass:: DateTime96 :noindex:9777Custom parameter types can be implemented by subclassing78:class:`click.ParamType`. For simple cases, passing a Python function that79fails with a `ValueError` is also supported, though discouraged.98How to Implement Custom Types99-------------------------------10081.. _parameter_names:101To 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.10283Parameter Names84---------------8586Parameters (both options and arguments) have a name that will be used as87the Python argument name when calling the decorated function with88values.8990Arguments take only one positional name. To provide a different name for91use in help text, see :ref:`doc-meta-variables`.9293Options can have many names that may be prefixed with one or two dashes.94Names with one dash are parsed as short options, names with two are95parsed as long options. If a name is not prefixed, it is used as the96Python argument name and not parsed as an option name. Otherwise, the97first name with a two dash prefix is used, or the first with a one dash98prefix if there are none with two. The prefix is removed and dashes are99converted to underscores to get the Python argument name.100101102Implementing Custom Types103-------------------------104105To implement a custom type, you need to subclass the :class:`ParamType`106class. Override the :meth:`~ParamType.convert` method to convert the107value from a string to the correct type.108103The following code implements an integer type that accepts hex and octal104numbers in addition to normal integers, and converts them into regular105integers.106
31 unmodified lines