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 fileTake it
1Options2=======34.. currentmodule:: click56Adding options to commands can be accomplished by the :func:`option`7decorator. Since options can come in various different versions thereMerge pull request #10 from epequeno/spellcheckArmin Ronacherbefore adoption6241146
line 8why, one step back8are a ton of parameters to configure their behavior.Initial commitArmin Ronacherbefore adoption4101de3
lines 9–15910Basic 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 beAlways pick first optoin instead of longestArmin Ronacherbefore adoptionb8e766f
lines 16–17why, one step back16:data:`STRING`. The name of the parameter is by default the first long17option defined, otherwise the first short one.18Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 19why, one step back19.. click:example::Initial commitArmin Ronacherbefore adoption4101de3
lines 20–232021 @click.command()22 @click.option('--n', default=1)23 def dots(n):Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3
line 24why, one step back24 click.echo('.' * n)25Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 26why, one step back26And on the command line:27Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 28why, one step back28.. click:run::29Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 30–31why, one step back30 invoke(dots, args=['--n=2'])31Initial commitArmin Ronacherbefore adoption4101de3
lines 32–3932In this case the option is of type :data:`INT` because the default value33is 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 byMerge pull request #21 from julen/doc-fixesArmin Ronacherbefore adoptiona4b7d70
line 40why, one step back40the ``nargs`` parameter. The values are then stored as a tuple.41Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 42why, one step back42.. click:example::Initial commitArmin Ronacherbefore adoption4101de3
lines 43–464344 @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 back47 click.echo('%s / %s' % pos)48Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 49why, one step back49And on the command line:50Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 51why, one step back51.. click:run::52Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 53–54why, one step back53 invoke(findme, args=['--pos', '2.0', '3.0'])54Added an example for multipleArmin Ronacherbefore adoption98b7d1d
lines 55–63why, one step back55Multiple Options56----------------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:63Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 64why, one step back64Example:Added an example for multipleArmin Ronacherbefore adoption98b7d1d
line 65why, one step back65Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 66–67why, one step back66.. click:example::67Added an example for multipleArmin Ronacherbefore adoption98b7d1d
lines 68–70why, one step back68 @click.command()69 @click.option('--message', '-m', multiple=True)70 def commit(message):Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3
line 71why, one step back71 click.echo('\n'.join(message))Added an example for multipleArmin Ronacherbefore adoption98b7d1d
line 72why, one step back72Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 73why, one step back73And on the command line:Added an example for multipleArmin Ronacherbefore adoption98b7d1d
line 74why, one step back74Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 75why, one step back75.. click:run::Added an example for multipleArmin Ronacherbefore adoption98b7d1d
line 76why, one step back76Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 77–78why, one step back77 invoke(commit, args=['-m', 'foo', '-m', 'bar'])78Added some more statements on argparse and how verbosity can be implemented in click.Armin Ronacherbefore adoption84fbcb2
lines 79–81why, one step back79Counting80--------81Added count support and int rangesArmin Ronacherbefore adoption81930b5
lines 82–84why, one step back82In some very rare circumstances it's interesting to use the repeating of83options 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 back85Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 86–87why, one step back86.. click:example::87Added some more statements on argparse and how verbosity can be implemented in click.Armin Ronacherbefore adoption84fbcb2
line 88why, one step back88 @click.command()Added count support and int rangesArmin Ronacherbefore adoption81930b5
line 89why, one step back89 @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 back90 def log(verbose):Added count support and int rangesArmin Ronacherbefore adoption81930b5
line 91why, one step back91 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 back92Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 93why, one step back93And 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 back94Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 95why, one step back95.. click:run::Added some more statements on argparse and how verbosity can be implemented in click.Armin Ronacherbefore adoption84fbcb2
line 96why, one step back96Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 97–98why, one step back97 invoke(log, args=['-vvv'])98Initial commitArmin Ronacherbefore adoption4101de3
lines 99–10699Boolean Flags100-------------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.106Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 107why, one step back107Example:108Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 109–110why, one step back109.. click:example::110Initial commitArmin Ronacherbefore adoption4101de3
lines 111–118111 import os112113 @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 back119 click.echo(rv)120Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 121why, one step back121And on the command line:122Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 123why, one step back123.. click:run::124Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 125–127why, one step back125 invoke(info, args=['--shout'])126 invoke(info, args=['--no-shout'])127128If you really don't want an off-switch you can just define one andMake documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 129why, one step back129manually inform click that something is a flag:130Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 131–132why, one step back131.. click:example::132Initial commitArmin Ronacherbefore adoption4101de3
lines 133–140133 import os134135 @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 back141 click.echo(rv)142Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 143why, one step back143And on the command line:144Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 145why, one step back145.. click:run::146Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 147–148why, one step back147 invoke(info, args=['--shout'])148Initial commitArmin Ronacherbefore adoption4101de3
lines 149–151149Feature Switches150----------------151Merge pull request #21 from julen/doc-fixesArmin Ronacherbefore adoptiona4b7d70
line 152why, one step back152In addition to boolean flags there are also feature switches. These are153implemented by setting multiple options to the same parameter name and byMerge pull request #10 from epequeno/spellcheckArmin Ronacherbefore adoption6241146
lines 154–155why, one step back154defining a flag value. To set a default flag assign a value of `True` to155the flag that should be the default.156Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 157why, one step back157.. click:example::Initial commitArmin Ronacherbefore adoption4101de3
lines 158–165158159 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 back166 click.echo(getattr(os.uname()[0], transformation)())167Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 168why, one step back168And on the command line:169Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 170why, one step back170.. click:run::171Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 172–175why, one step back172 invoke(info, args=['--upper'])173 invoke(info, args=['--lower'])174 invoke(info)175Added choice supportArmin Ronacherbefore adoption9eacc90
lines 176–181why, one step back176.. _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 back182In that case you can use :class:`Choice` type. It can be instantiatedAdded choice supportArmin Ronacherbefore adoption9eacc90
lines 183–184why, one step back183with a list of valid values.184Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 185why, one step back185Example:Added choice supportArmin Ronacherbefore adoption9eacc90
line 186why, one step back186Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 187–188why, one step back187.. click:example::188Added choice supportArmin Ronacherbefore adoption9eacc90
lines 189–191why, one step back189 @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 back192 click.echo(hash_type)Added choice supportArmin Ronacherbefore adoption9eacc90
line 193why, one step back193Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 194why, one step back194What it looks like:Added choice supportArmin Ronacherbefore adoption9eacc90
line 195why, one step back195Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 196why, one step back196.. click:run::Added choice supportArmin Ronacherbefore adoption9eacc90
line 197why, one step back197Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 198–202why, one step back198 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 back203Initial commitArmin Ronacherbefore adoption4101de3
lines 204–210204.. _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 implementedMerge pull request #21 from julen/doc-fixesArmin Ronacherbefore adoptiona4b7d70
line 211why, one step back211with click by defining a prompt string.212Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 213why, one step back213Example:214Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 215why, one step back215.. click:example::Initial commitArmin Ronacherbefore adoption4101de3
lines 216–219216217 @click.command()218 @click.option('--name', prompt=True)219 def hello(name):Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3
line 220why, one step back220 click.echo('Hello %s!' % name)221Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 222why, one step back222And what it looks like:223Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 224why, one step back224.. click:run::225Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 226–228why, one step back226 invoke(hello, args=['--name=John'])227 invoke(hello, input=['John'])228229If you are not happy with the default prompt string you can ask forMake documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 230why, one step back230a different one:231Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 232why, one step back232.. click:example::Initial commitArmin Ronacherbefore adoption4101de3
lines 233–236233234 @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 back237 click.echo('Hello %s!' % name)238Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 239why, one step back239What it looks like:240Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 241why, one step back241.. click:run::242Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 243–244why, one step back243 invoke(hello, input=['John'])244Initial commitArmin Ronacherbefore adoption4101de3
lines 245–248245Password Prompts246----------------247248Click also supports hidden prompts and asking for confirmation. This isMake documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 249why, one step back249useful for password input:250Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 251–252why, one step back251.. click:example::252Initial commitArmin Ronacherbefore adoption4101de3
lines 253–256253 @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 back257 click.echo('Encrypting password to %s' % password.encode('rot13'))258Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 259why, one step back259What it looks like:260Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 261why, one step back261.. click:run::262Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 263–264why, one step back263 invoke(encrypt, input=['secret', 'secret'])264265Because this combination of parameters is quite common this can also beMake documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 266why, one step back266replaced with the :func:`password_option` decorator:267Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 268–269why, one step back268.. click:example::269Initial commitArmin Ronacherbefore adoption4101de3
lines 270–272270 @click.command()271 @click.password_option()272 def encrypt(password):Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3
line 273why, one step back273 click.echo('Encrypting password to %s' % password.encode('rot13'))Initial commitArmin Ronacherbefore adoption4101de3
lines 274–279274275Callbacks 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 back280parameter that prints out the version and then exits the application.Initial commitArmin Ronacherbefore adoption4101de3
lines 281–285281282In 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 anMerge pull request #11 from yelite/typo-fixArmin Ronacherbefore adoption9c8a3eb
line 286why, one step back286error message. For instance if ``--version`` was not eager and aInitial commitArmin Ronacherbefore adoption4101de3
lines 287–294287parameter ``--foo`` was required and defined before, you would need to288specify 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.294Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 295why, one step back295Here an example for a ``--version`` flag:296Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 297–298why, one step back297.. click:example::298Initial commitArmin Ronacherbefore adoption4101de3
lines 299–301299 def print_version(ctx, value):300 if not value:301 returnImplemented and exposed click.echoArmin Ronacherbefore adoption95c24b3
line 302why, one step back302 click.echo('Version 1.0')Initial commitArmin Ronacherbefore adoption4101de3
lines 303–308303 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 back309 click.echo('Hello World!')Initial commitArmin Ronacherbefore adoption4101de3
lines 310–314310311The `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.314Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 315why, one step back315What it looks like:316Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 317why, one step back317.. click:run::318Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 319–321why, one step back319 invoke(hello)320 invoke(hello, args=['--version'])321Initial commitArmin Ronacherbefore adoption4101de3
lines 322–327322Yes Parameters323--------------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 aMake documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 328why, one step back328callback:329Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 330–331why, one step back330.. click:example::331Initial commitArmin Ronacherbefore adoption4101de3
lines 332–340332 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 back341 click.echo('Dropped all tables!')342Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 343why, one step back343And what it looks like on the command line:344Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 345why, one step back345.. click:run::346Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 347–349why, one step back347 invoke(dropdb, input=['n'])348 invoke(dropdb, args=['--yes'])349350Because this combination of parameters is quite common this can also beMake documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 351why, one step back351replaced with the :func:`confirmation_option` decorator:352Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 353–354why, one step back353.. click:example::354Initial commitArmin Ronacherbefore adoption4101de3
lines 355–357355 @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 back358 click.echo('Dropped all tables!')Initial commitArmin Ronacherbefore adoption4101de3
lines 359–364359360Values from Environment Variables361---------------------------------362363A very useful feature of click is the ability to accept parameters from364environment variables in addition to regular parameters. This allowsMerge pull request #10 from epequeno/spellcheckArmin Ronacherbefore adoption6241146
line 365why, one step back365tools to be automated much easier. For instance you might want to passInitial commitArmin Ronacherbefore adoption4101de3
lines 366–373366the config file as ``--config`` parameter but also support exporting a367``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 asMerge pull request #21 from julen/doc-fixesArmin Ronacherbefore adoptiona4b7d70
line 374why, one step back374underscore-separated variable in uppercase. So if you have a subcommandInitial commitArmin Ronacherbefore adoption4101de3
lines 375–377375called ``foo`` taking an option called ``bar`` and the prefix is376``MY_TOOL`` then the variable is ``MY_TOOL_FOO_BAR``.377Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 378why, one step back378Example usage:379Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 380–381why, one step back380.. click:example::381Initial commitArmin Ronacherbefore adoption4101de3
lines 382–384382 @click.command()383 @click.option('--username')384 def greet(username):Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3
line 385why, one step back385 click.echo('Hello %s!' % username)Initial commitArmin Ronacherbefore adoption4101de3
lines 386–389386387 if __name__ == '__main__':388 greet(auto_envvar_prefix='GREETER')389Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 390why, one step back390And from the command line:391Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 392why, one step back392.. click:run::393Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 394–396why, one step back394 invoke(greet, env={'GREETER_USERNAME': 'john'},395 auto_envvar_prefix='GREETER')396Initial commitArmin Ronacherbefore adoption4101de3
lines 397–399397The second option is to manually pull values in from specific environment398variables by defining the name of the environment variable on the option.399Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 400why, one step back400Example usage:401Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 402–403why, one step back402.. click:example::403Initial commitArmin Ronacherbefore adoption4101de3
lines 404–406404 @click.command()405 @click.option('--username', envvar='USERNAME')406 def greet(username):Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3
line 407why, one step back407 click.echo('Hello %s!' % username)Initial commitArmin Ronacherbefore adoption4101de3
lines 408–411408409 if __name__ == '__main__':410 greet()411Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 412why, one step back412And from the command line:413Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 414–416why, one step back414.. click:run::415416 invoke(greet, env={'USERNAME': 'john'})Initial commitArmin Ronacherbefore adoption4101de3
lines 417–419417418In 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 back420421Other 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 back446447.. _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.