2014-10-16 20:40:34 +02:00
|
|
|
Parameters
|
|
|
|
==========
|
|
|
|
|
|
|
|
.. currentmodule:: click
|
|
|
|
|
|
|
|
Click supports two types of parameters for scripts: options and arguments.
|
|
|
|
There is generally some confusion among authors of command line scripts of
|
|
|
|
when to use which, so here is a quick overview of the differences. As its
|
|
|
|
name indicates, an option is optional. While arguments can be optional
|
|
|
|
within reason, they are much more restricted in how optional they can be.
|
|
|
|
|
|
|
|
To help you decide between options and arguments, the recommendation is
|
|
|
|
to use arguments exclusively for things like going to subcommands or input
|
|
|
|
filenames / URLs, and have everything else be an option instead.
|
|
|
|
|
|
|
|
Differences
|
|
|
|
-----------
|
|
|
|
|
|
|
|
Arguments can do less than options. The following features are only
|
|
|
|
available for options:
|
|
|
|
|
|
|
|
* automatic prompting for missing input
|
|
|
|
* act as flags (boolean or otherwise)
|
|
|
|
* option values can be pulled from environment variables, arguments can not
|
|
|
|
* options are fully documented in the help page, arguments are not
|
2020-07-21 08:23:42 +02:00
|
|
|
(:ref:`this is intentional <documenting-arguments>` as arguments
|
|
|
|
might be too specific to be automatically documented)
|
2014-10-16 20:40:34 +02:00
|
|
|
|
2015-07-16 14:26:14 +02:00
|
|
|
On the other hand arguments, unlike options, can accept an arbitrary number
|
2014-10-16 20:40:34 +02:00
|
|
|
of arguments. Options can strictly ever only accept a fixed number of
|
2020-07-21 08:23:42 +02:00
|
|
|
arguments (defaults to 1), or they may be specified multiple times using
|
|
|
|
:ref:`multiple-options`.
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
Parameter Types
|
|
|
|
---------------
|
|
|
|
|
|
|
|
Parameters can be of different types. Types can be implemented with
|
|
|
|
different behavior and some are supported out of the box:
|
|
|
|
|
|
|
|
``str`` / :data:`click.STRING`:
|
|
|
|
The default parameter type which indicates unicode strings.
|
|
|
|
|
|
|
|
``int`` / :data:`click.INT`:
|
|
|
|
A parameter that only accepts integers.
|
|
|
|
|
|
|
|
``float`` / :data:`click.FLOAT`:
|
|
|
|
A parameter that only accepts floating point values.
|
|
|
|
|
|
|
|
``bool`` / :data:`click.BOOL`:
|
|
|
|
A parameter that accepts boolean values. This is automatically used
|
2018-09-06 20:55:10 +02:00
|
|
|
for boolean flags. If used with string values ``1``, ``yes``, ``y``, ``t``
|
|
|
|
and ``true`` convert to `True` and ``0``, ``no``, ``n``, ``f`` and ``false``
|
2014-10-16 20:40:34 +02:00
|
|
|
convert to `False`.
|
|
|
|
|
|
|
|
:data:`click.UUID`:
|
|
|
|
A parameter that accepts UUID values. This is not automatically
|
|
|
|
guessed but represented as :class:`uuid.UUID`.
|
|
|
|
|
|
|
|
.. autoclass:: File
|
|
|
|
:noindex:
|
|
|
|
|
|
|
|
.. autoclass:: Path
|
|
|
|
:noindex:
|
|
|
|
|
|
|
|
.. autoclass:: Choice
|
|
|
|
:noindex:
|
|
|
|
|
|
|
|
.. autoclass:: IntRange
|
|
|
|
:noindex:
|
|
|
|
|
2018-09-06 20:55:10 +02:00
|
|
|
.. autoclass:: FloatRange
|
|
|
|
:noindex:
|
|
|
|
|
2019-01-07 17:51:19 +01:00
|
|
|
.. autoclass:: DateTime
|
|
|
|
:noindex:
|
|
|
|
|
2014-10-16 20:40:34 +02:00
|
|
|
Custom parameter types can be implemented by subclassing
|
|
|
|
:class:`click.ParamType`. For simple cases, passing a Python function that
|
|
|
|
fails with a `ValueError` is also supported, though discouraged.
|
|
|
|
|
2018-09-06 20:55:10 +02:00
|
|
|
.. _parameter_names:
|
|
|
|
|
2014-10-16 20:40:34 +02:00
|
|
|
Parameter Names
|
|
|
|
---------------
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
Parameters (both options and arguments) have a name that will be used as
|
|
|
|
the Python argument name when calling the decorated function with
|
|
|
|
values.
|
2018-09-06 20:55:10 +02:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
Arguments take only one positional name. To provide a different name for
|
|
|
|
use in help text, see :ref:`doc-meta-variables`.
|
2014-10-16 20:40:34 +02:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
Options can have many names that may be prefixed with one or two dashes.
|
|
|
|
Names with one dash are parsed as short options, names with two are
|
|
|
|
parsed as long options. If a name is not prefixed, it is used as the
|
|
|
|
Python argument name and not parsed as an option name. Otherwise, the
|
|
|
|
first name with a two dash prefix is used, or the first with a one dash
|
|
|
|
prefix if there are none with two. The prefix is removed and dashes are
|
|
|
|
converted to underscores to get the Python argument name.
|
2018-09-06 20:55:10 +02:00
|
|
|
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
Implementing Custom Types
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
To implement a custom type, you need to subclass the :class:`ParamType`
|
2020-07-21 08:23:42 +02:00
|
|
|
class. Override the :meth:`~ParamType.convert` method to convert the
|
|
|
|
value from a string to the correct type.
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
The following code implements an integer type that accepts hex and octal
|
|
|
|
numbers in addition to normal integers, and converts them into regular
|
2020-07-21 08:23:42 +02:00
|
|
|
integers.
|
|
|
|
|
|
|
|
.. code-block:: python
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
import click
|
|
|
|
|
|
|
|
class BasedIntParamType(click.ParamType):
|
2020-07-21 08:23:42 +02:00
|
|
|
name = "integer"
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
def convert(self, value, param, ctx):
|
|
|
|
try:
|
2020-07-21 08:23:42 +02:00
|
|
|
if value[:2].lower() == "0x":
|
2014-10-16 20:40:34 +02:00
|
|
|
return int(value[2:], 16)
|
2020-07-21 08:23:42 +02:00
|
|
|
elif value[:1] == "0":
|
2014-10-16 20:40:34 +02:00
|
|
|
return int(value, 8)
|
|
|
|
return int(value, 10)
|
2020-07-21 08:23:42 +02:00
|
|
|
except TypeError:
|
|
|
|
self.fail(
|
|
|
|
"expected string for int() conversion, got "
|
|
|
|
f"{value!r} of type {type(value).__name__}",
|
|
|
|
param,
|
|
|
|
ctx,
|
|
|
|
)
|
2014-10-16 20:40:34 +02:00
|
|
|
except ValueError:
|
2020-07-21 08:23:42 +02:00
|
|
|
self.fail(f"{value!r} is not a valid integer", param, ctx)
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
BASED_INT = BasedIntParamType()
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
The :attr:`~ParamType.name` attribute is optional and is used for
|
|
|
|
documentation. Call :meth:`~ParamType.fail` if conversion fails. The
|
|
|
|
``param`` and ``ctx`` arguments may be ``None`` in some cases such as
|
|
|
|
prompts.
|