Skip to content
Myr

parameters.rst

This file

Path

docs/parameters.rst

149 lines · 5.0 KiB

Last changed by

Merge pull request #1325 from mattsb42/argdocDavid Lordbefore adoption8e4d4d2

History of this file

Take it

The walk stopped after 200 commits: lines older than 430c50a point at it.

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 1–25why, one step back
1Parameters
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.1112To 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.1516Differences17-----------1819Arguments can do less than options. The following features are only20available for options: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 not

Merge pull request #1325 from mattsb42/argdocDavid Lordbefore adoption8e4d4d2

lines 26–27why, one step back
26 (:ref:`this is intentional <documenting-arguments>` as arguments
27 might be too specific to be automatically documented)

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 28–49why, one step back
28
29On 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).3233Parameter Types34---------------3536Parameters can be of different types. Types can be implemented with37different behavior and some are supported out of the box:3839``str`` / :data:`click.STRING`:40 The default parameter type which indicates unicode strings.4142``int`` / :data:`click.INT`:43 A parameter that only accepts integers.4445``float`` / :data:`click.FLOAT`:46 A parameter that only accepts floating point values.4748``bool`` / :data:`click.BOOL`:49 A parameter that accepts boolean values. This is automatically used

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 50–51why, one step back
50 for boolean flags. If used with string values ``1``, ``yes``, ``y``, ``t``
51 and ``true`` convert to `True` and ``0``, ``no``, ``n``, ``f`` and ``false``

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 52–72why, one step back
52 convert to `False`.
5354:data:`click.UUID`:55 A parameter that accepts UUID values. This is not automatically56 guessed but represented as :class:`uuid.UUID`.5758.. autoclass:: File59 :noindex:6061.. autoclass:: Path62 :noindex:6364.. autoclass:: Choice65 :noindex:6667.. autoclass:: IntRange68 :noindex:6970.. autoclass:: FloatRange71 :noindex:72

Merge pull request #1099 from sirosen/type-datetimeDavid Lordbefore adoption2c622ee

lines 73–75why, one step back
73.. autoclass:: DateTime
74 :noindex:75

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 76–79why, one step back
76Custom parameter types can be implemented by subclassing
77:class:`click.ParamType`. For simple cases, passing a Python function that78fails with a `ValueError` is also supported, though discouraged.79

Merge pull request #1016 from dawran6/issue-725Dan Sullybefore adoptionc914686

lines 80–81why, one step back
80.. _parameter_names:
81

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 82–84why, one step back
82Parameter Names
83---------------84

Merge pull request #1009 from dougharris/parameter-949Dan Sullybefore adoptionbf30737

lines 85–88why, one step back
85Parameters (both options and arguments) accept a number of positional arguments
86which are passed to the command function as parameters. Each string with a87single dash is added as a short argument; each string starting with a double88dash as a long one.

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

line 89why, one step back
89

Merge pull request #1009 from dougharris/parameter-949Dan Sullybefore adoptionbf30737

lines 90–93why, one step back
90If a string is added without any dashes, it becomes the internal parameter name
91which is also used as variable name.9293If all names for a parameter contain dashes, the internal name is generated

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

line 94why, one step back
94automatically by taking the longest argument and converting all dashes to

Merge pull request #1009 from dougharris/parameter-949Dan Sullybefore adoptionbf30737

lines 95–96why, one step back
95underscores.
96

Merge pull request #1055 from ZenithDK/patch-1Dan Sullybefore adoptionfb3e9d2

lines 97–98why, one step back
97The internal name is converted to lowercase.
98

Merge pull request #1009 from dougharris/parameter-949Dan Sullybefore adoptionbf30737

lines 99–103why, one step back
99Examples:
100101* For an option with ``('-f', '--foo-bar')``, the parameter name is `foo_bar`.102* For an option with ``('-x',)``, the parameter is `x`.103* For an option with ``('-f', '--filename', 'dest')``, the parameter name is `dest`.

Merge pull request #1055 from ZenithDK/patch-1Dan Sullybefore adoptionfb3e9d2

line 104why, one step back
104* For an option with ``('--CamelCaseOption',)``, the parameter is `camelcaseoption`.

Merge pull request #1009 from dougharris/parameter-949Dan Sullybefore adoptionbf30737

lines 105–107why, one step back
105* For an arguments with ``(`foogle`)``, the parameter name is `foogle`. To
106 provide a different human readable name for use in help text, see the section107 about :ref:`doc-meta-variables`.

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 108–112why, one step back
108
109Implementing Custom Types110-------------------------111112To implement a custom type, you need to subclass the :class:`ParamType`

fix custom type docs indentationDavid Lordbefore adoption0049354

lines 113–114why, one step back
113class. Override the :meth:`~ParamType.convert` method to convert the
114value from a string to the correct type.

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 115–117why, one step back
115
116The following code implements an integer type that accepts hex and octal117numbers in addition to normal integers, and converts them into regular

fix custom type docs indentationDavid Lordbefore adoption0049354

line 118why, one step back
118integers.

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

line 119why, one step back
119

fix custom type docs indentationDavid Lordbefore adoption0049354

lines 120–121why, one step back
120.. code-block:: python
121

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 122–124why, one step back
122 import click
123124 class BasedIntParamType(click.ParamType):

fix custom type docs indentationDavid Lordbefore adoption0049354

line 125why, one step back
125 name = "integer"

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 126–128why, one step back
126
127 def convert(self, value, param, ctx):128 try:

fix custom type docs indentationDavid Lordbefore adoption0049354

line 129why, one step back
129 if value[:2].lower() == "0x":

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

line 130why, one step back
130 return int(value[2:], 16)

fix custom type docs indentationDavid Lordbefore adoption0049354

line 131why, one step back
131 elif value[:1] == "0":

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 132–133why, one step back
132 return int(value, 8)
133 return int(value, 10)

fix custom type docs indentationDavid Lordbefore adoption0049354

lines 134–140why, one step back
134 except TypeError:
135 self.fail(136 "expected string for int() conversion, got "137 f"{value!r} of type {type(value).__name__}",138 param,139 ctx,140 )

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

line 141why, one step back
141 except ValueError:

fix custom type docs indentationDavid Lordbefore adoption0049354

line 142why, one step back
142 self.fail(f"{value!r} is not a valid integer", param, ctx)

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 143–145why, one step back
143
144 BASED_INT = BasedIntParamType()145

fix custom type docs indentationDavid Lordbefore adoption0049354

lines 146–149why, one step back
146The :attr:`~ParamType.name` attribute is optional and is used for
147documentation. Call :meth:`~ParamType.fail` if conversion fails. The148``param`` and ``ctx`` arguments may be ``None`` in some cases such as149prompts.