parameters.rst
This file
Path
docs/parameters.rst
139 lines · 4.4 KiB
Last changed by
Break up arguments section (#2586)Rowlando13before adoption8e7bed0
History of this fileTake it
The walk stopped after 200 commits: lines older than 93551dd point at it.
Merge pull request #1878 from pallets/dependabot/pip/sphinx-tabs-3.0.0David Lordbefore adoption93551dd
lines 1–5why, one step back1Parameters2==========34.. currentmodule:: click5Clean up parameters intro. (#2585)Rowlando13before adoption9aeb586
line 6why, one step back6Click supports only two types of parameters for scripts (by design): options and arguments.Merge pull request #1878 from pallets/dependabot/pip/sphinx-tabs-3.0.0David Lordbefore adoption93551dd
line 7why, one step back7Clean up parameters intro. (#2585)Rowlando13before adoption9aeb586
lines 8–9why, one step back8Options9----------------Merge pull request #1878 from pallets/dependabot/pip/sphinx-tabs-3.0.0David Lordbefore adoption93551dd
line 10why, one step back10Clean up parameters intro. (#2585)Rowlando13before adoption9aeb586
lines 11–17why, one step back11* 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.Merge pull request #1878 from pallets/dependabot/pip/sphinx-tabs-3.0.0David Lordbefore adoption93551dd
line 18why, one step back18Clean up parameters intro. (#2585)Rowlando13before adoption9aeb586
lines 19–20why, one step back19Arguments20----------------Merge pull request #1878 from pallets/dependabot/pip/sphinx-tabs-3.0.0David Lordbefore adoption93551dd
line 21why, one step back21Clean up parameters intro. (#2585)Rowlando13before adoption9aeb586
lines 22–26why, one step back22* 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. For more see :ref:`documenting-arguments`.26* Can be pulled from environment variables but only explicitly named ones. For more see :ref:`environment-variables`.Merge pull request #1878 from pallets/dependabot/pip/sphinx-tabs-3.0.0David Lordbefore adoption93551dd
line 27why, one step back27Clean up parameters intro. (#2585)Rowlando13before adoption9aeb586
line 28why, one step back28.. _parameter_names:Merge pull request #1878 from pallets/dependabot/pip/sphinx-tabs-3.0.0David Lordbefore adoption93551dd
line 29why, one step back29Clean up parameters intro. (#2585)Rowlando13before adoption9aeb586
lines 30–55why, one step back30Parameter Names31---------------3233Parameters (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 filename multiple times."""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')55Break up arguments section (#2586)Rowlando13before adoption8e7bed0
lines 56–57why, one step back56.. _parameter-types:57Merge pull request #1878 from pallets/dependabot/pip/sphinx-tabs-3.0.0David Lordbefore adoption93551dd
lines 58–60why, one step back58Parameter Types59---------------60Clean up parameters intro. (#2585)Rowlando13before adoption9aeb586
line 61why, one step back61The supported parameter types are:Merge pull request #1878 from pallets/dependabot/pip/sphinx-tabs-3.0.0David Lordbefore adoption93551dd
lines 62–99why, one step back6263``str`` / :data:`click.STRING`:64 The default parameter type which indicates unicode strings.6566``int`` / :data:`click.INT`:67 A parameter that only accepts integers.6869``float`` / :data:`click.FLOAT`:70 A parameter that only accepts floating point values.7172``bool`` / :data:`click.BOOL`:73 A parameter that accepts boolean values. This is automatically used74 for boolean flags. The string values "1", "true", "t", "yes", "y",75 and "on" convert to ``True``. "0", "false", "f", "no", "n", and76 "off" convert to ``False``.7778:data:`click.UUID`:79 A parameter that accepts UUID values. This is not automatically80 guessed but represented as :class:`uuid.UUID`.8182.. autoclass:: File83 :noindex:8485.. autoclass:: Path86 :noindex:8788.. autoclass:: Choice89 :noindex:9091.. autoclass:: IntRange92 :noindex:9394.. autoclass:: FloatRange95 :noindex:9697.. autoclass:: DateTime98 :noindex:99Clean up parameters intro. (#2585)Rowlando13before adoption9aeb586
lines 100–101why, one step back100How to Implement Custom Types101-------------------------------Merge pull request #1878 from pallets/dependabot/pip/sphinx-tabs-3.0.0David Lordbefore adoption93551dd
line 102why, one step back102Clean up parameters intro. (#2585)Rowlando13before adoption9aeb586
line 103why, one step back103To 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.Merge pull request #1878 from pallets/dependabot/pip/sphinx-tabs-3.0.0David Lordbefore adoption93551dd
lines 104–116why, one step back104105The following code implements an integer type that accepts hex and octal106numbers in addition to normal integers, and converts them into regular107integers.108109.. code-block:: python110111 import click112113 class BasedIntParamType(click.ParamType):114 name = "integer"115116 def convert(self, value, param, ctx):Merge branch '8.0.x'David Lordbefore adoption29df879
lines 117–119why, one step back117 if isinstance(value, int):118 return value119Merge pull request #1878 from pallets/dependabot/pip/sphinx-tabs-3.0.0David Lordbefore adoption93551dd
lines 120–134why, one step back120 try:121 if value[:2].lower() == "0x":122 return int(value[2:], 16)123 elif value[:1] == "0":124 return int(value, 8)125 return int(value, 10)126 except ValueError:127 self.fail(f"{value!r} is not a valid integer", param, ctx)128129 BASED_INT = BasedIntParamType()130131The :attr:`~ParamType.name` attribute is optional and is used for132documentation. Call :meth:`~ParamType.fail` if conversion fails. The133``param`` and ``ctx`` arguments may be ``None`` in some cases such as134prompts.Merge branch '8.0.x'David Lordbefore adoption29df879
lines 135–139why, one step back135136Values from user input or the command line will be strings, but default137values and Python arguments may already be the correct type. The custom138type should check at the top if the value is already valid and pass it139through to support those cases.