Skip to content
Myr

options.rst

This file

Path

docs/options.rst

484 lines · 12.5 KiB

Last changed by

Always pick first optoin instead of longestArmin Ronacherbefore adoptionb8e766f

History of this file

Take it

Initial commitArmin Ronacherbefore adoption4101de3

lines 1–7
1Options
2=======34.. currentmodule:: click56Adding options to commands can be accomplished by the :func:`option`7decorator. Since options can come in various different versions there

Merge pull request #10 from epequeno/spellcheckArmin Ronacherbefore adoption6241146

line 8why, one step back
8are a ton of parameters to configure their behavior.

Initial commitArmin Ronacherbefore adoption4101de3

lines 9–15
9
10Basic Value Options11-------------------1213The most basic option is a value option. These options accept one14argument which is a value. If no type is provided the type of the default15value is used. If no default value is provided the type is assumed to be

Always pick first optoin instead of longestArmin Ronacherbefore adoptionb8e766f

lines 16–17why, one step back
16:data:`STRING`. The name of the parameter is by default the first long
17option defined, otherwise the first short one.

Initial commitArmin Ronacherbefore adoption4101de3

line 18
18

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 19why, one step back
19.. click:example::

Initial commitArmin Ronacherbefore adoption4101de3

lines 20–23
20
21 @click.command()22 @click.option('--n', default=1)23 def dots(n):

Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3

line 24why, one step back
24 click.echo('.' * n)

Initial commitArmin Ronacherbefore adoption4101de3

line 25
25

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 26why, one step back
26And on the command line:

Initial commitArmin Ronacherbefore adoption4101de3

line 27
27

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 28why, one step back
28.. click:run::

Initial commitArmin Ronacherbefore adoption4101de3

line 29
29

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 30–31why, one step back
30 invoke(dots, args=['--n=2'])
31

Initial commitArmin Ronacherbefore adoption4101de3

lines 32–39
32In this case the option is of type :data:`INT` because the default value
33is an integer.3435Multi Value Options36-------------------3738Sometimes you have options that take more than one argument. For options39only a fixed number of arguments is supported. This can be configured by

Merge pull request #21 from julen/doc-fixesArmin Ronacherbefore adoptiona4b7d70

line 40why, one step back
40the ``nargs`` parameter. The values are then stored as a tuple.

Initial commitArmin Ronacherbefore adoption4101de3

line 41
41

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 42why, one step back
42.. click:example::

Initial commitArmin Ronacherbefore adoption4101de3

lines 43–46
43
44 @click.command()45 @click.option('--pos', nargs=2, type=float)46 def findme(pos):

Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3

line 47why, one step back
47 click.echo('%s / %s' % pos)

Initial commitArmin Ronacherbefore adoption4101de3

line 48
48

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 49why, one step back
49And on the command line:

Initial commitArmin Ronacherbefore adoption4101de3

line 50
50

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 51why, one step back
51.. click:run::

Initial commitArmin Ronacherbefore adoption4101de3

line 52
52

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 53–54why, one step back
53 invoke(findme, args=['--pos', '2.0', '3.0'])
54

Added an example for multipleArmin Ronacherbefore adoption98b7d1d

lines 55–63why, one step back
55Multiple Options
56----------------5758Similar to ``nargs`` there is also the case where sometimes you want to59support a parameter to be provided multiple times to and have all values60recorded and not just the last one. For instance ``git commit -m foo -m61bar`` would record two lines for the commit message. ``foo`` and ``bar``.62This can be accomplished with the ``multiple`` flag:63

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 64why, one step back
64Example:

Added an example for multipleArmin Ronacherbefore adoption98b7d1d

line 65why, one step back
65

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 66–67why, one step back
66.. click:example::
67

Added an example for multipleArmin Ronacherbefore adoption98b7d1d

lines 68–70why, one step back
68 @click.command()
69 @click.option('--message', '-m', multiple=True)70 def commit(message):

Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3

line 71why, one step back
71 click.echo('\n'.join(message))

Added an example for multipleArmin Ronacherbefore adoption98b7d1d

line 72why, one step back
72

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 73why, one step back
73And on the command line:

Added an example for multipleArmin Ronacherbefore adoption98b7d1d

