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 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 beMake documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 16why, one step back16:data:`STRING`. The name of the parameter is the longest option defined.17Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 18why, one step back18.. click:example::Initial commitArmin Ronacherbefore adoption4101de3
lines 19–241920 @click.command()21 @click.option('--n', default=1)22 def dots(n):23 print('.' * n)24Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 25why, one step back25And on the command line:26Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 27why, one step back27.. click:run::28Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 29–30why, one step back29 invoke(dots, args=['--n=2'])30Initial commitArmin Ronacherbefore adoption4101de3
lines 31–3831In this case the option is of type :data:`INT` because the default value32is 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 byMerge pull request #21 from julen/doc-fixesArmin Ronacherbefore adoptiona4b7d70
line 39why, one step back39the ``nargs`` parameter. The values are then stored as a tuple.40Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 41why, one step back41.. click:example::Initial commitArmin Ronacherbefore adoption4101de3
lines 42–474243 @click.command()44 @click.option('--pos', nargs=2, type=float)45 def findme(pos):46 print('%s / %s' % pos)47Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 48why, one step back48And on the command line:49Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 50why, one step back50.. click:run::51Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 52–53why, one step back52 invoke(findme, args=['--pos', '2.0', '3.0'])53Added an example for multipleArmin Ronacherbefore adoption98b7d1d
lines 54–62why, one step back54Multiple Options55----------------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:62Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 63why, one step back63Example:Added an example for multipleArmin Ronacherbefore adoption98b7d1d
line 64why, one step back64Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 65–66why, one step back65.. click:example::66Added an example for multipleArmin Ronacherbefore adoption98b7d1d
lines 67–71why, one step back67 @click.command()68 @click.option('--message', '-m', multiple=True)69 def commit(message):70 print('\n'.join(message))71Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 72why, one step back72And on the command line:Added an example for multipleArmin Ronacherbefore adoption98b7d1d
line 73why, one step back73Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 74why, one step back74.. click:run::Added an example for multipleArmin Ronacherbefore adoption98b7d1d
line 75why, one step back75Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 76–77why, one step back76 invoke(commit, args=['-m', 'foo', '-m', 'bar'])77Added some more statements on argparse and how verbosity can be implemented in click.Armin Ronacherbefore adoption84fbcb2
lines 78–83why, one step back78Counting79--------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 byMake documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 84why, one step back84using 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 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
lines 88–93why, one step back88 @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)93Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 94why, one step back94And 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 back95Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 96why, one step back96.. click:run::Added some more statements on argparse and how verbosity can be implemented in click.Armin Ronacherbefore adoption84fbcb2
line 97why, one step back97Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 98–99why, one step back98 invoke(log, args=['-vvv'])99Initial commitArmin Ronacherbefore adoption4101de3
lines 100–107100Boolean Flags101-------------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.107Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 108why, one step back108Example:109Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 110–111why, one step back110.. click:example::111Initial commitArmin Ronacherbefore adoption4101de3
lines 112–121112 import os113114 @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)121Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 122why, one step back122And on the command line:123Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 124why, one step back124.. click:run::125Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 126–128why, one step back126 invoke(info, args=['--shout'])127 invoke(info, args=['--no-shout'])128129If 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 130why, one step back130manually inform click that something is a flag:131Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 132–133why, one step back132.. click:example::133Initial commitArmin Ronacherbefore adoption4101de3
lines 134–143134 import os135136 @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)143Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 144why, one step back144And on the command line:145Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 146why, one step back146.. click:run::147Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 148–149why, one step back148 invoke(info, args=['--shout'])149Initial commitArmin Ronacherbefore adoption4101de3
lines 150–152150Feature Switches151----------------152Merge pull request #21 from julen/doc-fixesArmin Ronacherbefore adoptiona4b7d70
line 153why, one step back153In addition to boolean flags there are also feature switches. These are154implemented by setting multiple options to the same parameter name and byMerge pull request #10 from epequeno/spellcheckArmin Ronacherbefore adoption6241146
lines 155–156why, one step back155defining a flag value. To set a default flag assign a value of `True` to156the flag that should be the default.157Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 158why, one step back158.. click:example::Initial commitArmin Ronacherbefore adoption4101de3
lines 159–168159160 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)())168Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 169why, one step back169And on the command line:170Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 171why, one step back171.. click:run::172Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 173–176why, one step back173 invoke(info, args=['--upper'])174 invoke(info, args=['--lower'])175 invoke(info)176Added choice supportArmin Ronacherbefore adoption9eacc90
lines 177–185why, one step back177.. _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.185Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 186why, one step back186Example:Added choice supportArmin Ronacherbefore adoption9eacc90
line 187why, one step back187Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 188–189why, one step back188.. click:example::189Added choice supportArmin Ronacherbefore adoption9eacc90
lines 190–194why, one step back190 @click.command()191 @click.option('--hash-type', type=click.Choice(['md5', 'sha1']))192 def digest(hash_type):193 print(hash_type)194Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 195why, one step back195What it looks like:Added choice supportArmin Ronacherbefore adoption9eacc90
line 196why, one step back196Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 197why, one step back197.. click:run::Added choice supportArmin Ronacherbefore adoption9eacc90
line 198why, one step back198Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 199–203why, one step back199 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 back204Initial commitArmin Ronacherbefore adoption4101de3
lines 205–211205.. _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 implementedMerge pull request #21 from julen/doc-fixesArmin Ronacherbefore adoptiona4b7d70
line 212why, one step back212with click by defining a prompt string.213Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 214why, one step back214Example:215Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 216why, one step back216.. click:example::Initial commitArmin Ronacherbefore adoption4101de3
lines 217–222217218 @click.command()219 @click.option('--name', prompt=True)220 def hello(name):221 print('Hello %s!' % name)222Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 223why, one step back223And what it looks like:224Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 225why, one step back225.. click:run::226Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 227–229why, one step back227 invoke(hello, args=['--name=John'])228 invoke(hello, input=['John'])229230If 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 231why, one step back231a different one:232Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 233why, one step back233.. click:example::Initial commitArmin Ronacherbefore adoption4101de3
lines 234–239234235 @click.command()236 @click.option('--name', prompt='Your name please')237 def hello(name):238 print('Hello %s!' % name)239Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 240why, one step back240What it looks like:241Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 242why, one step back242.. click:run::243Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 244–245why, one step back244 invoke(hello, input=['John'])245Initial commitArmin Ronacherbefore adoption4101de3
lines 246–249246Password Prompts247----------------248249Click also supports hidden prompts and asking for confirmation. This isMake documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 250why, one step back250useful for password input:251Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 252–253why, one step back252.. click:example::253Initial commitArmin Ronacherbefore adoption4101de3
lines 254–259254 @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'))259Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 260why, one step back260What it looks like:261Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 262why, one step back262.. click:run::263Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 264–265why, one step back264 invoke(encrypt, input=['secret', 'secret'])265266Because this combination of parameters is quite common this can also beMake documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 267why, one step back267replaced with the :func:`password_option` decorator:268Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 269–270why, one step back269.. click:example::270Initial commitArmin Ronacherbefore adoption4101de3
lines 271–280271 @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 back281parameter that prints out the version and then exits the application.Initial commitArmin Ronacherbefore adoption4101de3
lines 282–286282283In 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 anMerge pull request #11 from yelite/typo-fixArmin Ronacherbefore adoption9c8a3eb
line 287why, one step back287error message. For instance if ``--version`` was not eager and aInitial commitArmin Ronacherbefore adoption4101de3
lines 288–295288parameter ``--foo`` was required and defined before, you would need to289specify 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.295Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 296why, one step back296Here an example for a ``--version`` flag:297Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 298–299why, one step back298.. click:example::299Initial commitArmin Ronacherbefore adoption4101de3
lines 300–315300 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.315Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 316why, one step back316What it looks like:317Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 318why, one step back318.. click:run::319Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 320–322why, one step back320 invoke(hello)321 invoke(hello, args=['--version'])322Initial commitArmin Ronacherbefore adoption4101de3
lines 323–328323Yes Parameters324--------------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 aMake documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 329why, one step back329callback:330Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 331–332why, one step back331.. click:example::332Initial commitArmin Ronacherbefore adoption4101de3
lines 333–343333 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!')343Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 344why, one step back344And what it looks like on the command line:345Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 346why, one step back346.. click:run::347Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 348–350why, one step back348 invoke(dropdb, input=['n'])349 invoke(dropdb, args=['--yes'])350351Because this combination of parameters is quite common this can also beMake documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 352why, one step back352replaced with the :func:`confirmation_option` decorator:353Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 354–355why, one step back354.. click:example::355Initial commitArmin Ronacherbefore adoption4101de3
lines 356–365356 @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 allowsMerge pull request #10 from epequeno/spellcheckArmin Ronacherbefore adoption6241146
line 366why, one step back366tools to be automated much easier. For instance you might want to passInitial commitArmin Ronacherbefore adoption4101de3
lines 367–374367the config file as ``--config`` parameter but also support exporting a368``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 asMerge pull request #21 from julen/doc-fixesArmin Ronacherbefore adoptiona4b7d70
line 375why, one step back375underscore-separated variable in uppercase. So if you have a subcommandInitial commitArmin Ronacherbefore adoption4101de3
lines 376–378376called ``foo`` taking an option called ``bar`` and the prefix is377``MY_TOOL`` then the variable is ``MY_TOOL_FOO_BAR``.378Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 379why, one step back379Example usage:380Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 381–382why, one step back381.. click:example::382Initial commitArmin Ronacherbefore adoption4101de3
lines 383–390383 @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')390Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 391why, one step back391And from the command line:392Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 393why, one step back393.. click:run::394Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 395–397why, one step back395 invoke(greet, env={'GREETER_USERNAME': 'john'},396 auto_envvar_prefix='GREETER')397Initial commitArmin Ronacherbefore adoption4101de3
lines 398–400398The second option is to manually pull values in from specific environment399variables by defining the name of the environment variable on the option.400Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 401why, one step back401Example usage:402Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 403–404why, one step back403.. click:example::404Initial commitArmin Ronacherbefore adoption4101de3
lines 405–412405 @click.command()406 @click.option('--username', envvar='USERNAME')407 def greet(username):408 print('Hello %s!' % username)409410 if __name__ == '__main__':411 greet()412Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 413why, one step back413And from the command line:414Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 415–417why, one step back415.. click:run::416417 invoke(greet, env={'USERNAME': 'john'})Initial commitArmin Ronacherbefore adoption4101de3
lines 418–420418419In that case it can also be a list of different environment variables420where the first one is picked.