Skip to content
Myr

parameters.rst

This file

Path

docs/parameters.rst

150 lines · 5.0 KiB

Last changed by

Merge pull request #1157 from theazureshadow/clarify-multiple-optionsDavid Lordbefore adoption59a4530

History of this file

Take it

The walk stopped after 200 commits: lines older than 950463b point at it.

Fix typo (#817)scottbeldenbefore adoption950463b

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)

Fix typo (#817)scottbeldenbefore adoption950463b

lines 28–30why, 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 of

Merge pull request #1157 from theazureshadow/clarify-multiple-optionsDavid Lordbefore adoption59a4530

lines 31–32why, one step back
31arguments (defaults to 1), or they may be specified multiple times using
32:ref:`multiple-options`.

Fix typo (#817)scottbeldenbefore adoption950463b

lines 33–50why, one step back
33
34Parameter Types35---------------3637Parameters can be of different types. Types can be implemented with38different behavior and some are supported out of the box:3940``str`` / :data:`click.STRING`:41 The default parameter type which indicates unicode strings.4243``int`` / :data:`click.INT`:44 A parameter that only accepts integers.4546``float`` / :data:`click.FLOAT`:47 A parameter that only accepts floating point values.4849``bool`` / :data:`click.BOOL`:50 A parameter that accepts boolean values. This is automatically used

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 53–73why, one step back
53 convert to `False`.
5455:data:`click.UUID`:56 A parameter that accepts UUID values. This is not automatically57 guessed but represented as :class:`uuid.UUID`.5859.. autoclass:: File60 :noindex:6162.. autoclass:: Path63 :noindex:6465.. autoclass:: Choice66 :noindex:6768.. autoclass:: IntRange69 :noindex:7071.. autoclass:: FloatRange72 :noindex:73

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

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

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

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

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

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 90why, one step back
90

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

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

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

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

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

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

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

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

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

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

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

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 109–113why, one step back
109
110Implementing Custom Types111-------------------------112113To implement a custom type, you need to subclass the :class:`ParamType`

fix custom type docs indentationDavid Lordbefore adoption0049354

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

fix custom type docs indentationDavid Lordbefore adoption0049354

line 119why, one step back
119integers.

Fix typo (#817)scottbeldenbefore adoption950463b

line 120why, one step back
120

fix custom type docs indentationDavid Lordbefore adoption0049354

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 123–125why, one step back
123 import click
124125 class BasedIntParamType(click.ParamType):

fix custom type docs indentationDavid Lordbefore adoption0049354

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

fix custom type docs indentationDavid Lordbefore adoption0049354

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

fix custom type docs indentationDavid Lordbefore adoption0049354

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

fix custom type docs indentationDavid Lordbefore adoption0049354

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 142why, one step back
142 except ValueError:

fix custom type docs indentationDavid Lordbefore adoption0049354

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

fix custom type docs indentationDavid Lordbefore adoption0049354

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