Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rebase of #629; allow use of page break to split docstring-based helptext #1091

Merged
merged 2 commits into from
Aug 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions click/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,10 @@ def __init__(self, name, context_settings=None, callback=None,
#: should show up in the help page and execute. Eager parameters
#: will automatically be handled before non eager ones.
self.params = params or []
# if a form feed (page break) is found in the help text, truncate help
# text to the content preceding the first form feed
if help and '\f' in help:
help = help.split('\f', 1)[0]
self.help = help
self.epilog = epilog
self.options_metavar = options_metavar
Expand Down
34 changes: 34 additions & 0 deletions docs/documentation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,40 @@ And what it looks like:

.. _doc-meta-variables:

Truncating Help Texts
---------------------

Click gets command help text from function docstrings. However if you
already use docstrings to document function arguments you may not want
to see :param: and :return: lines in your help text.

You can use the ``\f`` escape marker to have Click truncate the help text
after the marker.

Example:

.. click:example::

@click.command()
@click.pass_context
def cli(ctx):
"""First paragraph.

This is a very long second
paragraph and not correctly
wrapped but it will be rewrapped.
\f

:param click.core.Context ctx: Click context.
"""

And what it looks like:

.. click:run::

invoke(cli, args=['--help'])


Meta Variables
--------------

Expand Down
29 changes: 29 additions & 0 deletions tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,32 @@ def cmd(param):
'Options:',
' --help Show this message and exit.'
]


def test_truncating_docstring(runner):
@click.command()
@click.pass_context
def cli(ctx):
"""First paragraph.

This is a very long second
paragraph and not correctly
wrapped but it will be rewrapped.
\f

:param click.core.Context ctx: Click context.
"""

result = runner.invoke(cli, ['--help'], terminal_width=60)
assert not result.exception
assert result.output.splitlines() == [
'Usage: cli [OPTIONS]',
'',
' First paragraph.',
'',
' This is a very long second paragraph and not correctly',
' wrapped but it will be rewrapped.',
'',
'Options:',
' --help Show this message and exit.',
]