Skip to content
Myr

Merge pull request #1325 from mattsb42/argdoc

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Clarify where arguments should be documented and provide examples.

authored by David Lord · committed by GitHub · parents 7f13da3, 9478306 · Browse files · 3 files+62 added−11 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+62 added−11 removed

  • other:+62 added−11 removed

3 files+62 added−11 removed

modified:docs/arguments.rst+12 added-5 removed
6 unmodified lines
78Arguments work similarly to :ref:`options <options>` but are positional.9They also only support a subset of the features of options due to their10syntactical nature. Click will also not attempt to document arguments for11you and wants you to document them manually in order to avoid ugly help12pages.11you and wants you to :ref:`document them manually <documenting-arguments>`12in order to avoid ugly help pages.1314Basic Arguments15---------------16
7 unmodified lines
2425 @click.command()26 @click.argument('filename')27 def touch(filename):28 """Print FILENAME."""29 click.echo(filename)3031And what it looks like:32
19 unmodified lines
52 @click.command()53 @click.argument('src', nargs=-1)54 @click.argument('dst', nargs=1)55 def copy(src, dst):56 """Move file SRC to DST."""57 for fn in src:58 click.echo('move %s to folder %s' % (fn, dst))5960And what it looks like:
41 unmodified lines
102 @click.command()103 @click.argument('input', type=click.File('rb'))104 @click.argument('output', type=click.File('wb'))105 def inout(input, output):106 """Copy contents of INPUT to OUTPUT."""107 while True:108 chunk = input.read(1024)109 if not chunk:110 break
27 unmodified lines
138139.. click:example::140141 @click.command()139 @click.argument('f', type=click.Path(exists=True))140 def touch(f):141 click.echo(click.format_filename(f))142 @click.argument('filename', type=click.Path(exists=True))143 def touch(filename):144 """Print FILENAME if the file exists."""145 click.echo(click.format_filename(filename))146147And what it does:148149.. click:run::
52 unmodified lines
202203 @click.command()204 @click.argument('src', envvar='SRC', type=click.File('r'))205 def echo(src):206 """Print value of SRC environment variable."""207 click.echo(src.read())208209And from the command line:210
28 unmodified lines
239240 @click.command()241 @click.argument('files', nargs=-1, type=click.Path())242 def touch(files):243 """Print all FILES file names."""244 for filename in files:245 click.echo(filename)246247And from the command line:
9 unmodified lines
257258 @click.command(context_settings={"ignore_unknown_options": True})259 @click.argument('files', nargs=-1, type=click.Path())260 def touch(files):261 """Print all FILES file names."""262 for filename in files:263 click.echo(filename)264265And from the command line:
5 unmodified lines
modified:docs/documentation.rst+48 added-4 removed
30 unmodified lines
31.. click:run::3233 invoke(hello, args=['--help'])3435Arguments cannot be documented this way. This is to follow the general36convention of Unix tools of using arguments for only the most necessary37things and to document them in the introduction text by referring to them38by name.3536.. _documenting-arguments:3738Documenting Arguments39~~~~~~~~~~~~~~~~~~~~~4041:func:`click.argument` does not take a ``help`` parameter. This is to42follow the general convention of Unix tools of using arguments for only43the most necessary things, and to document them in the command help text44by referring to them by name.4546You might prefer to reference the argument in the description:4748.. click:example::4950 @click.command()51 @click.argument('filename')52 def touch(filename):53 """Print FILENAME."""54 click.echo(filename)5556And what it looks like:5758.. click:run::5960 invoke(touch, args=['--help'])6162Or you might prefer to explicitly provide a description of the argument:6364.. click:example::6566 @click.command()67 @click.argument('filename')68 def touch(filename):69 """Print FILENAME.7071 FILENAME is the name of the file to check.72 """73 click.echo(filename)7475And what it looks like:7677.. click:run::7879 invoke(touch, args=['--help'])8081For more examples, see the examples in :doc:`/arguments`.828384Preventing Rewrapping85---------------------8687The default behavior of Click is to rewrap text based on the width of the
154 unmodified lines
modified:docs/parameters.rst+2 added-2 removed
21 unmodified lines
22* automatic prompting for missing input23* act as flags (boolean or otherwise)24* option values can be pulled from environment variables, arguments can not25* options are fully documented in the help page, arguments are not26 (this is intentional as arguments might be too specific to be27 automatically documented)26 (:ref:`this is intentional <documenting-arguments>` as arguments27 might be too specific to be automatically documented)2829On the other hand arguments, unlike options, can accept an arbitrary number30of arguments. Options can strictly ever only accept a fixed number of31arguments (defaults to 1).
118 unmodified lines