Skip to content

Commit

Permalink
FIX If the help of an argument is None, don't fail
Browse files Browse the repository at this point in the history
This is a fix for a regression introduced in click-contrib#134. It's possible that the
argument has a `help` attribute, but the value of it is `None`. We then hit a
type error where we pass `None` to `re.sub`.
  • Loading branch information
hoodmane committed May 9, 2024
1 parent e8cf016 commit b193cd6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
5 changes: 3 additions & 2 deletions sphinx_click/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,10 @@ def _format_argument(arg: click.Argument) -> ty.Generator[str, None, None]:
)
)
# Subclasses of click.Argument may add a `help` attribute (like typer.main.TyperArgument)
if hasattr(arg, 'help'):
help = getattr(arg, 'help', None)
if help:
yield ''
help_string = ANSI_ESC_SEQ_RE.sub('', getattr(arg, 'help'))
help_string = ANSI_ESC_SEQ_RE.sub('', help)
for line in _format_help(help_string):
yield _indent(line)

Expand Down
7 changes: 6 additions & 1 deletion tests/test_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def __init__(self, *args, help=None, **kwargs):
@click.command()
@click.option('--option', help='A sample option')
@click.argument('ARG', help='A sample argument', cls=CustomArgument)
@click.argument('ARG_NO_HELP', cls=CustomArgument)
def foobar(bar):
"""A sample command."""
pass
Expand All @@ -200,7 +201,7 @@ def foobar(bar):
.. program:: foobar
.. code-block:: shell
foobar [OPTIONS] ARG
foobar [OPTIONS] ARG ARG_NO_HELP
.. rubric:: Options
Expand All @@ -216,6 +217,10 @@ def foobar(bar):
A sample argument
.. option:: ARG_NO_HELP
Required argument
"""
).lstrip(),
'\n'.join(output),
Expand Down

0 comments on commit b193cd6

Please sign in to comment.