Skip to content
Myr

options.rst

This file

Path

docs/options.rst

420 lines · 10.8 KiB

Last changed by

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

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

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

line 16why, one step back
16:data:`STRING`. The name of the parameter is the longest option defined.

Initial commitArmin Ronacherbefore adoption4101de3

line 17
17

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

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

Initial commitArmin Ronacherbefore adoption4101de3

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

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 26
26

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 28
28

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

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

Initial commitArmin Ronacherbefore adoption4101de3

lines 31–38
31In this case the option is of type :data:`INT` because the default value
32is an integer.3334Multi Value Options35-------------------3637Sometimes you have options that take more than one argument. For options38only a fixed number of arguments is supported. This can be configured by

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 40
40

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

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

Initial commitArmin Ronacherbefore adoption4101de3

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

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 49
49

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 51
51

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

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

Added an example for multipleArmin Ronacherbefore adoption98b7d1d

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

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

line 63why, one step back
63Example:

Added an example for multipleArmin Ronacherbefore adoption98b7d1d

line 64why, one step back
64

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

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

Added an example for multipleArmin Ronacherbefore adoption98b7d1d

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

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

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

Added an example for multipleArmin Ronacherbefore adoption98b7d1d

line 73why, one step back
73

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

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

Added an example for multipleArmin Ronacherbefore adoption98b7d1d

line 75why, one step back
75

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

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

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

lines 78–83why, one step back
78Counting
79--------8081If you have used ``optparse`` or ``argparse`` before you might be missing82support for counting. This is a very rarely useful feature, usually only83useful for implementing verbosity flags. It can however be emulated by

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

line 84why, one step back
84using the multiple flag and taking the length of the end result:

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

lines 88–93why, one step back
88 @click.command()
89 @click.option('-v', '--verbose', is_flag=True, multiple=True)90 def log(verbose):91 verbosity = len(verbose)92 print('Verbosity: %s' % verbosity)93

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

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

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

line 95why, one step back
95

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

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

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

line 97why, one step back
97

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

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

Initial commitArmin Ronacherbefore adoption4101de3

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

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

line 108why, one step back
108Example:

Initial commitArmin Ronacherbefore adoption4101de3

line 109
109

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

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

Initial commitArmin Ronacherbefore adoption4101de3

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

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 123
123

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 125
125

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 129
129If 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 130why, one step back
130manually inform click that something is a flag:

Initial commitArmin Ronacherbefore adoption4101de3

line 131
131

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

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

Initial commitArmin Ronacherbefore adoption4101de3

lines 134–143
134 import os
135136 @click.command()137 @click.option('--shout', is_flag=True)138 def info(shout):139 rv = os.uname()[0]140 if shout:141 rv = rv.upper() + '!!!!111'142 print(rv)143

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 145
145

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 147
147

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

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

Initial commitArmin Ronacherbefore adoption4101de3

lines 150–152
150Feature Switches
151----------------152

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

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

Initial commitArmin Ronacherbefore adoption4101de3

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

Merge pull request #10 from epequeno/spellcheckArmin Ronacherbefore adoption6241146

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

Initial commitArmin Ronacherbefore adoption4101de3

line 157
157

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

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

Initial commitArmin Ronacherbefore adoption4101de3

lines 159–168
159
160 import os161162 @click.command()163 @click.option('--upper', 'transformation', flag_value='upper',164 default=True)165 @click.option('--lower', 'transformation', flag_value='lower')166 def info(transformation):167 print(getattr(os.uname()[0], transformation)())168

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 170
170

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 172
172

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

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

Added choice supportArmin Ronacherbefore adoption9eacc90

lines 177–185why, one step back
177.. _choice-opts:
178179Choice Options180--------------181182Sometimes you want to have a parameter be a choice of a list of values.183In that case you can use :class:`Choice` type. It can be instanciated184with a list of valid values.185

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

line 186why, one step back
186Example:

Added choice supportArmin Ronacherbefore adoption9eacc90

line 187why, one step back
187

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

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

Added choice supportArmin Ronacherbefore adoption9eacc90

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

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

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

Added choice supportArmin Ronacherbefore adoption9eacc90

line 196why, one step back
196

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

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

Added choice supportArmin Ronacherbefore adoption9eacc90

line 198why, one step back
198

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

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

Added choice supportArmin Ronacherbefore adoption9eacc90

line 204why, one step back
204

Initial commitArmin Ronacherbefore adoption4101de3

lines 205–211
205.. _option-prompting:
206207Prompting208---------209210Sometimes you want parameters that can either be provided from the command211line 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 212why, one step back
212with click by defining a prompt string.

Initial commitArmin Ronacherbefore adoption4101de3

line 213
213

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

line 214why, one step back
214Example:

Initial commitArmin Ronacherbefore adoption4101de3

line 215
215

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

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

Initial commitArmin Ronacherbefore adoption4101de3

lines 217–222
217
218 @click.command()219 @click.option('--name', prompt=True)220 def hello(name):221 print('Hello %s!' % name)222

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 224
224

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 226
226

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 230
230If 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 231why, one step back
231a different one:

Initial commitArmin Ronacherbefore adoption4101de3

