Skip to content

Commit 33be00d

Browse files
authored
Add option to especify 'prog' (#1)
1 parent cce9329 commit 33be00d

File tree

6 files changed

+38
-4
lines changed

6 files changed

+38
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ Within the reStructuredText files use the `sphinx_argparse_cli` directive that t
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
39+
- (optional) a program name that overwrites the autodiscovered running argument parser
3940

4041
```rst
4142
.. sphinx_argparse_cli::
4243
:module: a_project.cli
4344
:func: build_parser
45+
:prog: my-cli-program
4446
```

roots/test-prog/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-prog/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+
:prog: magic

roots/test-prog/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(add_help=False)

src/sphinx_argparse_cli/_logic.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
section,
2727
title,
2828
)
29-
from docutils.parsers.rst.directives import unchanged_required
29+
from docutils.parsers.rst.directives import unchanged, unchanged_required
3030
from docutils.parsers.rst.states import RSTState, RSTStateMachine
3131
from docutils.statemachine import StringList
3232
from sphinx.util.docutils import SphinxDirective
@@ -41,7 +41,11 @@ def make_id(key: str) -> str:
4141
class SphinxArgparseCli(SphinxDirective):
4242
name = "sphinx_argparse_cli"
4343
has_content = False
44-
option_spec = {"module": unchanged_required, "func": unchanged_required}
44+
option_spec = {
45+
"module": unchanged_required,
46+
"func": unchanged_required,
47+
"prog": unchanged,
48+
}
4549

4650
def __init__(
4751
self,
@@ -64,6 +68,8 @@ def parser(self) -> ArgumentParser:
6468
module_name, attr_name = self.options["module"], self.options["func"]
6569
parser_creator = getattr(__import__(module_name, fromlist=[attr_name]), attr_name)
6670
self._parser = parser_creator()
71+
if "prog" in self.options:
72+
self._parser.prog = self.options["prog"]
6773
del sys.modules[module_name] # no longer needed cleanup
6874
return self._parser
6975

tests/test_logic.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,21 @@
77

88

99
@pytest.mark.sphinx("html", testroot="basic")
10-
def test_basic_as_text(app: SphinxTestApp) -> None:
10+
def test_basic_as_html(app: SphinxTestApp) -> None:
1111
app.build()
1212
outcome = (Path(app.outdir) / "index.html").read_text()
1313
assert outcome
1414

1515

1616
@pytest.mark.sphinx("html", testroot="complex")
17-
def test_complex_as_text(app: SphinxTestApp) -> None:
17+
def test_complex_as_html(app: SphinxTestApp) -> None:
1818
app.build()
1919
outcome = (Path(app.outdir) / "index.html").read_text()
2020
assert outcome
21+
22+
23+
@pytest.mark.sphinx("text", testroot="prog")
24+
def test_prog_as_text(app: SphinxTestApp) -> None:
25+
app.build()
26+
outcome = (Path(app.outdir) / "index.txt").read_text()
27+
assert outcome == "magic - CLI interface\n*********************\n\n magic\n"

0 commit comments

Comments
 (0)