parameters.rst
This file
Path
docs/parameters.rst
139 lines · 4.7 KiB
Last changed by
Merge pull request #1099 from sirosen/type-datetimeDavid Lordbefore adoption2c622ee
History of this fileTake it
The walk stopped after 200 commits: lines older than 4552159 point at it.
Fixed syntax errorArmin Ronacherbefore adoption4552159
lines 1–49why, one step back1Parameters2==========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 not26 (this is intentional as arguments might be too specific to be27 automatically documented)2829On 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 usedMerge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
lines 50–51why, one step back50 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``Fixed syntax errorArmin Ronacherbefore adoption4552159
lines 52–69why, one step back52 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:69Merge pull request #538 from DenisCarriere/masterMarkus Unterwaditzerbefore adoptioncdea8fc
lines 70–72why, one step back70.. autoclass:: FloatRange71 :noindex:72Merge pull request #1099 from sirosen/type-datetimeDavid Lordbefore adoption2c622ee
lines 73–75why, one step back73.. autoclass:: DateTime74 :noindex:75Fixed syntax errorArmin Ronacherbefore adoption4552159
lines 76–79why, one step back76Custom parameter types can be implemented by subclassing77:class:`click.ParamType`. For simple cases, passing a Python function that78fails with a `ValueError` is also supported, though discouraged.79Merge pull request #1016 from dawran6/issue-725Dan Sullybefore adoptionc914686
lines 80–81why, one step back80.. _parameter_names:81Fixed syntax errorArmin Ronacherbefore adoption4552159
lines 82–84why, one step back82Parameter Names83---------------84Merge pull request #1009 from dougharris/parameter-949Dan Sullybefore adoptionbf30737
lines 85–88why, one step back85Parameters (both options and arguments) accept a number of positional arguments86which 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.Fixed syntax errorArmin Ronacherbefore adoption4552159
line 89why, one step back89Merge pull request #1009 from dougharris/parameter-949Dan Sullybefore adoptionbf30737
lines 90–93why, one step back90If a string is added without any dashes, it becomes the internal parameter name91which is also used as variable name.9293If all names for a parameter contain dashes, the internal name is generatedFixed syntax errorArmin Ronacherbefore adoption4552159
line 94why, one step back94automatically by taking the longest argument and converting all dashes toMerge pull request #1009 from dougharris/parameter-949Dan Sullybefore adoptionbf30737
lines 95–96why, one step back95underscores.96Merge pull request #1055 from ZenithDK/patch-1Dan Sullybefore adoptionfb3e9d2
lines 97–98why, one step back97The internal name is converted to lowercase.98Merge pull request #1009 from dougharris/parameter-949Dan Sullybefore adoptionbf30737
lines 99–103why, one step back99Examples: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 back104* For an option with ``('--CamelCaseOption',)``, the parameter is `camelcaseoption`.Merge pull request #1009 from dougharris/parameter-949Dan Sullybefore adoptionbf30737
lines 105–107why, one step back105* For an arguments with ``(`foogle`)``, the parameter name is `foogle`. To106 provide a different human readable name for use in help text, see the section107 about :ref:`doc-meta-variables`.Fixed syntax errorArmin Ronacherbefore adoption4552159
lines 108–139why, one step back108109Implementing Custom Types110-------------------------111112To implement a custom type, you need to subclass the :class:`ParamType`113class. Types can be invoked with or without context and parameter object,114which is why they need to be able to deal with this.115116The following code implements an integer type that accepts hex and octal117numbers in addition to normal integers, and converts them into regular118integers::119120 import click121122 class BasedIntParamType(click.ParamType):123 name = 'integer'124125 def convert(self, value, param, ctx):126 try:127 if value[:2].lower() == '0x':128 return int(value[2:], 16)129 elif value[:1] == '0':130 return int(value, 8)131 return int(value, 10)132 except ValueError:133 self.fail('%s is not a valid integer' % value, param, ctx)134135 BASED_INT = BasedIntParamType()136137As you can see, a subclass needs to implement the :meth:`ParamType.convert`138method and optionally provide the :attr:`ParamType.name` attribute. The139latter can be used for documentation purposes.