Skip to content

Commit bb9e491

Browse files
Support rendering with argparse.RawDescriptionHelpFormatter (#144)
* Create literal_block for multiline description * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 8aa7d16 commit bb9e491

File tree

5 files changed

+46
-2
lines changed

5 files changed

+46
-2
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from __future__ import annotations
2+
3+
import sys
4+
from pathlib import Path
5+
6+
sys.path.insert(0, str(Path(__file__).parent))
7+
extensions = ["sphinx_argparse_cli"]
8+
nitpicky = True
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.. sphinx_argparse_cli::
2+
:module: parser
3+
:func: make
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from __future__ import annotations
2+
3+
from argparse import ArgumentParser, RawDescriptionHelpFormatter
4+
5+
6+
def make() -> ArgumentParser:
7+
return ArgumentParser(
8+
prog="foo",
9+
description="""This description
10+
spans multiple lines.
11+
12+
this line is indented.
13+
and also this.
14+
15+
Now this should be a separate paragraph.
16+
""",
17+
formatter_class=RawDescriptionHelpFormatter,
18+
add_help=False,
19+
)

src/sphinx_argparse_cli/_logic.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Action,
88
ArgumentParser,
99
HelpFormatter,
10+
RawDescriptionHelpFormatter,
1011
_ArgumentGroup,
1112
_StoreFalseAction,
1213
_StoreTrueAction,
@@ -148,8 +149,12 @@ def run(self) -> list[Node]:
148149

149150
description = self.options.get("description", self.parser.description)
150151
if description:
151-
desc_paragraph = paragraph("", Text(description))
152-
home_section += desc_paragraph
152+
if isinstance(self.parser.formatter_class(""), RawDescriptionHelpFormatter) and "\n" in description:
153+
lit = literal_block("", Text(description))
154+
lit["language"] = "none"
155+
home_section += lit
156+
else:
157+
home_section += paragraph("", Text(description))
153158
# construct groups excluding sub-parsers
154159
home_section += self._mk_usage(self.parser)
155160
for group in self.parser._action_groups: # noqa: SLF001

tests/test_logic.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ def test_empty_description_as_text(build_outcome: str) -> None:
9999
assert build_outcome == "foo - CLI interface\n*******************\n\n foo\n"
100100

101101

102+
@pytest.mark.sphinx(buildername="html", testroot="description-multiline")
103+
def test_multiline_description_as_html(build_outcome: str) -> None:
104+
ref = (
105+
"This description\nspans multiple lines.\n\n this line is indented.\n and also this.\n\nNow this should be"
106+
" a separate paragraph.\n"
107+
)
108+
assert ref in build_outcome
109+
110+
102111
@pytest.mark.sphinx(buildername="text", testroot="complex")
103112
@pytest.mark.prepare(directive_args=[":usage_width: 100"])
104113
def test_usage_width_default(build_outcome: str) -> None:

0 commit comments

Comments
 (0)