Skip to content

Commit

Permalink
Interpret help for arguments
Browse files Browse the repository at this point in the history
Closes: #132
  • Loading branch information
aeisenbarth authored and stephenfin committed Apr 5, 2024
1 parent 9dcaf75 commit fc9ec21
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
6 changes: 6 additions & 0 deletions sphinx_click/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ def _format_argument(arg: click.Argument) -> ty.Generator[str, None, None]:
'Required' if arg.required else 'Optional', '(s)' if arg.nargs != 1 else ''
)
)
# Subclasses of click.Argument may add a `help` attribute (like typer.main.TyperArgument)
if hasattr(arg, 'help'):
yield ''
help_string = ANSI_ESC_SEQ_RE.sub('', getattr(arg, 'help'))
for line in _format_help(help_string):
yield _indent(line)


@_process_lines("sphinx-click-process-arguments")
Expand Down
51 changes: 51 additions & 0 deletions tests/test_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,57 @@ def foobar(bar):
'\n'.join(output),
)

def test_help_argument(self):
"""Validate a help text for arguments.
While click only provides the help attribute for options, but not for arguments,
it allows customization with subclasses.
"""

class CustomArgument(click.Argument):
def __init__(self, *args, help=None, **kwargs):
super().__init__(*args, **kwargs)
self.help = help

@click.command()
@click.option('--option', help='A sample option')
@click.argument('ARG', help='A sample argument', cls=CustomArgument)
def foobar(bar):
"""A sample command."""
pass

ctx = click.Context(foobar, info_name='foobar')
output = list(ext._format_command(ctx, nested='short'))

self.assertEqual(
textwrap.dedent(
"""
A sample command.
.. program:: foobar
.. code-block:: shell
foobar [OPTIONS] ARG
.. rubric:: Options
.. option:: --option <option>
A sample option
.. rubric:: Arguments
.. option:: ARG
Required argument
A sample argument
"""
).lstrip(),
'\n'.join(output),
)

def test_defaults(self):
"""Validate formatting of user documented defaults."""

Expand Down

0 comments on commit fc9ec21

Please sign in to comment.