rename extension from .rst to .md
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Jaron Rosenau · committed by Jaron Rosenau · parent 781d6a8 · Browse files · 2 files·+158 added−158 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+158 added−158 removed
- other:+158 added−158 removed
Provenance
- Commit
5aad9b6- Tree
70b2eaf- Author
Jaron Rosenau <jaron.rosenau@gmail.com>- Committer
Jaron Rosenau <jaron.rosenau@gmail.com>- Parent
781d6a8
2 files changed+158 added−158 removed
added:docs/arguments.md+158 added
1+.. _arguments:2+3+Arguments4+=========5+6+.. currentmodule:: click7+8+Arguments are:9+10+* 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>`.13+14+Useful and often used kwargs are:15+16+* ``default``: Passes a default.17+* ``nargs``: Sets the number of arguments. Set to -1 to take an arbitrary number.18+19+Basic Arguments20+---------------21+22+A 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``.23+24+Example:25+26+.. click:example::27+28+ @click.command()29+ @click.argument('filename')30+ def touch(filename: str):31+ """Print FILENAME."""32+ click.echo(filename)33+34+And from the command line:35+36+.. click:run::37+38+ invoke(touch, args=['foo.txt'])39+40+41+An 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`.42+43+.. admonition:: Note on Required Arguments44+45+ 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.46+47+Multiple Arguments48+-----------------------------------49+50+To 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.51+52+.. click:example::53+54+ @click.command()55+ @click.argument('src', nargs=1)56+ @click.argument('dsts', nargs=-1)57+ def copy(src: str, dsts: tuple[str, ...]):58+ """Move file SRC to DST."""59+ for destination in dsts:60+ click.echo(f"Copy {src} to folder {destination}")61+62+And from the command line:63+64+.. click:run::65+66+ invoke(copy, args=['foo.txt', 'usr/david/foo.txt', 'usr/mitsuko/foo.txt'])67+68+.. admonition:: Note on Handling Files69+70+ 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.71+72+Argument Escape Sequences73+---------------------------74+75+If 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.76+77+Example usage:78+79+.. click:example::80+81+ @click.command()82+ @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)87+88+And from the command line:89+90+.. click:run::91+92+ invoke(touch, ['--', '-foo.txt', 'bar.txt'])93+94+If you don't like the ``--`` marker, you can set ignore_unknown_options to True to avoid checking unknown options:95+96+.. click:example::97+98+ @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)104+105+And from the command line:106+107+.. click:run::108+109+ invoke(touch, ['-foo.txt', 'bar.txt'])110+111+112+.. _environment-variables:113+114+Environment Variables115+---------------------116+117+Arguments can use environment variables. To do so, pass the name(s) of the environment variable(s) via `envvar` in ``click.argument``.118+119+Checking one environment variable:120+121+.. click:example::122+123+ @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())128+129+And from the command line:130+131+.. click:run::132+133+ with isolated_filesystem():134+ # Writing the file in the filesystem.135+ with open('hello.txt', 'w') as f:136+ f.write('Hello World!')137+ invoke(echo, env={'SRC': 'hello.txt'})138+139+140+Checking multiple environment variables:141+142+.. click:example::143+144+ @click.command()145+ @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())149+150+And from the command line:151+152+.. click:run::153+154+ 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'})deleted:docs/arguments.rst-158 removed
1-.. _arguments:2-3-Arguments4-=========5-6-.. currentmodule:: click7-8-Arguments are:9-10-* 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>`.13-14-Useful and often used kwargs are:15-16-* ``default``: Passes a default.17-* ``nargs``: Sets the number of arguments. Set to -1 to take an arbitrary number.18-19-Basic Arguments20----------------21-22-A 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``.23-24-Example:25-26-.. click:example::27-28- @click.command()29- @click.argument('filename')30- def touch(filename: str):31- """Print FILENAME."""32- click.echo(filename)33-34-And from the command line:35-36-.. click:run::37-38- invoke(touch, args=['foo.txt'])39-40-41-An 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`.42-43-.. admonition:: Note on Required Arguments44-45- 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.46-47-Multiple Arguments48------------------------------------49-50-To 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.51-52-.. click:example::53-54- @click.command()55- @click.argument('src', nargs=1)56- @click.argument('dsts', nargs=-1)57- def copy(src: str, dsts: tuple[str, ...]):58- """Move file SRC to DST."""59- for destination in dsts:60- click.echo(f"Copy {src} to folder {destination}")61-62-And from the command line:63-64-.. click:run::65-66- invoke(copy, args=['foo.txt', 'usr/david/foo.txt', 'usr/mitsuko/foo.txt'])67-68-.. admonition:: Note on Handling Files69-70- 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.71-72-Argument Escape Sequences73----------------------------74-75-If 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.76-77-Example usage:78-79-.. click:example::80-81- @click.command()82- @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)87-88-And from the command line:89-90-.. click:run::91-92- invoke(touch, ['--', '-foo.txt', 'bar.txt'])93-94-If you don't like the ``--`` marker, you can set ignore_unknown_options to True to avoid checking unknown options:95-96-.. click:example::97-98- @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)104-105-And from the command line:106-107-.. click:run::108-109- invoke(touch, ['-foo.txt', 'bar.txt'])110-111-112-.. _environment-variables:113-114-Environment Variables115----------------------116-117-Arguments can use environment variables. To do so, pass the name(s) of the environment variable(s) via `envvar` in ``click.argument``.118-119-Checking one environment variable:120-121-.. click:example::122-123- @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())128-129-And from the command line:130-131-.. click:run::132-133- with isolated_filesystem():134- # Writing the file in the filesystem.135- with open('hello.txt', 'w') as f:136- f.write('Hello World!')137- invoke(echo, env={'SRC': 'hello.txt'})138-139-140-Checking multiple environment variables:141-142-.. click:example::143-144- @click.command()145- @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())149-150-And from the command line:151-152-.. click:run::153-154- 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'})