Apply suggestions from code review for arguments docs.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Edward G · committed by Edward G · parent e9a76ba · Browse files · 2 files·+29 added−25 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+29 added−25 removed
- other:+29 added−25 removed
Provenance
- Commit
9959aa9- Tree
da11131- Author
Edward G <edward.g2013@gmail.com>- Committer
Edward G <edward.g2013@gmail.com>- Parent
e9a76ba
2 files changed+29 added−25 removed
modified:docs/arguments.rst+16 added-16 removed
5 unmodified lines6 .. currentmodule:: click7 8 Arguments are:9 10-* Are positional in nature10+* 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>`12+* :ref:`Documented manually <documenting-arguments>`.13 14 Useful and often used kwargs are:15 16-* ``default`` : Passes a default17-* ``nargs`` : Sets the number of arguments. Set to -1 to take an arbitrary number.16+* ``default``: Passes a default.17+* ``nargs``: Sets the number of arguments. Set to -1 to take an arbitrary number.18 19-Basic Argument19+Basic Arguments20 ---------------21 22-A minimal :class:`click.Argument` takes the name of function argument. If you just pass it this, it assumes 1 argument, required, no default and string type.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):30+ def touch(filename: str):31 """Print FILENAME."""32 click.echo(filename)33 34-And what it looks like: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 STRING.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.7 unmodified lines53 54 @click.command()55 @click.argument('src', nargs=1)56 @click.argument('dsts', nargs=-1)57- def copy(src, dsts):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 what it looks like: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 Files.68+.. admonition:: Note on Handling Files69 70- This is not how you would handle files and files paths, since it passes them in as strings. For more see :ref:`handling-files`.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 3 unmodified lines78 79 .. click:example::80 81 @click.command()82- @click.argument('files', nargs=-1, type=click.Path())83- def touch(files):82+ @click.argument('files', nargs=-1, type=click.Path(path_type=pathlib.Path))83+ def touch(files: tuple[pathlib.Path, ...]):84 """Print all FILES file names."""85 for filename in files:86 click.echo(filename)87 25 unmodified lines113 114 Environment Variables115 ---------------------116 117-Arguments can use environment variables. To do so pass the name of the environment variable(s) into ``click.argument``.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::37 unmodified linesmodified:docs/handling-files.rst+13 added-9 removed
3 unmodified lines4 ================5 6 .. currentmodule:: click7 8-Click has built in features to support file and file path handling. Since files and files paths are recommended to be passed in as arguments, the examples use arguments.8+Click has built in features to support file and file path handling. The examples use arguments but the same principle applies to options as well.9 10 .. _file-args:11 12 File Arguments13 -----------------14 15 Click supports working with files with the :class:`File` type. Some notable features are:16 17-* Support for ``-`` to mean a special file that refers to stdin/stdout. This is common pattern for POSIX command line utilities, for example wget.18-* Deals with Unicode and bytes correctly for all versions of Python.17+* Support for ``-`` to mean a special file that refers to stdin when used for reading, and stdout when used for writing. This is a common pattern for POSIX command line utilities.18+* Deals with ``str`` and ``bytes`` correctly for all versions of Python.19 20 Example:21 22 .. click:example::8 unmodified lines31 if not chunk:32 break33 output.write(chunk)34 35-And what it does:35+And from the command line:36 37 .. click:run::38 39 with isolated_filesystem():3 unmodified lines43 44 File Path Arguments45 ----------------------46 47-For handling paths, the :class:`Path` type is better than a string. Some notable features are:47+For handling paths, the :class:`Path` type is better than a ``str``. Some notable features are:48 49-* It can run checks such as does the path exist or is the file executable.50-* It also handles formatting error messages with :func:`format_filename` so any undecodable bytes will be printed nicely.49+* The ``exists`` argument will verify whether the path exists.50+* ``readable``, ``writable``, and ``executable`` can perform permission checks.51+* ``file_okay`` and ``dir_okay`` allow specifying whether files/directories are accepted.52+* Error messages are nicely formatted using :func:`format_filename` so any undecodable bytes will be printed nicely.53 54+See :class:`Path` for all features.55+56 Example:57 58 .. click:example::59 2 unmodified lines62 def touch(filename):63 """Print FILENAME if the file exists."""64 click.echo(click.format_filename(filename))65 62-And what it does:66+And from the command line:67 68 .. click:run::69 70 with isolated_filesystem():11 unmodified lines82 83 File open behavior can be controlled by the boolean kwarg ``lazy``. If a file is opened lazily:84 85 * A failure at first IO operation will happen by raising an :exc:`FileError`.82-* It can help minimize resource handling confusion. If a file is opened in lazy mode, it will receive a ``close_intelligently`` method that can help figure out if the file needs closing or not. This is not needed for parameters, but is necessary for manually prompting. For manual prompts with the :func:`prompt` function you do not know if a stream like stdout was opened (which was already open before) or a real file was opened (that needs closing).86+* It can help minimize resource handling confusion. If a file is opened in lazy mode, it will call :meth:`LazyFile.close_intelligently` to help figure out if the file needs closing or not. This is not needed for parameters, but is necessary for manually prompting. For manual prompts with the :func:`prompt` function you do not know if a stream like stdout was opened (which was already open before) or a real file was opened (that needs closing).87 88 Since files opened for writing will typically empty the file, the lazy mode should only be disabled if the developer is absolutely sure that this is intended behavior.89 90 It is also possible to open files in atomic mode by passing ``atomic=True``. In atomic mode, all writes go into a separate file in the same folder, and upon completion, the file will be moved over to the original location. This is useful if a file regularly read by other users is modified.