clarify where arguments should be documented and add examples
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by mattsb42 · committed by David Lord · parent 7f13da3 · 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
Provenance
- Commit
9478306- Tree
c9e29c7- Author
mattsb42 <matt.s.b.42@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
7f13da3
3 files changed+62 added−11 removed
modified:docs/arguments.rst+12 added-5 removed
6 unmodified lines7 8 Arguments work similarly to :ref:`options <options>` but are positional.9 They also only support a subset of the features of options due to their10 syntactical nature. Click will also not attempt to document arguments for11-you and wants you to document them manually in order to avoid ugly help12-pages.11+you and wants you to :ref:`document them manually <documenting-arguments>`12+in order to avoid ugly help pages.13 14 Basic Arguments15 ---------------16 7 unmodified lines24 25 @click.command()26 @click.argument('filename')27 def touch(filename):28+ """Print FILENAME."""29 click.echo(filename)30 31 And what it looks like:32 19 unmodified lines52 @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))59 60 And what it looks like:41 unmodified lines102 @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 break27 unmodified lines138 139 .. click:example::140 141 @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))146 147 And what it does:148 149 .. click:run::52 unmodified lines202 203 @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())208 209 And from the command line:210 28 unmodified lines239 240 @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)246 247 And from the command line:9 unmodified lines257 258 @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)264 265 And from the command line:5 unmodified linesmodified:docs/documentation.rst+48 added-4 removed
30 unmodified lines31 .. click:run::32 33 invoke(hello, args=['--help'])34 35-Arguments cannot be documented this way. This is to follow the general36-convention of Unix tools of using arguments for only the most necessary37-things and to document them in the introduction text by referring to them38-by name.35 36+.. _documenting-arguments:37+38+Documenting Arguments39+~~~~~~~~~~~~~~~~~~~~~40+41+:func:`click.argument` does not take a ``help`` parameter. This is to42+follow the general convention of Unix tools of using arguments for only43+the most necessary things, and to document them in the command help text44+by referring to them by name.45+46+You might prefer to reference the argument in the description:47+48+.. click:example::49+50+ @click.command()51+ @click.argument('filename')52+ def touch(filename):53+ """Print FILENAME."""54+ click.echo(filename)55+56+And what it looks like:57+58+.. click:run::59+60+ invoke(touch, args=['--help'])61+62+Or you might prefer to explicitly provide a description of the argument:63+64+.. click:example::65+66+ @click.command()67+ @click.argument('filename')68+ def touch(filename):69+ """Print FILENAME.70+71+ FILENAME is the name of the file to check.72+ """73+ click.echo(filename)74+75+And what it looks like:76+77+.. click:run::78+79+ invoke(touch, args=['--help'])80+81+For more examples, see the examples in :doc:`/arguments`.82+83+84 Preventing Rewrapping85 ---------------------86 87 The default behavior of Click is to rewrap text based on the width of the154 unmodified linesmodified:docs/parameters.rst+2 added-2 removed
21 unmodified lines22 * 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)28 29 On the other hand arguments, unlike options, can accept an arbitrary number30 of arguments. Options can strictly ever only accept a fixed number of31 arguments (defaults to 1).118 unmodified lines