Skip to content

Commit 827dab4

Browse files
authored
Add option to override description (#45)
* Add option to override description * Update changelog * Update CHANGELOG.md
1 parent 727964b commit 827dab4

File tree

10 files changed

+60
-3
lines changed

10 files changed

+60
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## 1.9.0
6+
7+
- Add the option to override the description using the `description` attribute of the directive
8+
- Add the option to retrieve the arguments by hooking argparse, in cases where `func` consumes the arguments
9+
and does not return them
10+
511
## 1.8.2
612

713
- Don't raise label clashing warnings for options which only differ between upper and lower case

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ Within the reStructuredText files use the `sphinx_argparse_cli` directive that t
3333
| ---------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
3434
| module | the module path to where the parser is defined |
3535
| func | the name of the function that once called with no arguments constructs the parser |
36-
| prog | (optional) the module path to where the parser is defined |
36+
| prog | (optional) when provided, overwrites the `<prog>` name. | |
3737
| hook | (optional) hook `argparse` to retrieve the parser if `func` uses a parser instead of returning it. |
3838
| title | (optional) when provided, overwrites the `<prog> - CLI interface` title added by default and when empty, will not be included |
39+
| description | (optional) when provided, overwrites the description and when empty, will not be included |
3940
| usage_width | (optional) how large should usage examples be - defaults to 100 character |
4041
| group_title_prefix | (optional) groups subsections title prefixes, accepts the string `{prog}` as a replacement for the program name - defaults to `{prog}` |
4142
| group_sub_title_prefix | (optional) subcommands groups subsections title prefixes, accepts replacement of `{prog}` and `{subcommand}` for program and subcommand name - defaults to `{prog} {subcommand}` |

roots/test-description-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
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+
:description:
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", description="desc", add_help=False)

roots/test-description-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-description-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+
:description: My own description

roots/test-description-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", description="desc", add_help=False)

src/sphinx_argparse_cli/_logic.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class SphinxArgparseCli(SphinxDirective):
6464
"hook": flag,
6565
"prog": unchanged,
6666
"title": unchanged,
67+
"description": unchanged,
6768
"usage_width": positive_int,
6869
"group_title_prefix": unchanged,
6970
"group_sub_title_prefix": unchanged,
@@ -143,8 +144,9 @@ def run(self) -> list[Node]:
143144
else:
144145
home_section = section("", title("", Text(title_text)), ids=[make_id(title_text)], names=[title_text])
145146

146-
if self.parser.description:
147-
desc_paragraph = paragraph("", Text(self.parser.description))
147+
description = self.options.get("description", self.parser.description)
148+
if description:
149+
desc_paragraph = paragraph("", Text(description))
148150
home_section += desc_paragraph
149151
# construct groups excluding sub-parsers
150152
home_section += self._mk_usage(self.parser)

tests/test_logic.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ def test_empty_title_as_text(build_outcome: str) -> None:
8686
assert build_outcome == " foo\n"
8787

8888

89+
@pytest.mark.sphinx(buildername="text", testroot="description-set")
90+
def test_set_description_as_text(build_outcome: str) -> None:
91+
assert build_outcome == "foo - CLI interface\n*******************\n\nMy own description\n\n foo\n"
92+
93+
94+
@pytest.mark.sphinx(buildername="text", testroot="description-empty")
95+
def test_empty_description_as_text(build_outcome: str) -> None:
96+
assert build_outcome == "foo - CLI interface\n*******************\n\n foo\n"
97+
98+
8999
@pytest.mark.sphinx(buildername="text", testroot="complex")
90100
@pytest.mark.prepare(directive_args=[":usage_width: 100"])
91101
def test_usage_width_default(build_outcome: str) -> None:

0 commit comments

Comments
 (0)