arguments.rst
This file
Path
docs/arguments.rst
158 lines · 4.7 KiB
Last changed by
Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4
History of this fileTake it
The walk stopped after 200 commits: lines older than 55b2715 point at it.
Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715
lines 1–7why, one step back1.. _arguments:23Arguments4=========56.. currentmodule:: click7Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4
line 8why, one step back8Arguments are:Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715
line 9why, one step back9Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4
lines 10–18why, one step back10* Are positional in nature.11* Similar to a limited version of :ref:`options <options>` that can take an arbitrary number of inputs12* :ref:`Documented manually <documenting-arguments>`.1314Useful and often used kwargs are:1516* ``default``: Passes a default.17* ``nargs``: Sets the number of arguments. Set to -1 to take an arbitrary number.18Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715
lines 19–21why, one step back19Basic Arguments20---------------21Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4
line 22why, one step back22A minimal :class:`click.Argument` solely takes one string argument: the name of the argument. This will assume the argument is required, has no default, and is of the type ``str``.Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715
lines 23–29why, one step back2324Example:2526.. click:example::2728 @click.command()29 @click.argument('filename')Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4
line 30why, one step back30 def touch(filename: str):Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715
lines 31–33why, one step back31 """Print FILENAME."""32 click.echo(filename)33Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4
line 34why, one step back34And from the command line:Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715
lines 35–40why, one step back3536.. click:run::3738 invoke(touch, args=['foo.txt'])3940Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4
line 41why, one step back41An argument may be assigned a :ref:`parameter type <parameter-types>`. If no type is provided, the type of the default value is used. If no default value is provided, the type is assumed to be :data:`STRING`.Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715
line 42why, one step back42Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4
line 43why, one step back43.. admonition:: Note on Required ArgumentsMerge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715
line 44why, one step back44Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4
line 45why, one step back45 It is possible to make an argument required by setting ``required=True``. It is not recommended since we think command line tools should gracefully degrade into becoming no ops. We think this because command line tools are often invoked with wildcard inputs and they should not error out if the wildcard is empty.Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715
line 46why, one step back46Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4
lines 47–51why, one step back47Multiple Arguments48-----------------------------------4950To set the number of argument use the ``nargs`` kwarg. It can be set to any positive integer and -1. Setting it to -1, makes the number of arguments arbitrary (which is called variadic) and can only be used once. The arguments are then packed as a tuple and passed to the function.51Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715
lines 52–54why, one step back52.. click:example::5354 @click.command()Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4
lines 55–57why, one step back55 @click.argument('src', nargs=1)56 @click.argument('dsts', nargs=-1)57 def copy(src: str, dsts: tuple[str, ...]):Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715
line 58why, one step back58 """Move file SRC to DST."""Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4
lines 59–60why, one step back59 for destination in dsts:60 click.echo(f"Copy {src} to folder {destination}")Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715
line 61why, one step back61Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4
line 62why, one step back62And from the command line:Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715
lines 63–65why, one step back6364.. click:run::65Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4
line 66why, one step back66 invoke(copy, args=['foo.txt', 'usr/david/foo.txt', 'usr/mitsuko/foo.txt'])Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715
line 67why, one step back67Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4
line 68why, one step back68.. admonition:: Note on Handling FilesMerge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715
line 69why, one step back69Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4
line 70why, one step back70 This is not how you should handle files and files paths. This merely used as a simple example. See :ref:`handling-files` to learn more about how to handle files in parameters.Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715
line 71why, one step back71Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4
lines 72–73why, one step back72Argument Escape Sequences73---------------------------Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715
line 74why, one step back74Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4
line 75why, one step back75If you want to process arguments that look like options, like a file named ``-foo.txt`` or ``--foo.txt`` , you must pass the ``--`` separator first. After you pass the ``--``, you may only pass arguments. This is a common feature for POSIX command line tools.Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715
line 76why, one step back76Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4
line 77why, one step back77Example usage:Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715
lines 78–81why, one step back7879.. click:example::8081 @click.command()Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4
lines 82–86why, one step back82 @click.argument('files', nargs=-1, type=click.Path())83 def touch(files):84 """Print all FILES file names."""85 for filename in files:86 click.echo(filename)Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715
line 87why, one step back87Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4
line 88why, one step back88And from the command line:Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715
lines 89–91why, one step back8990.. click:run::91Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4
line 92why, one step back92 invoke(touch, ['--', '-foo.txt', 'bar.txt'])Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715
line 93why, one step back93Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4
line 94why, one step back94If you don't like the ``--`` marker, you can set ignore_unknown_options to True to avoid checking unknown options:Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715
lines 95–97why, one step back9596.. click:example::97Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4
lines 98–103why, one step back98 @click.command(context_settings={"ignore_unknown_options": True})99 @click.argument('files', nargs=-1, type=click.Path())100 def touch(files):101 """Print all FILES file names."""102 for filename in files:103 click.echo(filename)Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715
line 104why, one step back104Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4
line 105why, one step back105And from the command line:Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715
lines 106–108why, one step back106107.. click:run::108Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4
line 109why, one step back109 invoke(touch, ['-foo.txt', 'bar.txt'])Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715
lines 110–111why, one step back110111Merge stable into main (#2799)Rowlando13before adoptiond791537
lines 112–113why, one step back112.. _environment-variables:113Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715
lines 114–116why, one step back114Environment Variables115---------------------116Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4
line 117why, one step back117Arguments can use environment variables. To do so, pass the name(s) of the environment variable(s) via `envvar` in ``click.argument``.Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715
line 118why, one step back118Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4
line 119why, one step back119Checking one environment variable:Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715
lines 120–133why, one step back120121.. click:example::122123 @click.command()124 @click.argument('src', envvar='SRC', type=click.File('r'))125 def echo(src):126 """Print value of SRC environment variable."""127 click.echo(src.read())128129And from the command line:130131.. click:run::132133 with isolated_filesystem():Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4
line 134why, one step back134 # Writing the file in the filesystem.Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715
lines 135–139why, one step back135 with open('hello.txt', 'w') as f:136 f.write('Hello World!')137 invoke(echo, env={'SRC': 'hello.txt'})138139Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4
line 140why, one step back140Checking multiple environment variables:Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715
lines 141–144why, one step back141142.. click:example::143144 @click.command()Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4
lines 145–148why, one step back145 @click.argument('src', envvar=['SRC', 'SRC_2'], type=click.File('r'))146 def echo(src):147 """Print value of SRC environment variable."""148 click.echo(src.read())Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715
lines 149–153why, one step back149150And from the command line:151152.. click:run::153Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4
lines 154–158why, one step back154 with isolated_filesystem():155 # Writing the file in the filesystem.156 with open('hello.txt', 'w') as f:157 f.write('Hello World from second variable!')158 invoke(echo, env={'SRC_2': 'hello.txt'})