Skip to content
Myr

documentation.rst to documentation.md

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

authored by Veritia · committed by Veritia · parent d548479 · Browse files · 2 files+280 added−268 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+280 added−268 removed

  • other:+280 added−268 removed

2 files+280 added−268 removed

added:docs/documentation.md+280 added
1# Help Pages23```{eval-rst}4.. currentmodule:: click5```67Click makes it very easy to document your command line tools. For most things Click automatically generates help pages for you. By design the text is customizable, but the layout is not.89## Help Texts1011Commands and options accept help arguments. For commands, the docstring of the function is automatically used if provided.1213Simple example:1415```{eval-rst}16.. click:example::1718 @click.command()19 @click.argument('name')20 @click.option('--count', default=1, help='number of greetings')21 def hello(name: str, count: int):22 """This script prints hello and a name one or more times."""23 for x in range(count):24 if name:25 click.echo(f"Hello {name}!")26 else:27 click.echo("Hello!")2829.. click:run::30 invoke(hello, args=['--help'])313233```3435## Command Short Help3637For subcommands, a short help snippet is generated. By default, it's the first sentence of the docstring. If too long, then it will ellipsize what cannot be fit on a single line with `...`. The short help snippet can also be overridden with `short_help`:3839```{eval-rst}40.. click:example::4142 import click4344 @click.group()45 def cli():46 """A simple command line tool."""4748 @cli.command('init', short_help='init the repo')49 def init():50 """Initializes the repository."""5152.. click:run::53 invoke(cli, args=['--help'])54```5556## Command Epilog Help5758The help epilog is printed at the end of the help and is useful for showing example command usages or referencing additional help resources.5960```{eval-rst}61.. click:example::6263 import click6465 @click.command(66 epilog='See https://example.com for more details',67 )68 def init():69 """Initializes the repository."""7071.. click:run::72 invoke(init, args=['--help'])73```7475(documenting-arguments)=7677## Documenting Arguments7879{class}`click.argument` does not take a `help` parameter. This follows the Unix Command Line Tools convention of using arguments only for necessary things and documenting them in the command help text80by name. This should then be done via the docstring.8182A brief example:8384```{eval-rst}85.. click:example::8687 @click.command()88 @click.argument('filename')89 def touch(filename):90 """Print FILENAME."""91 click.echo(filename)9293.. click:run::94 invoke(touch, args=['--help'])95```9697Or more explicitly:9899```{eval-rst}100.. click:example::101102 @click.command()103 @click.argument('filename')104 def touch(filename):105 """Print FILENAME.106107 FILENAME is the name of the file to check.108 """109 click.echo(filename)110111.. click:run::112 invoke(touch, args=['--help'])113```114115## Showing Defaults116117To control the appearance of defaults pass `show_default`.118119```{eval-rst}120.. click:example::121122 @click.command()123 @click.option('--n', default=1, show_default=False, help='number of dots')124 def dots(n):125 click.echo('.' * n)126127.. click:run::128 invoke(dots, args=['--help'])129```130131For single option boolean flags, the default remains hidden if the default value is False, even if show default is set to true.132133```{eval-rst}134.. click:example::135136 @click.command()137 @click.option('--n', default=1, show_default=True)138 @click.option("--gr", is_flag=True, show_default=True, default=False, help="Greet the world.")139 @click.option("--br", is_flag=True, show_default=True, default=True, help="Add a thematic break")140 def dots(n, gr, br):141 if gr:142 click.echo('Hello world!')143 click.echo('.' * n)144 if br:145 click.echo('-' * n)146147.. click:run::148 invoke(dots, args=['--help'])149```150151## Click's Wrapping Behavior152153Click's default wrapping ignores single new lines and rewraps the text based on the width of the terminal, to a maximum of 80 characters. In the example notice how the second grouping of three lines is rewrapped into a single paragraph.154155```{eval-rst}156.. click:example::157158 import click159160 @click.command()161 def cli():162 """163 This is a very long paragraph and as you164 can see wrapped very early in the source text165 but will be rewrapped to the terminal width in166 the final output.167168 This is169 a paragraph170 that is compacted.171 """172173.. click:run::174 invoke(cli, args=['--help'])175```176177## Escaping Click's Wrapping178179Sometimes Click's wrapping can be a problem, such as when showing code examples where new lines are significant. This behavior can be escaped on a per-paragraph basis by adding a line with only `\b` . The `\b` is removed from the rendered help text.180181Example:182183```{eval-rst}184.. click:example::185186 import click187188 @click.command()189 def cli():190 """First paragraph.191192 \b193 This is194 a paragraph195 without rewrapping.196197 And this is a paragraph198 that will be rewrapped again.199 """200201.. click:run::202 invoke(cli, args=['--help'])203```204205To change the rendering maximum width, pass `max_content_width` when calling the command.206207```bash208cli(max_content_width=120)209```210211## Truncating Help Texts212213Click gets {class}`Command` help text from the docstring. If you do not want to include part of the docstring, add the `\f` escape marker to have Click truncate the help text after the marker.214215Example:216217```{eval-rst}218.. click:example::219220 import click221222 @click.command()223 def cli():224 """First paragraph.225 \f226227 Words to not be included.228 """229230.. click:run::231 invoke(cli, args=['--help'])232```233234(doc-meta-variables)=235236## Placeholder / Meta Variable237238The default placeholder variable ([meta variable](https://en.wikipedia.org/wiki/Metasyntactic_variable#IETF_Requests_for_Comments)) in the help pages is the parameter name in uppercase with underscores. This can be changed for Commands and Parameters with the `options_metavar` and `metavar` kwargs.239240```{eval-rst}241.. click:example::242243 # This controls entry on the usage line.244 @click.command(options_metavar='[[options]]')245 @click.option('--count', default=1, help='number of greetings',246 metavar='<int>')247 @click.argument('name', metavar='<name>')248 def hello(name: str, count: int) -> None:249 """This script prints 'hello <name>' a total of <count> times."""250 for x in range(count):251 click.echo(f"Hello {name}!")252253# Example usage:254 from click.testing import CliRunner255 runner = CliRunner()256 result = runner.invoke(hello, args=['--help'])257 print(result.output)258```259260## Help Parameter Customization261262Help parameters are automatically added by Click for any command. The default is `--help` but can be overridden by the context setting {attr}`~Context.help_option_names`. Click also performs automatic conflict resolution on the default help parameter, so if a command itself implements a parameter named `help` then the default help will not be run.263264This example changes the default parameters to `-h` and `--help`265instead of just `--help`:266267```{eval-rst}268.. click:example::269270 import click271272 CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])273274 @click.command(context_settings=CONTEXT_SETTINGS)275 def cli():276 pass277278.. click:run::279 invoke(cli, ['-h'])280```
deleted:docs/documentation.rst-268 removed
1Help Pages2===================34.. currentmodule:: click56Click makes it very easy to document your command line tools. For most things Click automatically generates help pages for you. By design the text is customizable, but the layout is not.78Help Texts9--------------1011Commands and options accept help arguments. For commands, the docstring of the function is automatically used if provided.1213Simple example:1415.. click:example::1617 @click.command()18 @click.argument('name')19 @click.option('--count', default=1, help='number of greetings')20 def hello(name: str, count: int):21 """This script prints hello and a name one or more times."""22 for x in range(count):23 if name:24 click.echo(f"Hello {name}!")25 else:26 click.echo("Hello!")2728.. click:run::2930 invoke(hello, args=['--help'])313233Command Short Help34------------------3536For subcommands, a short help snippet is generated. By default, it's the first sentence of the docstring. If too long, then it will ellipsize what cannot be fit on a single line with ``...``. The short help snippet can also be overridden with ``short_help``:3738.. click:example::3940 @click.group()41 def cli():42 """A simple command line tool."""4344 @cli.command('init', short_help='init the repo')45 def init():46 """Initializes the repository."""474849.. click:run::5051 invoke(cli, args=['--help'])5253Command Epilog Help54-------------------5556The help epilog is printed at the end of the help and is useful for showing example command usages or referencing additional help resources.5758.. click:example::5960 @click.command(61 epilog='See https://example.com for more details',62 )63 def init():64 """Initializes the repository."""656667.. click:run::6869 invoke(init, args=['--help'])7071.. _documenting-arguments:7273Documenting Arguments74----------------------7576:class:`click.argument` does not take a ``help`` parameter. This follows the Unix Command Line Tools convention of using arguments only for necessary things and documenting them in the command help text77by name. This should then be done via the docstring.7879A brief example:8081.. click:example::8283 @click.command()84 @click.argument('filename')85 def touch(filename):86 """Print FILENAME."""87 click.echo(filename)888990.. click:run::9192 invoke(touch, args=['--help'])9394Or more explicitly:9596.. click:example::9798 @click.command()99 @click.argument('filename')100 def touch(filename):101 """Print FILENAME.102103 FILENAME is the name of the file to check.104 """105 click.echo(filename)106107.. click:run::108109 invoke(touch, args=['--help'])110111112Showing Defaults113---------------------------114To control the appearance of defaults pass ``show_default``.115116.. click:example::117118 @click.command()119 @click.option('--n', default=1, show_default=False, help='number of dots')120 def dots(n):121 click.echo('.' * n)122123.. click:run::124125 invoke(dots, args=['--help'])126127For single option boolean flags, the default remains hidden if the default value is False even if show default is true.128129.. click:example::130131 @click.command()132 @click.option('--n', default=1, show_default=True)133 @click.option("--gr", is_flag=True, show_default=True, default=False, help="Greet the world.")134 @click.option("--br", is_flag=True, show_default=True, default=True, help="Add a thematic break")135 def dots(n, gr, br):136 if gr:137 click.echo('Hello world!')138 click.echo('.' * n)139 if br:140 click.echo('-' * n)141142.. click:run::143144 invoke(dots, args=['--help'])145146147Click's Wrapping Behavior148----------------------------149Click's default wrapping ignores single new lines and rewraps the text based on the width of the terminal, to a maximum of 80 characters. In the example notice how the second grouping of three lines is rewrapped into a single paragraph.150151.. click:example::152153 @click.command()154 def cli():155 """156 This is a very long paragraph and as you157 can see wrapped very early in the source text158 but will be rewrapped to the terminal width in159 the final output.160161 This is162 a paragraph163 that is compacted.164 """165166.. click:run::167168 invoke(cli, args=['--help'])169170Escaping Click's Wrapping171---------------------------172Sometimes Click's wrapping can be a problem, such as when showing code examples where newlines are significant. This behavior can be escaped on a per-paragraph basis by adding a line with only ``\b`` . The ``\b`` is removed from the rendered help text.173174Example:175176.. click:example::177178 @click.command()179 def cli():180 """First paragraph.181182 \b183 This is184 a paragraph185 without rewrapping.186187 And this is a paragraph188 that will be rewrapped again.189 """190191192.. click:run::193194 invoke(cli, args=['--help'])195196To change the rendering maximum width, pass ``max_content_width`` when calling the command.197198.. code-block:: python199200 cli(max_content_width=120)201202203204Truncating Help Texts205---------------------206207Click gets :class:`Command` help text from the docstring. If you do not want to include part of the docstring, add the ``\f`` escape marker to have Click truncate the help text after the marker.208209Example:210211.. click:example::212213 @click.command()214 def cli():215 """First paragraph.216 \f217218 Words to not be included.219 """220221.. click:run::222223 invoke(cli, args=['--help'])224225.. _doc-meta-variables:226227Placeholder / Meta Variable228-----------------------------229230The default placeholder variable (`meta variable <https://en.wikipedia.org/wiki/Metasyntactic_variable#IETF_Requests_for_Comments>`_) in the help pages is the parameter name in uppercase with underscores. This can be changed for Commands and Parameters with the ``options_metavar`` and ``metavar`` kwargs.231232.. click:example::233234 # This controls entry on the usage line.235 @click.command(options_metavar='[[options]]')236 @click.option('--count', default=1, help='number of greetings',237 metavar='<int>')238 @click.argument('name', metavar='<name>')239 def hello(name: str, count: int) -> None:240 """This script prints 'hello <name>' a total of <count> times."""241 for x in range(count):242 click.echo(f"Hello {name}!")243244Example:245246.. click:run::247248 invoke(hello, args=['--help'])249250Help Parameter Customization251----------------------------252Help parameters are automatically added by Click for any command. The default is ``--help`` but can be override by the context setting :attr:`~Context.help_option_names`. Click also performs automatic conflict resolution on the default help parameter so if a command itself implements a parameter named ``help`` then the default help will not be run.253254This example changes the default parameters to ``-h`` and ``--help``255instead of just ``--help``:256257.. click:example::258259 CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])260261 @click.command(context_settings=CONTEXT_SETTINGS)262 def cli():263 pass264265266.. click:run::267268 invoke(cli, ['-h'])