Skip to content

Commit

Permalink
Deduplicate help block formatting code
Browse files Browse the repository at this point in the history
After fetching the help string, the bodies of `_format_description()`
and `_format_epilog()` were identical.
  • Loading branch information
mthuurne authored and stephenfin committed Jan 27, 2022
1 parent 69da77c commit 42cc68c
Showing 1 changed file with 14 additions and 27 deletions.
41 changes: 14 additions & 27 deletions sphinx_click/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,7 @@ def _write_opts(opts):
return ', '.join(rv), '\n'.join(out)


def _format_description(ctx):
"""Format the description for a given `click.Command`.
We parse this as reStructuredText, allowing users to embed rich
information in their help messages if they so choose.
"""
help_string = ctx.command.help or ctx.command.short_help
if not help_string:
return

def _format_help(help_string):
help_string = ANSI_ESC_SEQ_RE.sub('', help_string)

bar_enabled = False
Expand All @@ -127,6 +118,17 @@ def _format_description(ctx):
yield ''


def _format_description(ctx):
"""Format the description for a given `click.Command`.
We parse this as reStructuredText, allowing users to embed rich
information in their help messages if they so choose.
"""
help_string = ctx.command.help or ctx.command.short_help
if help_string:
yield from _format_help(help_string)


def _format_usage(ctx):
"""Format the usage for a `click.Command`."""
yield '.. code-block:: shell'
Expand Down Expand Up @@ -236,23 +238,8 @@ def _format_epilog(ctx):
We parse this as reStructuredText, allowing users to embed rich
information in their help messages if they so choose.
"""
if not ctx.command.epilog:
return

bar_enabled = False
for line in statemachine.string2lines(
ANSI_ESC_SEQ_RE.sub('', ctx.command.epilog),
tab_width=4,
convert_whitespace=True,
):
if line == '\b':
bar_enabled = True
continue
if line == '':
bar_enabled = False
line = '| ' + line if bar_enabled else line
yield line
yield ''
if ctx.command.epilog:
yield from _format_help(ctx.command.epilog)


def _get_lazyload_commands(multicommand):
Expand Down

0 comments on commit 42cc68c

Please sign in to comment.