documentation.rst
This file
Path
docs/documentation.rst
267 lines · 6.4 KiB
Last changed by
Merge branch '8.1.x'David Lordbefore adoptiond9af5cf
History of this fileTake it
The walk stopped after 200 commits: lines older than 98e471f point at it.
Merge pull request #1792 from pallets/consistencyDavid Lordbefore adoption98e471f
lines 1–87why, one step back1Documenting Scripts2===================34.. currentmodule:: click56Click makes it very easy to document your command line tools. First of7all, it automatically generates help pages for you. While these are8currently not customizable in terms of their layout, all of the text9can be changed.1011Help Texts12----------1314Commands and options accept help arguments. In the case of commands, the15docstring of the function is automatically used if provided.1617Simple example:1819.. click:example::2021 @click.command()22 @click.option('--count', default=1, help='number of greetings')23 @click.argument('name')24 def hello(count, name):25 """This script prints hello NAME COUNT times."""26 for x in range(count):27 click.echo(f"Hello {name}!")2829And what it looks like:3031.. click:run::3233 invoke(hello, args=['--help'])343536.. _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 theMerge branch '8.1.x'David Lordbefore adoptiond9af5cf
lines 88–90why, one step back88terminal, to a maximum 80 characters. In some circumstances, this can become89a problem. The main issue is when showing code examples, where newlines are90significant.Merge pull request #1792 from pallets/consistencyDavid Lordbefore adoption98e471f
lines 91–123why, one step back9192Rewrapping can be disabled on a per-paragraph basis by adding a line with93solely the ``\b`` escape marker in it. This line will be removed from the94help text and rewrapping will be disabled.9596Example:9798.. click:example::99100 @click.command()101 def cli():102 """First paragraph.103104 This is a very long second paragraph and as you105 can see wrapped very early in the source text106 but will be rewrapped to the terminal width in107 the final output.108109 \b110 This is111 a paragraph112 without rewrapping.113114 And this is a paragraph115 that will be rewrapped again.116 """117118And what it looks like:119120.. click:run::121122 invoke(cli, args=['--help'])123Merge branch '8.1.x'David Lordbefore adoptiond9af5cf
lines 124–130why, one step back124To change the maximum width, pass ``max_content_width`` when calling the command.125126.. code-block:: python127128 cli(max_content_width=120)129130Merge pull request #1792 from pallets/consistencyDavid Lordbefore adoption98e471f
lines 131–219why, one step back131.. _doc-meta-variables:132133Truncating Help Texts134---------------------135136Click gets command help text from function docstrings. However if you137already use docstrings to document function arguments you may not want138to see :param: and :return: lines in your help text.139140You can use the ``\f`` escape marker to have Click truncate the help text141after the marker.142143Example:144145.. click:example::146147 @click.command()148 @click.pass_context149 def cli(ctx):150 """First paragraph.151152 This is a very long second153 paragraph and not correctly154 wrapped but it will be rewrapped.155 \f156157 :param click.core.Context ctx: Click context.158 """159160And what it looks like:161162.. click:run::163164 invoke(cli, args=['--help'])165166167Meta Variables168--------------169170Options and parameters accept a ``metavar`` argument that can change the171meta variable in the help page. The default version is the parameter name172in uppercase with underscores, but can be annotated differently if173desired. This can be customized at all levels:174175.. click:example::176177 @click.command(options_metavar='<options>')178 @click.option('--count', default=1, help='number of greetings',179 metavar='<int>')180 @click.argument('name', metavar='<name>')181 def hello(count, name):182 """This script prints hello <name> <int> times."""183 for x in range(count):184 click.echo(f"Hello {name}!")185186Example:187188.. click:run::189190 invoke(hello, args=['--help'])191192193Command Short Help194------------------195196For commands, a short help snippet is generated. By default, it's the first197sentence of the help message of the command, unless it's too long. This can198also be overridden:199200.. click:example::201202 @click.group()203 def cli():204 """A simple command line tool."""205206 @cli.command('init', short_help='init the repo')207 def init():208 """Initializes the repository."""209210 @cli.command('delete', short_help='delete the repo')211 def delete():212 """Deletes the repository."""213214And what it looks like:215216.. click:run::217218 invoke(cli, prog_name='repo.py')219Merge branch '8.1.x'David Lordbefore adoptiond9af5cf
lines 220–237why, one step back220Command Epilog Help221-------------------222223The help epilog is like the help string but it's printed at the end of the help224page after everything else. Useful for showing example command usages or225referencing additional help resources.226227.. click:example::228229 @click.command(epilog='Check out our docs at https://click.palletsprojects.com/ for more details')230 def init():231 """Initializes the repository."""232233And what it looks like:234235.. click:run::236237 invoke(init, prog_name='repo.py', args=['--help'])Merge pull request #1792 from pallets/consistencyDavid Lordbefore adoption98e471f
lines 238–267why, one step back238239Help Parameter Customization240----------------------------241242.. versionadded:: 2.0243244The help parameter is implemented in Click in a very special manner.245Unlike regular parameters it's automatically added by Click for any246command and it performs automatic conflict resolution. By default it's247called ``--help``, but this can be changed. If a command itself implements248a parameter with the same name, the default help parameter stops accepting249it. There is a context setting that can be used to override the names of250the help parameters called :attr:`~Context.help_option_names`.251252This example changes the default parameters to ``-h`` and ``--help``253instead of just ``--help``:254255.. click:example::256257 CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])258259 @click.command(context_settings=CONTEXT_SETTINGS)260 def cli():261 pass262263And what it looks like:264265.. click:run::266267 invoke(cli, ['-h'])