line 74why, one step back
74

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 75why, one step back
75.. click:run::

Added an example for multipleArmin Ronacherbefore adoption98b7d1d

line 76why, one step back
76

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 77–78why, one step back
77 invoke(commit, args=['-m', 'foo', '-m', 'bar'])
78

Added some more statements on argparse and how verbosity can be implemented in click.Armin Ronacherbefore adoption84fbcb2

lines 79–81why, one step back
79Counting
80--------81

Added count support and int rangesArmin Ronacherbefore adoption81930b5

lines 82–84why, one step back
82In some very rare circumstances it's interesting to use the repeating of
83options to count an integer up. This for instance can be used for84verbosity flags:

Added some more statements on argparse and how verbosity can be implemented in click.Armin Ronacherbefore adoption84fbcb2

line 85why, one step back
85

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 86–87why, one step back
86.. click:example::
87

Added some more statements on argparse and how verbosity can be implemented in click.Armin Ronacherbefore adoption84fbcb2

line 88why, one step back
88 @click.command()

Added count support and int rangesArmin Ronacherbefore adoption81930b5

line 89why, one step back
89 @click.option('-v', '--verbose', count=True)

Added some more statements on argparse and how verbosity can be implemented in click.Armin Ronacherbefore adoption84fbcb2

line 90why, one step back
90 def log(verbose):

Added count support and int rangesArmin Ronacherbefore adoption81930b5

line 91why, one step back
91 click.echo('Verbosity: %s' % verbose)

Added some more statements on argparse and how verbosity can be implemented in click.Armin Ronacherbefore adoption84fbcb2

line 92why, one step back
92

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 93why, one step back
93And on the command line:

Added some more statements on argparse and how verbosity can be implemented in click.Armin Ronacherbefore adoption84fbcb2

line 94why, one step back
94

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 95why, one step back
95.. click:run::

Added some more statements on argparse and how verbosity can be implemented in click.Armin Ronacherbefore adoption84fbcb2

line 96why, one step back
96

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 97–98why, one step back
97 invoke(log, args=['-vvv'])
98

Initial commitArmin Ronacherbefore adoption4101de3

lines 99–106
99Boolean Flags
100-------------101102Boolean flags are options that can be enabled or disabled. This can be103accomplished by defining two flags in one go separated by a slash (``/``)104for enabling or disabling the option. Click always wants you to provide105an enable and disable flag so that you can change the default later.106

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 107why, one step back
107Example:

Initial commitArmin Ronacherbefore adoption4101de3

line 108
108

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 109–110why, one step back
109.. click:example::
110

Initial commitArmin Ronacherbefore adoption4101de3

lines 111–118
111 import os
112113 @click.command()114 @click.option('--shout/--no-shout', default=False)115 def info(shout):116 rv = os.uname()[0]117 if shout:118 rv = rv.upper() + '!!!!111'

Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3

line 119why, one step back
119 click.echo(rv)

Initial commitArmin Ronacherbefore adoption4101de3

line 120
120

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 121why, one step back
121And on the command line:

Initial commitArmin Ronacherbefore adoption4101de3

line 122
122

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 123why, one step back
123.. click:run::

Initial commitArmin Ronacherbefore adoption4101de3

line 124
124

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 125–127why, one step back
125 invoke(info, args=['--shout'])
126 invoke(info, args=['--no-shout'])127

Initial commitArmin Ronacherbefore adoption4101de3

line 128
128If you really don't want an off-switch you can just define one and

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 129why, one step back
129manually inform click that something is a flag:

Initial commitArmin Ronacherbefore adoption4101de3

line 130
130

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 131–132why, one step back
131.. click:example::
132

Initial commitArmin Ronacherbefore adoption4101de3

lines 133–140
133 import os
134135 @click.command()136 @click.option('--shout', is_flag=True)137 def info(shout):138 rv = os.uname()[0]139 if shout:140 rv = rv.upper() + '!!!!111'

Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3

line 141why, one step back
141 click.echo(rv)

Initial commitArmin Ronacherbefore adoption4101de3

line 142
142

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 143why, one step back
143And on the command line:

Initial commitArmin Ronacherbefore adoption4101de3

line 144
144

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 145why, one step back
145.. click:run::

