Skip to content

Commit 2637f83

Browse files
authored
Don't render default values for store_true and store_false actions (#30)
1 parent 9f7220a commit 2637f83

File tree

9 files changed

+56
-17
lines changed

9 files changed

+56
-17
lines changed

CHANGELOG.md

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

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

5+
## 1.8.1
6+
7+
- Fix reference clashing for options which only differ between upper and lower case
8+
- Do not render default values for `store_true` and `store_false` actions
9+
510
## 1.6.0 (2021-04-15)
611

712
- Support for using the `ref` sphinx role to refer to all anchor-able objects generated by the tool

roots/test-store-true-false/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-store-true-false/index.rst

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

roots/test-store-true-false/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 make() -> ArgumentParser:
7+
parser = ArgumentParser(prog="basic")
8+
parser.add_argument("--no-foo", dest="foo", action="store_false")
9+
parser.add_argument("--bar", dest="bar", action="store_true")
10+
return parser

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ noqa-require-code = true
6363
dictionaries = en_US,python,technical
6464
ignore =
6565
E203 # whitespace before :
66+
W503 # line break before binary operator
6667

6768
[coverage:report]
6869
show_missing = True

src/sphinx_argparse_cli/_logic.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
ArgumentParser,
1010
HelpFormatter,
1111
_ArgumentGroup,
12+
_StoreFalseAction,
13+
_StoreTrueAction,
1214
_SubParsersAction,
1315
)
1416
from collections import defaultdict, namedtuple
@@ -209,7 +211,11 @@ def _mk_option_line(self, action: Action, prefix: str) -> list_item:
209211
line += Text(" - ")
210212
for content in cast(paragraph, temp.children[0]).children:
211213
line += content
212-
if action.default != SUPPRESS and not re.match(r".*[ (]default[s]? .*", (action.help or "")):
214+
if (
215+
action.default != SUPPRESS
216+
and not re.match(r".*[ (]default[s]? .*", (action.help or ""))
217+
and not isinstance(action, (_StoreTrueAction, _StoreFalseAction))
218+
):
213219
line += Text(" (default: ")
214220
line += literal(text=str(action.default).replace(os.getcwd(), "{cwd}"))
215221
line += Text(")")

tests/complex.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ complex options
1212

1313
* **"-h"**, **"--help"** - show this help message and exit
1414

15-
* **"--root"** - root flag (default: "False")
15+
* **"--root"** - root flag
1616

17-
* **"--no-help"** (default: "False")
17+
* **"--no-help"**
1818

1919
* **"--outdir"** "OUT_DIR", **"-o"** "OUT_DIR" - output directory
2020
(default: "None")
@@ -28,9 +28,9 @@ complex Exclusive
2828

2929
this is an exclusive group
3030

31-
* **"--foo"** - foo (default: "False")
31+
* **"--foo"** - foo
3232

33-
* **"--bar"** - bar (default: "False")
33+
* **"--bar"** - bar
3434

3535

3636
complex first (f)
@@ -54,9 +54,9 @@ complex first options
5454

5555
* **"-h"**, **"--help"** - show this help message and exit
5656

57-
* **"--flag"** - a parser first flag (default: "False")
57+
* **"--flag"** - a parser first flag
5858

59-
* **"--root"** - root flag (default: "False")
59+
* **"--root"** - root flag
6060

6161

6262
complex second
@@ -78,9 +78,9 @@ complex second options
7878

7979
* **"-h"**, **"--help"** - show this help message and exit
8080

81-
* **"--flag"** - a parser second flag (default: "False")
81+
* **"--flag"** - a parser second flag
8282

83-
* **"--root"** - root flag (default: "False")
83+
* **"--root"** - root flag
8484

8585

8686
complex third

tests/complex_pre_310.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ complex optional arguments
1212

1313
* **"-h"**, **"--help"** - show this help message and exit
1414

15-
* **"--root"** - root flag (default: "False")
15+
* **"--root"** - root flag
1616

17-
* **"--no-help"** (default: "False")
17+
* **"--no-help"**
1818

1919
* **"--outdir"** "OUT_DIR", **"-o"** "OUT_DIR" - output directory
2020
(default: "None")
@@ -28,9 +28,9 @@ complex Exclusive
2828

2929
this is an exclusive group
3030

31-
* **"--foo"** - foo (default: "False")
31+
* **"--foo"** - foo
3232

33-
* **"--bar"** - bar (default: "False")
33+
* **"--bar"** - bar
3434

3535

3636
complex first (f)
@@ -54,9 +54,9 @@ complex first optional arguments
5454

5555
* **"-h"**, **"--help"** - show this help message and exit
5656

57-
* **"--flag"** - a parser first flag (default: "False")
57+
* **"--flag"** - a parser first flag
5858

59-
* **"--root"** - root flag (default: "False")
59+
* **"--root"** - root flag
6060

6161

6262
complex second
@@ -78,9 +78,9 @@ complex second optional arguments
7878

7979
* **"-h"**, **"--help"** - show this help message and exit
8080

81-
* **"--flag"** - a parser second flag (default: "False")
81+
* **"--flag"** - a parser second flag
8282

83-
* **"--root"** - root flag (default: "False")
83+
* **"--root"** - root flag
8484

8585

8686
complex third

tests/test_logic.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,9 @@ def test_group_title_prefix_sub_command_replacement(build_outcome: str, opt_grp_
201201
assert '<h2>bar Exclusive<a class="headerlink" href="#bar-exclusive"' in build_outcome
202202
assert '<h2>bar baronlyroot (f)<a class="headerlink" href="#bar-root-first-(f)"' in build_outcome
203203
assert '<h3>bar baronlyroot first positional arguments<a class="headerlink"' in build_outcome
204+
205+
206+
@pytest.mark.sphinx(buildername="html", testroot="store-true-false")
207+
def test_store_true_false(build_outcome: str) -> None:
208+
assert "False" not in build_outcome
209+
assert "True" not in build_outcome

0 commit comments

Comments
 (0)