Skip to content
Myr

documentation.md

This file

Path

docs/documentation.md

293 lines · 7.4 KiB · Markdown

Last changed by

Document show_envvar in Help Pages (#3166)Rowlando13before adoption2570bbc

History of this file

Take it

Documentation.rst reST to MyST (#2980)Rowlando13before adoption5c1239b

lines 1–147why, one step back
1# Help Pages
23```{currentmodule} click4```56Click 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.78## Help Texts910Commands and options accept help arguments. For commands, the docstring of the function is automatically used if provided.1112Simple example:1314```{eval-rst}15.. 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::29 invoke(hello, args=['--help'])30```3132## Command Short Help3334For 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`:3536```{eval-rst}37.. click:example::3839 import click4041 @click.group()42 def cli():43 """A simple command line tool."""4445 @cli.command('init', short_help='init the repo')46 def init():47 """Initializes the repository."""4849.. click:run::50 invoke(cli, args=['--help'])51```5253## Command Epilog Help5455The help epilog is printed at the end of the help and is useful for showing example command usages or referencing additional help resources.5657```{eval-rst}58.. click:example::5960 import click6162 @click.command(63 epilog='See https://example.com for more details',64 )65 def init():66 """Initializes the repository."""6768.. click:run::69 invoke(init, args=['--help'])70```7172(documenting-arguments)=7374## Documenting Arguments7576{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```{eval-rst}82.. click:example::8384 @click.command()85 @click.argument('filename')86 def touch(filename):87 """Print FILENAME."""88 click.echo(filename)8990.. click:run::91 invoke(touch, args=['--help'])92```9394Or more explicitly:9596```{eval-rst}97.. click:example::9899 @click.command()100 @click.argument('filename')101 def touch(filename):102 """Print FILENAME.103104 FILENAME is the name of the file to check.105 """106 click.echo(filename)107108.. click:run::109 invoke(touch, args=['--help'])110```111112## Showing Defaults113114To control the appearance of defaults pass `show_default`.115116```{eval-rst}117.. click:example::118119 @click.command()120 @click.option('--n', default=1, show_default=False, help='number of dots')121 def dots(n):122 click.echo('.' * n)123124.. click:run::125 invoke(dots, args=['--help'])126```127128For single option boolean flags, the default remains hidden if the default value is False, even if show default is set to true.129130```{eval-rst}131.. click:example::132133 @click.command()134 @click.option('--n', default=1, show_default=True)135 @click.option("--gr", is_flag=True, show_default=True, default=False, help="Greet the world.")136 @click.option("--br", is_flag=True, show_default=True, default=True, help="Add a thematic break")137 def dots(n, gr, br):138 if gr:139 click.echo('Hello world!')140 click.echo('.' * n)141 if br:142 click.echo('-' * n)143144.. click:run::145 invoke(dots, args=['--help'])146```147

Document show_envvar in Help Pages (#3166)Rowlando13before adoption2570bbc

lines 148–163why, one step back
148## Showing Environment Variables
149150To control the appearance of environment variables pass `show_envvar`.151152```{eval-rst}153.. click:example::154155 @click.command()156 @click.option('--username', envvar='USERNAME', show_envvar=True)157 def greet(username):158 click.echo(f'Hello {username}!')159160.. click:run::161 invoke(greet, args=['--help'])162```163

Documentation.rst reST to MyST (#2980)Rowlando13before adoption5c1239b

lines 164–165why, one step back
164## Click's Wrapping Behavior
165

Improve discoverability of Context.max_content_width (#3011)Rowlando13before adoptiona97412d

line 166why, one step back
166Click's default wrapping ignores single new lines and rewraps the text based on the width of the terminal to a maximum of 80 characters by default, but this can be modified with {attr}`~Context.max_content_width`. In the example notice how the second grouping of three lines is rewrapped into a single paragraph.

Documentation.rst reST to MyST (#2980)Rowlando13before adoption5c1239b

lines 167–293why, one step back
167
168```{eval-rst}169.. click:example::170171 import click172173 @click.command()174 def cli():175 """176 This is a very long paragraph and as you177 can see wrapped very early in the source text178 but will be rewrapped to the terminal width in179 the final output.180181 This is182 a paragraph183 that is compacted.184 """185186.. click:run::187 invoke(cli, args=['--help'])188```189190## Escaping Click's Wrapping191192Sometimes 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.193194Example:195196```{eval-rst}197.. click:example::198199 import click200201 @click.command()202 def cli():203 """First paragraph.204205 \b206 This is207 a paragraph208 without rewrapping.209210 And this is a paragraph211 that will be rewrapped again.212 """213214.. click:run::215 invoke(cli, args=['--help'])216```217218To change the rendering maximum width, pass `max_content_width` when calling the command.219220```bash221cli(max_content_width=120)222```223224## Truncating Help Texts225226Click 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.227228Example:229230```{eval-rst}231.. click:example::232233 import click234235 @click.command()236 def cli():237 """First paragraph.238 \f239240 Words to not be included.241 """242243.. click:run::244 invoke(cli, args=['--help'])245```246247(doc-meta-variables)=248249## Placeholder / Meta Variable250251The 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.252253```{eval-rst}254.. click:example::255256 # This controls entry on the usage line.257 @click.command(options_metavar='[[options]]')258 @click.option('--count', default=1, help='number of greetings',259 metavar='<int>')260 @click.argument('name', metavar='<name>')261 def hello(name: str, count: int) -> None:262 """This script prints 'hello <name>' a total of <count> times."""263 for x in range(count):264 click.echo(f"Hello {name}!")265266# Example usage:267268.. click:run::269 invoke(hello, args=['--help'])270271```272273## Help Parameter Customization274275Help 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.276277This example changes the default parameters to `-h` and `--help`278instead of just `--help`:279280```{eval-rst}281.. click:example::282283 import click284285 CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])286287 @click.command(context_settings=CONTEXT_SETTINGS)288 def cli():289 pass290291.. click:run::292 invoke(cli, ['-h'])293```