Initial commitArmin Ronacherbefore adoption4101de3

line 146
146

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 147–148why, one step back
147 invoke(info, args=['--shout'])
148

Initial commitArmin Ronacherbefore adoption4101de3

lines 149–151
149Feature Switches
150----------------151

Merge pull request #21 from julen/doc-fixesArmin Ronacherbefore adoptiona4b7d70

line 152why, one step back
152In addition to boolean flags there are also feature switches. These are

Initial commitArmin Ronacherbefore adoption4101de3

line 153
153implemented by setting multiple options to the same parameter name and by

Merge pull request #10 from epequeno/spellcheckArmin Ronacherbefore adoption6241146

lines 154–155why, one step back
154defining a flag value. To set a default flag assign a value of `True` to
155the flag that should be the default.

Initial commitArmin Ronacherbefore adoption4101de3

line 156
156

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 157why, one step back
157.. click:example::

Initial commitArmin Ronacherbefore adoption4101de3

lines 158–165
158
159 import os160161 @click.command()162 @click.option('--upper', 'transformation', flag_value='upper',163 default=True)164 @click.option('--lower', 'transformation', flag_value='lower')165 def info(transformation):

Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3

line 166why, one step back
166 click.echo(getattr(os.uname()[0], transformation)())

Initial commitArmin Ronacherbefore adoption4101de3

line 167
167

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 168why, one step back
168And on the command line:

Initial commitArmin Ronacherbefore adoption4101de3

line 169
169

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 170why, one step back
170.. click:run::

Initial commitArmin Ronacherbefore adoption4101de3

line 171
171

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 172–175why, one step back
172 invoke(info, args=['--upper'])
173 invoke(info, args=['--lower'])174 invoke(info)175

Added choice supportArmin Ronacherbefore adoption9eacc90

lines 176–181why, one step back
176.. _choice-opts:
177178Choice Options179--------------180181Sometimes you want to have a parameter be a choice of a list of values.

Merge pull request #34 from julen/doc-fixesArmin Ronacherbefore adoption50690e4

line 182why, one step back
182In that case you can use :class:`Choice` type. It can be instantiated

Added choice supportArmin Ronacherbefore adoption9eacc90

lines 183–184why, one step back
183with a list of valid values.
184

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 185why, one step back
185Example:

Added choice supportArmin Ronacherbefore adoption9eacc90

line 186why, one step back
186

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 187–188why, one step back
187.. click:example::
188

Added choice supportArmin Ronacherbefore adoption9eacc90

lines 189–191why, one step back
189 @click.command()
190 @click.option('--hash-type', type=click.Choice(['md5', 'sha1']))191 def digest(hash_type):

Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3

line 192why, one step back
192 click.echo(hash_type)

Added choice supportArmin Ronacherbefore adoption9eacc90

line 193why, one step back
193

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 194why, one step back
194What it looks like:

Added choice supportArmin Ronacherbefore adoption9eacc90

line 195why, one step back
195

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 196why, one step back
196.. click:run::

Added choice supportArmin Ronacherbefore adoption9eacc90

line 197why, one step back
197

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 198–202why, one step back
198 invoke(digest, args=['--hash-type=md5'])
199 println()200 invoke(digest, args=['--hash-type=foo'])201 println()202 invoke(digest, args=['--help'])

Added choice supportArmin Ronacherbefore adoption9eacc90

line 203why, one step back
203

Initial commitArmin Ronacherbefore adoption4101de3

lines 204–210
204.. _option-prompting:
205206Prompting207---------208209Sometimes you want parameters that can either be provided from the command210line or if not, you want to ask for user input. This can be implemented

Merge pull request #21 from julen/doc-fixesArmin Ronacherbefore adoptiona4b7d70

line 211why, one step back
211with click by defining a prompt string.

Initial commitArmin Ronacherbefore adoption4101de3

line 212
212

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 213why, one step back
213Example:

Initial commitArmin Ronacherbefore adoption4101de3

line 214
214

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 215why, one step back
215.. click:example::

Initial commitArmin Ronacherbefore adoption4101de3

lines 216–219
216
217 @click.command()218 @click.option('--name', prompt=True)219 def hello(name):

Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3

