Skip to content
Myr

parameters.rst

This file

Path

docs/parameters.rst

112 lines · 4.2 KiB

Last changed by

Added choice supportArmin Ronacherbefore adoption9eacc90

History of this file

Take it

Initial commitArmin Ronacherbefore adoption4101de3

lines 1–11
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 scripts8when to use which so here is a quick overview of the differences. As the9name indicates an option is optional. While arguments can be optional10within reason they are much more restricted in how they can be optional.11

Merge pull request #10 from epequeno/spellcheckArmin Ronacherbefore adoption6241146

lines 12–14why, one step back
12To help you decide between options and arguments, the recommendation is
13to use arguments exclusively for things like going to subcommands or input 14filenames / URLs and have everything else be an option instead.

Initial commitArmin Ronacherbefore adoption4101de3

lines 15–55
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)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 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:class:`click.File`:55 A parameter that is an opened file. It can be opened for reading or

Added choice supportArmin Ronacherbefore adoption9eacc90

lines 56–60why, one step back
56 writing. See :ref:`file-args` for an example.
5758:class:`click.Choice`:59 A parameter that represents a choice of something. See60 :ref:`choice-opts` for an example.

Initial commitArmin Ronacherbefore adoption4101de3

lines 61–63
61
62Custom parameter types can be implemented by subclassing63:class:`click.ParamType`. For simple cases passing a python function that

Merge pull request #11 from yelite/typo-fixArmin Ronacherbefore adoption9c8a3eb

line 64why, one step back
64fails with a `ValueError` is also supported though discouraged.

Initial commitArmin Ronacherbefore adoption4101de3

lines 65–69
65
66Parameter Names67---------------6869Parameters (both options and arguments) accept a number of positional

Merge pull request #10 from epequeno/spellcheckArmin Ronacherbefore adoption6241146

line 70why, one step back
70arguments which are the parameter declarations. Each string with a

Initial commitArmin Ronacherbefore adoption4101de3

lines 71–74
71single dash is added as short argument, each string starting with a double
72dash as long one. If a string is added without any dashes it's the73internal parameter name which is also used as variable name.74

Merge pull request #10 from epequeno/spellcheckArmin Ronacherbefore adoption6241146

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

Initial commitArmin Ronacherbefore adoption4101de3

lines 76–112
76automatically by taking the longest argument and converting all dashes to
77underscores. So for an option with ``('-f', '--foo-bar')`` the parameter78name is `foo_bar`. For an option with ``('-x',)`` the parameter is `x`.79For an option with ``('-f', '--filename', 'dest')`` the parameter is80called `dest`.8182Implementing Custom Types83-------------------------8485To implement a custom type you need to subclass the :class:`ParamType`86class. Types can be invoked with or without context and parameter object87which is why they need to be able to deal with this.8889The following code implements an integer type that accepts hex, octal90numbers in addition to normal integers and converts them into regular91integers::9293 import click9495 class BasedIntParamType(click.ParamType):96 name = 'integer'9798 def convert(self, value, param, ctx):99 try:100 if value[:2].lower() == '0x':101 return int(value[2:], 16)102 elif value[:1] == '0':103 return int(value, 8)104 return int(value, 10)105 except ValueError:106 self.fail('%s is not a valid integer' % value, param, ctx)107108 BASED_INT = BasedIntParamType()109110As you can see a subclass needs to implement the :meth:`ParamType.convert`111method and optionally provide the :attr:`ParamType.name` attribute. The112latter can be used for documentation purposes.