Skip to content

Commit 84b2151

Browse files
authored
Allow omitting default values (#56)
1 parent 08c0468 commit 84b2151

File tree

7 files changed

+50
-2
lines changed

7 files changed

+50
-2
lines changed

CHANGELOG.md

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

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

5+
## 1.10.0
6+
7+
- Add the option to override the generated `default` part of the CLI parameter description.
8+
59
## 1.9.0
610

711
- Add the option to override the description using the `description` attribute of the directive

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ 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) when provided, overwrites the `<prog>` name. | |
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 |
3939
| description | (optional) when provided, overwrites the description and when empty, will not be included |
4040
| usage_width | (optional) how large should usage examples be - defaults to 100 character |
4141
| group_title_prefix | (optional) groups subsections title prefixes, accepts the string `{prog}` as a replacement for the program name - defaults to `{prog}` |
4242
| 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}` |
43+
| no_default_values | (optional) supresses generation of `default` entries |
4344

4445
For example:
4546

roots/test-default-handling/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-default-handling/index.rst

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

roots/test-default-handling/parser.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from __future__ import annotations
2+
3+
from argparse import ArgumentParser
4+
5+
6+
def main() -> None:
7+
parser = ArgumentParser(prog="foo", add_help=False)
8+
parser.add_argument("x", default=1, help="arg (default: True)")
9+
args = parser.parse_args()
10+
print(args)

src/sphinx_argparse_cli/_logic.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class SphinxArgparseCli(SphinxDirective):
6868
"usage_width": positive_int,
6969
"group_title_prefix": unchanged,
7070
"group_sub_title_prefix": unchanged,
71+
"no_default_values": unchanged,
7172
}
7273

7374
def __init__(
@@ -231,7 +232,8 @@ def _mk_option_line(self, action: Action, prefix: str) -> list_item:
231232
for content in cast(paragraph, temp.children[0]).children:
232233
line += content
233234
if (
234-
action.default != SUPPRESS
235+
"no_default_values" not in self.options
236+
and action.default != SUPPRESS
235237
and not re.match(r".*[ (]default[s]? .*", (action.help or ""))
236238
and not isinstance(action, (_StoreTrueAction, _StoreFalseAction))
237239
):

tests/test_logic.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,21 @@ def test_lower_upper_refs(build_outcome: str, warning: StringIO) -> None:
233233
assert '<p id="basic--d"><a class="reference internal" href="#basic--d" title="basic -d">' in build_outcome
234234
assert '<p id="basic--D"><a class="reference internal" href="#basic--D" title="basic -D">' in build_outcome
235235
assert not warning.getvalue()
236+
237+
238+
@pytest.mark.sphinx(buildername="text", testroot="default-handling")
239+
def test_with_default(build_outcome: str) -> None:
240+
assert (
241+
build_outcome
242+
== """foo - CLI interface
243+
*******************
244+
245+
foo x
246+
247+
248+
foo positional arguments
249+
========================
250+
251+
* **"x"** - arg (default: True)
252+
"""
253+
)

0 commit comments

Comments
 (0)