line 220why, one step back
220 click.echo('Hello %s!' % name)

Initial commitArmin Ronacherbefore adoption4101de3

line 221
221

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 222why, one step back
222And what it looks like:

Initial commitArmin Ronacherbefore adoption4101de3

line 223
223

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 224why, one step back
224.. click:run::

Initial commitArmin Ronacherbefore adoption4101de3

line 225
225

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 226–228why, one step back
226 invoke(hello, args=['--name=John'])
227 invoke(hello, input=['John'])228

Initial commitArmin Ronacherbefore adoption4101de3

line 229
229If you are not happy with the default prompt string you can ask for

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 230why, one step back
230a different one:

Initial commitArmin Ronacherbefore adoption4101de3

line 231
231

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 232why, one step back
232.. click:example::

Initial commitArmin Ronacherbefore adoption4101de3

lines 233–236
233
234 @click.command()235 @click.option('--name', prompt='Your name please')236 def hello(name):

Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3

line 237why, one step back
237 click.echo('Hello %s!' % name)

Initial commitArmin Ronacherbefore adoption4101de3

line 238
238

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 239why, one step back
239What it looks like:

Initial commitArmin Ronacherbefore adoption4101de3

line 240
240

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 241why, one step back
241.. click:run::

Initial commitArmin Ronacherbefore adoption4101de3

line 242
242

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 243–244why, one step back
243 invoke(hello, input=['John'])
244

Initial commitArmin Ronacherbefore adoption4101de3

lines 245–248
245Password Prompts
246----------------247248Click also supports hidden prompts and asking for confirmation. This is

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 249why, one step back
249useful for password input:

Initial commitArmin Ronacherbefore adoption4101de3

line 250
250

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 251–252why, one step back
251.. click:example::
252

Initial commitArmin Ronacherbefore adoption4101de3

lines 253–256
253 @click.command()
254 @click.option('--password', prompt=True, hide_input=True,255 confirmation_prompt=True)256 def encrypt(password):

Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3

line 257why, one step back
257 click.echo('Encrypting password to %s' % password.encode('rot13'))

Initial commitArmin Ronacherbefore adoption4101de3

line 258
258

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 259why, one step back
259What it looks like:

Initial commitArmin Ronacherbefore adoption4101de3

line 260
260

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 261why, one step back
261.. click:run::

Initial commitArmin Ronacherbefore adoption4101de3

line 262
262

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 263–264why, one step back
263 invoke(encrypt, input=['secret', 'secret'])
264

Initial commitArmin Ronacherbefore adoption4101de3

line 265
265Because this combination of parameters is quite common this can also be

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 266why, one step back
266replaced with the :func:`password_option` decorator:

Initial commitArmin Ronacherbefore adoption4101de3

line 267
267

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 268–269why, one step back
268.. click:example::
269

Initial commitArmin Ronacherbefore adoption4101de3

lines 270–272
270 @click.command()
271 @click.password_option()272 def encrypt(password):

Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3

line 273why, one step back
273 click.echo('Encrypting password to %s' % password.encode('rot13'))

Initial commitArmin Ronacherbefore adoption4101de3

lines 274–279
274
275Callbacks and Eager Options276---------------------------277278Sometimes you want a parameter to completely change the execution flow.279This for instance is the case when you want to have a ``--version``

Merge pull request #21 from julen/doc-fixesArmin Ronacherbefore adoptiona4b7d70

line 280why, one step back
280parameter that prints out the version and then exits the application.

Initial commitArmin Ronacherbefore adoption4101de3

lines 281–285
281
282In such cases you need two concepts: eager parameters and a callback. An283eager parameter is a parameter that is handled before others and a284callback is what executes after the parameter is handled. The eagerness285is necessary so that an earlier required parameter does not produce an

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

line 286why, one step back
286error message. For instance if ``--version`` was not eager and a

Initial commitArmin Ronacherbefore adoption4101de3

lines 287–294
287parameter ``--foo`` was required and defined before, you would need to
288specify it for ``--version`` to work.289290A callback is a function that is invoked with two parameters: the current291:class:`Context` and the value. The context provides some useful features292such as quitting the application and gives access to other already293processed parameters.294

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 295why, one step back
295Here an example for a ``--version`` flag:

