Skip to content

Commit 470dac4

Browse files
mondejagaborbernat
andauthored
Implement 'title' directive argument (#3)
Co-authored-by: Bernát Gábor <[email protected]>
1 parent 33be00d commit 470dac4

File tree

9 files changed

+63
-3
lines changed

9 files changed

+63
-3
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ extensions = ["sphinx_argparse_cli"]
3131

3232
## use
3333

34-
Within the reStructuredText files use the `sphinx_argparse_cli` directive that takes two arguments:
34+
Within the reStructuredText files use the `sphinx_argparse_cli` directive that takes, at least, two arguments:
3535

3636
- the module path to where the parser is defined,
3737
- a no argument function within that module that once called returns the constructed
3838
[argparse](https://docs.python.org/3/library/argparse.html) parser
3939
- (optional) a program name that overwrites the autodiscovered running argument parser
40+
- (optional) a `:title:` argument which, when provided, overwrites the
41+
`<prog> - CLI interface` title added by default and when empty, will not be included
4042

4143
```rst
4244
.. sphinx_argparse_cli::

roots/test-title-empty/conf.py

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

roots/test-title-empty/index.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.. sphinx_argparse_cli::
2+
:module: parser
3+
:func: make
4+
:title:

roots/test-title-empty/parser.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from __future__ import annotations
2+
3+
from argparse import ArgumentParser
4+
5+
6+
def make() -> ArgumentParser:
7+
return ArgumentParser(prog="foo", add_help=False)

roots/test-title-set/conf.py

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

roots/test-title-set/index.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.. sphinx_argparse_cli::
2+
:module: parser
3+
:func: make
4+
:title: My own title

roots/test-title-set/parser.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from __future__ import annotations
2+
3+
from argparse import ArgumentParser
4+
5+
6+
def make() -> ArgumentParser:
7+
return ArgumentParser(prog="foo", add_help=False)

src/sphinx_argparse_cli/_logic.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from typing import Iterator, Set, cast
1616

1717
from docutils.nodes import (
18+
Element,
1819
Node,
1920
Text,
2021
bullet_list,
@@ -45,6 +46,7 @@ class SphinxArgparseCli(SphinxDirective):
4546
"module": unchanged_required,
4647
"func": unchanged_required,
4748
"prog": unchanged,
49+
"title": unchanged,
4850
}
4951

5052
def __init__(
@@ -97,8 +99,12 @@ def load_sub_parsers(self) -> Iterator[tuple[list[str], str, ArgumentParser]]:
9799

98100
def run(self) -> list[Node]:
99101
# construct headers
100-
title_text = f"{self.parser.prog} - CLI interface"
101-
home_section = section("", title("", Text(title_text)), ids=[make_id(title_text)], names=[title_text])
102+
title_text = self.options.get("title", f"{self.parser.prog} - CLI interface").strip()
103+
if title_text.strip() == "":
104+
home_section: Element = paragraph()
105+
else:
106+
home_section = section("", title("", Text(title_text)), ids=[make_id(title_text)], names=[title_text])
107+
102108
if self.parser.description:
103109
desc_paragraph = paragraph("", Text(self.parser.description))
104110
home_section += desc_paragraph

tests/test_logic.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,17 @@ def test_prog_as_text(app: SphinxTestApp) -> None:
2525
app.build()
2626
outcome = (Path(app.outdir) / "index.txt").read_text()
2727
assert outcome == "magic - CLI interface\n*********************\n\n magic\n"
28+
29+
30+
@pytest.mark.sphinx("text", testroot="title-set")
31+
def test_set_title_as_text(app: SphinxTestApp) -> None:
32+
app.build()
33+
outcome = (Path(app.outdir) / "index.txt").read_text()
34+
assert outcome == "My own title\n************\n\n foo\n"
35+
36+
37+
@pytest.mark.sphinx("text", testroot="title-empty")
38+
def test_empty_title_as_text(app: SphinxTestApp) -> None:
39+
app.build()
40+
outcome = (Path(app.outdir) / "index.txt").read_text()
41+
assert outcome == " foo\n"

0 commit comments

Comments
 (0)