line 232
232

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

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

Initial commitArmin Ronacherbefore adoption4101de3

lines 234–239
234
235 @click.command()236 @click.option('--name', prompt='Your name please')237 def hello(name):238 print('Hello %s!' % name)239

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 241
241

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 243
243

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

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

Initial commitArmin Ronacherbefore adoption4101de3

lines 246–249
246Password Prompts
247----------------248249Click also supports hidden prompts and asking for confirmation. This is

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 251
251

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

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

Initial commitArmin Ronacherbefore adoption4101de3

lines 254–259
254 @click.command()
255 @click.option('--password', prompt=True, hide_input=True,256 confirmation_prompt=True)257 def encrypt(password):258 print('Encrypting password to %s' % password.encode('rot13'))259

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 261
261

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 263
263

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

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

Initial commitArmin Ronacherbefore adoption4101de3

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

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 268
268

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

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

Initial commitArmin Ronacherbefore adoption4101de3

lines 271–280
271 @click.command()
272 @click.password_option()273 def encrypt(password):274 print('Encrypting password to %s' % password.encode('rot13'))275276Callbacks and Eager Options277---------------------------278279Sometimes you want a parameter to completely change the execution flow.280This for instance is the case when you want to have a ``--version``

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

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

Initial commitArmin Ronacherbefore adoption4101de3

lines 282–286
282
283In such cases you need two concepts: eager parameters and a callback. An284eager parameter is a parameter that is handled before others and a285callback is what executes after the parameter is handled. The eagerness286is necessary so that an earlier required parameter does not produce an

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

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

Initial commitArmin Ronacherbefore adoption4101de3

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

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 297
297

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

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

Initial commitArmin Ronacherbefore adoption4101de3

lines 300–315
300 def print_version(ctx, value):
301 if not value:302 return303 print('Version 1.0')304 ctx.exit()305306 @click.command()307 @click.option('--version', is_flag=True, callback=print_version,308 expose_value=False, is_eager=True)309 def hello():310 print('Hello World!')311312The `expose_value` parameter prevents the now pretty pointless ``version``313parameter to be passed to the callback. If that was not specified a314boolean would be passed to the `hello` script.315

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 317
317

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 319
319

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

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

Initial commitArmin Ronacherbefore adoption4101de3

lines 323–328
323Yes Parameters
324--------------325326For dangerous operations it's very useful to be able to ask a user for327confirmation. This can be done by adding a boolean ``--yes`` flag and328asking 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 329why, one step back
329callback:

Initial commitArmin Ronacherbefore adoption4101de3

line 330
330

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

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

Initial commitArmin Ronacherbefore adoption4101de3

lines 333–343
333 def abort_if_false(ctx, value):
334 if not value:335 ctx.abort()336337 @click.command()338 @click.option('--yes', is_flag=True, callback=abort_if_false,339 expose_value=False,340 prompt='Are you sure you want to drop the db?')341 def dropdb():342 print('Dropped all tables!')343

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 345
345

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 347
347

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

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

Initial commitArmin Ronacherbefore adoption4101de3

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

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 353
353

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

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

Initial commitArmin Ronacherbefore adoption4101de3

lines 356–365
356 @click.command()
357 @click.confirmation_option('Are you sure you want to drop the db?')358 def dropdb():359 print('Dropped all tables!')360361Values from Environment Variables362---------------------------------363364A very useful feature of click is the ability to accept parameters from365environment variables in addition to regular parameters. This allows

Merge pull request #10 from epequeno/spellcheckArmin Ronacherbefore adoption6241146

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

Initial commitArmin Ronacherbefore adoption4101de3

lines 367–374
367the config file as ``--config`` parameter but also support exporting a
368``TOOL_CONFIG=hello.cfg`` key / value pair for a nicer development369experience.370371This is supported by click in two ways. One is to automatically build372environment variables which is supported for options only. To enable this373feature the ``auto_envvar_prefix`` parameter needs to be passed to the374script that is invoked. Each command and parameter is then added as

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

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

Initial commitArmin Ronacherbefore adoption4101de3

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

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

line 379why, one step back
379Example usage:

Initial commitArmin Ronacherbefore adoption4101de3

line 380
380

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

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

Initial commitArmin Ronacherbefore adoption4101de3

lines 383–390
383 @click.command()
384 @click.option('--username')385 def greet(username):386 print('Hello %s!' % username)387388 if __name__ == '__main__':389 greet(auto_envvar_prefix='GREETER')390

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 392
392

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 394
394

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

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

Initial commitArmin Ronacherbefore adoption4101de3

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

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

line 401why, one step back
401Example usage:

Initial commitArmin Ronacherbefore adoption4101de3

line 402
402

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

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

Initial commitArmin Ronacherbefore adoption4101de3

lines 405–412
405 @click.command()
406 @click.option('--username', envvar='USERNAME')407 def greet(username):408 print('Hello %s!' % username)409410 if __name__ == '__main__':411 greet()412

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 414
414

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

lines 415–417why, one step back
415.. click:run::
416417 invoke(greet, env={'USERNAME': 'john'})

Initial commitArmin Ronacherbefore adoption4101de3

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