Initial commitArmin Ronacherbefore adoption4101de3

line 296
296

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 297–298why, one step back
297.. click:example::
298

Initial commitArmin Ronacherbefore adoption4101de3

lines 299–301
299 def print_version(ctx, value):
300 if not value:301 return

Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3

line 302why, one step back
302 click.echo('Version 1.0')

Initial commitArmin Ronacherbefore adoption4101de3

lines 303–308
303 ctx.exit()
304305 @click.command()306 @click.option('--version', is_flag=True, callback=print_version,307 expose_value=False, is_eager=True)308 def hello():

Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3

line 309why, one step back
309 click.echo('Hello World!')

Initial commitArmin Ronacherbefore adoption4101de3

lines 310–314
310
311The `expose_value` parameter prevents the now pretty pointless ``version``312parameter to be passed to the callback. If that was not specified a313boolean would be passed to the `hello` script.314

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 315why, one step back
315What it looks like:

Initial commitArmin Ronacherbefore adoption4101de3

line 316
316

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 317why, one step back
317.. click:run::

Initial commitArmin Ronacherbefore adoption4101de3

line 318
318

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 319–321why, one step back
319 invoke(hello)
320 invoke(hello, args=['--version'])321

Initial commitArmin Ronacherbefore adoption4101de3

lines 322–327
322Yes Parameters
323--------------324325For dangerous operations it's very useful to be able to ask a user for326confirmation. This can be done by adding a boolean ``--yes`` flag and327asking for confirmation if the user did not provide it and to fail in a

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 328why, one step back
328callback:

Initial commitArmin Ronacherbefore adoption4101de3

line 329
329

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 330–331why, one step back
330.. click:example::
331

Initial commitArmin Ronacherbefore adoption4101de3

lines 332–340
332 def abort_if_false(ctx, value):
333 if not value:334 ctx.abort()335336 @click.command()337 @click.option('--yes', is_flag=True, callback=abort_if_false,338 expose_value=False,339 prompt='Are you sure you want to drop the db?')340 def dropdb():

Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3

line 341why, one step back
341 click.echo('Dropped all tables!')

Initial commitArmin Ronacherbefore adoption4101de3

line 342
342

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 343why, one step back
343And what it looks like on the command line:

Initial commitArmin Ronacherbefore adoption4101de3

line 344
344

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 345why, one step back
345.. click:run::

Initial commitArmin Ronacherbefore adoption4101de3

line 346
346

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 347–349why, one step back
347 invoke(dropdb, input=['n'])
348 invoke(dropdb, args=['--yes'])349

Initial commitArmin Ronacherbefore adoption4101de3

line 350
350Because this combination of parameters is quite common this can also be

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 351why, one step back
351replaced with the :func:`confirmation_option` decorator:

Initial commitArmin Ronacherbefore adoption4101de3

line 352
352

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 353–354why, one step back
353.. click:example::
354

Initial commitArmin Ronacherbefore adoption4101de3

lines 355–357
355 @click.command()
356 @click.confirmation_option('Are you sure you want to drop the db?')357 def dropdb():

Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3

line 358why, one step back
358 click.echo('Dropped all tables!')

Initial commitArmin Ronacherbefore adoption4101de3

lines 359–364
359
360Values from Environment Variables361---------------------------------362363A very useful feature of click is the ability to accept parameters from364environment variables in addition to regular parameters. This allows

Merge pull request #10 from epequeno/spellcheckArmin Ronacherbefore adoption6241146

line 365why, one step back
365tools to be automated much easier. For instance you might want to pass

Initial commitArmin Ronacherbefore adoption4101de3

lines 366–373
366the config file as ``--config`` parameter but also support exporting a
367``TOOL_CONFIG=hello.cfg`` key / value pair for a nicer development368experience.369370This is supported by click in two ways. One is to automatically build371environment variables which is supported for options only. To enable this372feature the ``auto_envvar_prefix`` parameter needs to be passed to the373script that is invoked. Each command and parameter is then added as

Merge pull request #21 from julen/doc-fixesArmin Ronacherbefore adoptiona4b7d70

line 374why, one step back
374underscore-separated variable in uppercase. So if you have a subcommand

