Skip to content

Commit fc9ec21

Browse files
aeisenbarthstephenfin
authored andcommitted
Interpret help for arguments
Closes: #132
1 parent 9dcaf75 commit fc9ec21

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

sphinx_click/ext.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,12 @@ def _format_argument(arg: click.Argument) -> ty.Generator[str, None, None]:
210210
'Required' if arg.required else 'Optional', '(s)' if arg.nargs != 1 else ''
211211
)
212212
)
213+
# Subclasses of click.Argument may add a `help` attribute (like typer.main.TyperArgument)
214+
if hasattr(arg, 'help'):
215+
yield ''
216+
help_string = ANSI_ESC_SEQ_RE.sub('', getattr(arg, 'help'))
217+
for line in _format_help(help_string):
218+
yield _indent(line)
213219

214220

215221
@_process_lines("sphinx-click-process-arguments")

tests/test_formatter.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,57 @@ def foobar(bar):
170170
'\n'.join(output),
171171
)
172172

173+
def test_help_argument(self):
174+
"""Validate a help text for arguments.
175+
176+
While click only provides the help attribute for options, but not for arguments,
177+
it allows customization with subclasses.
178+
"""
179+
180+
class CustomArgument(click.Argument):
181+
def __init__(self, *args, help=None, **kwargs):
182+
super().__init__(*args, **kwargs)
183+
self.help = help
184+
185+
@click.command()
186+
@click.option('--option', help='A sample option')
187+
@click.argument('ARG', help='A sample argument', cls=CustomArgument)
188+
def foobar(bar):
189+
"""A sample command."""
190+
pass
191+
192+
ctx = click.Context(foobar, info_name='foobar')
193+
output = list(ext._format_command(ctx, nested='short'))
194+
195+
self.assertEqual(
196+
textwrap.dedent(
197+
"""
198+
A sample command.
199+
200+
.. program:: foobar
201+
.. code-block:: shell
202+
203+
foobar [OPTIONS] ARG
204+
205+
.. rubric:: Options
206+
207+
.. option:: --option <option>
208+
209+
A sample option
210+
211+
.. rubric:: Arguments
212+
213+
.. option:: ARG
214+
215+
Required argument
216+
217+
A sample argument
218+
219+
"""
220+
).lstrip(),
221+
'\n'.join(output),
222+
)
223+
173224
def test_defaults(self):
174225
"""Validate formatting of user documented defaults."""
175226

0 commit comments

Comments
 (0)