Skip to content
Myr

Add ability to truncate help text in docstrings.

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

I use docstrings in all of my functions to document function arguments so it makes me sad that I couldn't exclude :param: and other tags from my command help texts. Here I am introducing the ability for Click to truncate help text in docstrings using the \f form feed escaped character. I chose it randomly.

authored by Robert Pooley · committed by Stephen Rosen · parent 7463db5 · Browse files · 3 files+65 added−0 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+65 added−0 removed

  • tests:+29 added−0 removed
  • other:+36 added−0 removed

3 files+65 added−0 removed

modified:click/core.py+2 added
776 unmodified lines
777 #: the list of parameters for this command in the order they778 #: should show up in the help page and execute. Eager parameters779 #: will automatically be handled before non eager ones.780 self.params = params or []781 if help and '\f' in help:782 help = help.split('\f', 1)[0]783 self.help = help784 self.epilog = epilog785 self.options_metavar = options_metavar786 self.short_help = short_help
1046 unmodified lines
modified:docs/documentation.rst+34 added
76 unmodified lines
77 invoke(cli, args=['--help'])7879.. _doc-meta-variables:8081Truncating Help Texts82---------------------8384Click gets command help text from function docstrings. However if you85already use docstrings to document function arguments you may not want86to see :param: and :return: lines in your help text.8788You can use the ``\f`` escape marker to have Click truncate the help text89after the marker.9091Example:9293.. click:example::9495 @click.command()96 @click.pass_context97 def cli(ctx):98 """First paragraph.99100 This is a very long second101 paragraph and not correctly102 wrapped but it will be rewrapped.103 \f104105 :param click.core.Context ctx: Click context.106 """107108And what it looks like:109110.. click:run::111112 invoke(cli, args=['--help'])113114115Meta Variables116--------------117118Options and parameters accept a ``metavar`` argument that can change the
79 unmodified lines
modified:tests/test_formatting.py+29 added
266 unmodified lines
267 '',268 'Options:',269 ' --help Show this message and exit.'270 ]271272273def test_truncating_docstring(runner):274 @click.command()275 @click.pass_context276 def cli(ctx):277 """First paragraph.278279 This is a very long second280 paragraph and not correctly281 wrapped but it will be rewrapped.282 \f283284 :param click.core.Context ctx: Click context.285 """286287 result = runner.invoke(cli, ['--help'], terminal_width=60)288 assert not result.exception289 assert result.output.splitlines() == [290 'Usage: cli [OPTIONS]',291 '',292 ' First paragraph.',293 '',294 ' This is a very long second paragraph and not correctly',295 ' wrapped but it will be rewrapped.',296 '',297 'Options:',298 ' --help Show this message and exit.',299 ]