Initial commitArmin Ronacherbefore adoption4101de3

lines 375–377
375called ``foo`` taking an option called ``bar`` and the prefix is
376``MY_TOOL`` then the variable is ``MY_TOOL_FOO_BAR``.377

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 378why, one step back
378Example usage:

Initial commitArmin Ronacherbefore adoption4101de3

line 379
379

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 380–381why, one step back
380.. click:example::
381

Initial commitArmin Ronacherbefore adoption4101de3

lines 382–384
382 @click.command()
383 @click.option('--username')384 def greet(username):

Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3

line 385why, one step back
385 click.echo('Hello %s!' % username)

Initial commitArmin Ronacherbefore adoption4101de3

lines 386–389
386
387 if __name__ == '__main__':388 greet(auto_envvar_prefix='GREETER')389

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 390why, one step back
390And from the command line:

Initial commitArmin Ronacherbefore adoption4101de3

line 391
391

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 392why, one step back
392.. click:run::

Initial commitArmin Ronacherbefore adoption4101de3

line 393
393

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 394–396why, one step back
394 invoke(greet, env={'GREETER_USERNAME': 'john'},
395 auto_envvar_prefix='GREETER')396

Initial commitArmin Ronacherbefore adoption4101de3

lines 397–399
397The second option is to manually pull values in from specific environment
398variables by defining the name of the environment variable on the option.399

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 400why, one step back
400Example usage:

Initial commitArmin Ronacherbefore adoption4101de3

line 401
401

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 402–403why, one step back
402.. click:example::
403

Initial commitArmin Ronacherbefore adoption4101de3

lines 404–406
404 @click.command()
405 @click.option('--username', envvar='USERNAME')406 def greet(username):

Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3

line 407why, one step back
407 click.echo('Hello %s!' % username)

Initial commitArmin Ronacherbefore adoption4101de3

lines 408–411
408
409 if __name__ == '__main__':410 greet()411

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 412why, one step back
412And from the command line:

Initial commitArmin Ronacherbefore adoption4101de3

line 413
413

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 414–416why, one step back
414.. click:run::
415416 invoke(greet, env={'USERNAME': 'john'})

Initial commitArmin Ronacherbefore adoption4101de3

lines 417–419
417
418In that case it can also be a list of different environment variables419where the first one is picked.

Documented alternative prefix charsArmin Ronacherbefore adoption521e9fc

lines 420–445why, one step back
420
421Other Prefix Characters422-----------------------423424Click can deal with alternative prefix characters, other than ``-`` for425options. This is for instance useful if you want to handle slashes as426parameters ``/`` or something similar. Note that this is strongly427discouraged in general because click wants developers to stay close to428POSIX semantics. However in certain situations this can be useful:429430.. click:example::431432 @click.command()433 @click.option('+w/-w')434 def chmod(w):435 click.echo('writable=%s' % w)436437 if __name__ == '__main__':438 chmod()439440And from the command line:441442.. click:run::443444 invoke(chmod, args=['+w'])445 invoke(chmod, args=['-w'])

Added count support and int rangesArmin Ronacherbefore adoption81930b5

lines 446–484why, one step back
446
447.. _ranges:448449Range Options450-------------451452A special mention should go to the :class:`IntRange` type which works very453similar to the :data:`INT` type but restricts the value to fall into a454specific range (inclusive on both edges). It has two modes:455456- the default mode (non clamping mode) where a value that falls outside457 of the range will cause an error.458- an optional clamping mode where a value that falls outside of the459 range will be clamped. This means that a range of ``0-5`` would460 return ``5`` for the value ``10`` or ``0`` for the value ``-1`` (for461 example).462463Example:464465.. click:example::466467 @click.command()468 @click.option('--count', type=click.IntRange(0, 20, clamp=True))469 @click.option('--digit', type=click.IntRange(0, 10))470 def repeat(count, digit):471 click.echo(str(digit) * count)472473 if __name__ == '__main__':474 repeat()475476And from the command line:477478.. click:run::479480 invoke(repeat, args=['--count=1000', '--digit=5'])481 invoke(repeat, args=['--count=1000', '--digit=12'])482483If you pass ``None`` for any of the edges it means that the range is open484at that side.