Skip to content
Myr

documentation.md

This file

Path

docs/documentation.md

277 lines · 7.0 KiB · Markdown

Last changed by

Merge branch 'rstTomyst' of https://github.com/denuoweb/click into rstTomystdenuowebbefore adoptionbb11331

History of this file

Take it

Merge branch 'rstTomyst' of https://github.com/denuoweb/click into rstTomystdenuowebbefore adoptionbb11331

lines 1–277why, 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```147148## Click's Wrapping Behavior149150Click'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.151152```{eval-rst}153.. click:example::154155 import click156157 @click.command()158 def cli():159 """160 This is a very long paragraph and as you161 can see wrapped very early in the source text162 but will be rewrapped to the terminal width in163 the final output.164165 This is166 a paragraph167 that is compacted.168 """169170.. click:run::171 invoke(cli, args=['--help'])172```173174## Escaping Click's Wrapping175176Sometimes 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.177178Example:179180```{eval-rst}181.. click:example::182183 import click184185 @click.command()186 def cli():187 """First paragraph.188189 \b190 This is191 a paragraph192 without rewrapping.193194 And this is a paragraph195 that will be rewrapped again.196 """197198.. click:run::199 invoke(cli, args=['--help'])200```201202To change the rendering maximum width, pass `max_content_width` when calling the command.203204```bash205cli(max_content_width=120)206```207208## Truncating Help Texts209210Click 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.211212Example:213214```{eval-rst}215.. click:example::216217 import click218219 @click.command()220 def cli():221 """First paragraph.222 \f223224 Words to not be included.225 """226227.. click:run::228 invoke(cli, args=['--help'])229```230231(doc-meta-variables)=232233## Placeholder / Meta Variable234235The 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.236237```{eval-rst}238.. click:example::239240 # This controls entry on the usage line.241 @click.command(options_metavar='[[options]]')242 @click.option('--count', default=1, help='number of greetings',243 metavar='<int>')244 @click.argument('name', metavar='<name>')245 def hello(name: str, count: int) -> None:246 """This script prints 'hello <name>' a total of <count> times."""247 for x in range(count):248 click.echo(f"Hello {name}!")249250# Example usage:251252.. click:run::253 invoke(hello, args=['--help'])254255```256257## Help Parameter Customization258259Help 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.260261This example changes the default parameters to `-h` and `--help`262instead of just `--help`:263264```{eval-rst}265.. click:example::266267 import click268269 CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])270271 @click.command(context_settings=CONTEXT_SETTINGS)272 def cli():273 pass274275.. click:run::276 invoke(cli, ['-h'])277```