Skip to content
Myr

parameters.rst

This file

Path

docs/parameters.rst

120 lines · 4.2 KiB

Last changed by

Merge pull request #250 from yuvallanger/minor_typosArmin Ronacherbefore adoption898963e

History of this file

Take it

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

Errors now go to stderr. This fixes #103Armin Ronacherbefore adoptionae0572b

lines 1–6why, one step back
1Parameters
2==========34.. currentmodule:: click56Click supports two types of parameters for scripts: options and arguments.

Documentation fixesKevin Yapbefore adoption67523af

lines 7–10why, one step back
7There is generally some confusion among authors of command line scripts of
8when 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.

Errors now go to stderr. This fixes #103Armin Ronacherbefore adoptionae0572b

lines 11–12why, one step back
11
12To help you decide between options and arguments, the recommendation is

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

line 13why, one step back
13to use arguments exclusively for things like going to subcommands or input

Documentation fixesKevin Yapbefore adoption67523af

line 14why, one step back
14filenames / URLs, and have everything else be an option instead.

Errors now go to stderr. This fixes #103Armin Ronacherbefore adoptionae0572b

lines 15–28why, one step back
15
16Differences17-----------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)28

Merge pull request #250 from yuvallanger/minor_typosArmin Ronacherbefore adoption898963e

line 29why, one step back
29On the other hand arguments, unlike options, can accept an arbitrary number

Errors now go to stderr. This fixes #103Armin Ronacherbefore adoptionae0572b

lines 30–70why, one step back
30of arguments. Options can strictly ever only accept a fixed number of
31arguments (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 used50 for boolean flags. If used with string values ``1``, ``yes``, ``y``51 and ``true`` convert to `True` and ``0``, ``no``, ``n`` and ``false``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:6970Custom parameter types can be implemented by subclassing

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

lines 71–72why, one step back
71:class:`click.ParamType`. For simple cases, passing a Python function that
72fails with a `ValueError` is also supported, though discouraged.

Errors now go to stderr. This fixes #103Armin Ronacherbefore adoptionae0572b

lines 73–78why, one step back
73
74Parameter Names75---------------7677Parameters (both options and arguments) accept a number of positional78arguments which are the parameter declarations. Each string with a

Documentation fixesKevin Yapbefore adoption67523af

lines 79–80why, one step back
79single dash is added as short argument; each string starting with a double
80dash as long one. If a string is added without any dashes, it becomes the

Errors now go to stderr. This fixes #103Armin Ronacherbefore adoptionae0572b

lines 81–82why, one step back
81internal parameter name which is also used as variable name.
82

Documentation fixesKevin Yapbefore adoption67523af

line 83why, one step back
83If a parameter is not given a name without dashes, a name is generated

Errors now go to stderr. This fixes #103Armin Ronacherbefore adoptionae0572b

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

Documentation fixesKevin Yapbefore adoption67523af

lines 85–87why, one step back
85underscores. For an option with ``('-f', '--foo-bar')``, the parameter
86name is `foo_bar`. For an option with ``('-x',)``, the parameter is `x`.87For an option with ``('-f', '--filename', 'dest')``, the parameter is

Errors now go to stderr. This fixes #103Armin Ronacherbefore adoptionae0572b

lines 88–92why, one step back
88called `dest`.
8990Implementing Custom Types91-------------------------92

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

line 93why, one step back
93To implement a custom type, you need to subclass the :class:`ParamType`

Documentation fixesKevin Yapbefore adoption67523af

line 94why, one step back
94class. Types can be invoked with or without context and parameter object,

Errors now go to stderr. This fixes #103Armin Ronacherbefore adoptionae0572b

lines 95–96why, one step back
95which is why they need to be able to deal with this.
96

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

line 97why, one step back
97The following code implements an integer type that accepts hex and octal

Documentation fixesKevin Yapbefore adoption67523af

line 98why, one step back
98numbers in addition to normal integers, and converts them into regular

Errors now go to stderr. This fixes #103Armin Ronacherbefore adoptionae0572b

lines 99–117why, one step back
99integers::
100101 import click102103 class BasedIntParamType(click.ParamType):104 name = 'integer'105106 def convert(self, value, param, ctx):107 try:108 if value[:2].lower() == '0x':109 return int(value[2:], 16)110 elif value[:1] == '0':111 return int(value, 8)112 return int(value, 10)113 except ValueError:114 self.fail('%s is not a valid integer' % value, param, ctx)115116 BASED_INT = BasedIntParamType()117

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

line 118why, one step back
118As you can see, a subclass needs to implement the :meth:`ParamType.convert`

Errors now go to stderr. This fixes #103Armin Ronacherbefore adoptionae0572b

lines 119–120why, one step back
119method and optionally provide the :attr:`ParamType.name` attribute. The
120latter can be used for documentation purposes.