Skip to content
Myr

parameters.rst

This file

Path

docs/parameters.rst

142 lines · 4.7 KiB

Last changed by

Merge branch '7.x'David Lordbefore adoption8f36eab

History of this file

Take it

The walk stopped after 200 commits: lines older than 81b2fda point at it.

Merge pull request #801 from pombredanne/patch-3David Lordbefore adoption81b2fda

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 branch '7.x'David Lordbefore adoptionffcce4f

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 #801 from pombredanne/patch-3David Lordbefore adoption81b2fda

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 branch '7.x'David Lordbefore adoptionc6ef735

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

Merge pull request #801 from pombredanne/patch-3David Lordbefore adoption81b2fda

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``

Merge pull request #801 from pombredanne/patch-3David Lordbefore adoption81b2fda

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

Merge pull request #801 from pombredanne/patch-3David Lordbefore adoption81b2fda

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

Merge pull request #801 from pombredanne/patch-3David Lordbefore adoption81b2fda

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

Merge branch '7.x'David Lordbefore adoption8f36eab

lines 86–88why, one step back
86Parameters (both options and arguments) have a name that will be used as
87the Python argument name when calling the decorated function with88values.

Merge pull request #801 from pombredanne/patch-3David Lordbefore adoption81b2fda

line 89why, one step back
89

Merge branch '7.x'David Lordbefore adoption8f36eab

lines 90–91why, one step back
90Arguments take only one positional name. To provide a different name for
91use in help text, see :ref:`doc-meta-variables`.

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

line 92why, one step back
92

Merge branch '7.x'David Lordbefore adoption8f36eab

lines 93–99why, one step back
93Options 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.

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

line 100why, one step back
100

Merge pull request #801 from pombredanne/patch-3David Lordbefore adoption81b2fda

lines 101–105why, one step back
101
102Implementing Custom Types103-------------------------104105To implement a custom type, you need to subclass the :class:`ParamType`

Merge branch '7.x'David Lordbefore adoptiona438322

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

Merge pull request #801 from pombredanne/patch-3David Lordbefore adoption81b2fda

lines 108–110why, one step back
108
109The following code implements an integer type that accepts hex and octal110numbers in addition to normal integers, and converts them into regular

Merge branch '7.x'David Lordbefore adoptiona438322

line 111why, one step back
111integers.

Merge pull request #801 from pombredanne/patch-3David Lordbefore adoption81b2fda

line 112why, one step back
112

Merge branch '7.x'David Lordbefore adoptiona438322

lines 113–114why, one step back
113.. code-block:: python
114

Merge pull request #801 from pombredanne/patch-3David Lordbefore adoption81b2fda

lines 115–117why, one step back
115 import click
116117 class BasedIntParamType(click.ParamType):

Merge branch '7.x'David Lordbefore adoptiona438322

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

Merge pull request #801 from pombredanne/patch-3David Lordbefore adoption81b2fda

lines 119–121why, one step back
119
120 def convert(self, value, param, ctx):121 try:

Merge branch '7.x'David Lordbefore adoptiona438322

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

Merge pull request #801 from pombredanne/patch-3David Lordbefore adoption81b2fda

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

Merge branch '7.x'David Lordbefore adoptiona438322

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

Merge pull request #801 from pombredanne/patch-3David Lordbefore adoption81b2fda

lines 125–126why, one step back
125 return int(value, 8)
126 return int(value, 10)

Merge branch '7.x'David Lordbefore adoptiona438322

lines 127–133why, one step back
127 except TypeError:
128 self.fail(129 "expected string for int() conversion, got "130 f"{value!r} of type {type(value).__name__}",131 param,132 ctx,133 )

Merge pull request #801 from pombredanne/patch-3David Lordbefore adoption81b2fda

line 134why, one step back
134 except ValueError:

Merge branch '7.x'David Lordbefore adoptiona438322

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

Merge pull request #801 from pombredanne/patch-3David Lordbefore adoption81b2fda

lines 136–138why, one step back
136
137 BASED_INT = BasedIntParamType()138

Merge branch '7.x'David Lordbefore adoptiona438322

lines 139–142why, one step back
139The :attr:`~ParamType.name` attribute is optional and is used for
140documentation. Call :meth:`~ParamType.fail` if conversion fails. The141``param`` and ``ctx`` arguments may be ``None`` in some cases such as142prompts.