Skip to content

Commit 9d086ca

Browse files
committed
šŸ› fix(metavar): render all tuple metavar elements and suppress None defaults
Tuple metavars (e.g. nargs=2 with metavar=("KEY", "VALUE")) only rendered the first element. Now all elements are joined with spaces. Additionally, arguments with default=None no longer display "(default: None)" since that adds no useful information. Fixes #159, fixes #177
1 parent b745b17 commit 9d086ca

7 files changed

Lines changed: 43 additions & 11 deletions

File tree

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: 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
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="tool")
8+
parser.add_argument("--pair", nargs=2, metavar=("A", "B"), help="select a pair")
9+
parser.add_argument("--val", metavar="VAL", help="single val")
10+
return parser

ā€Žsrc/sphinx_argparse_cli/_logic.pyā€Ž

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,12 @@ def _mk_option_line(self, action: Action, prefix: str) -> list_item:
268268
self._mk_option_name(line, prefix, opt)
269269
if not is_flag:
270270
line += Text(" ")
271-
line += literal(text=as_key.upper())
271+
metavar_text = (
272+
" ".join(meta.upper() for meta in action.metavar)
273+
if isinstance(action.metavar, tuple)
274+
else as_key.upper()
275+
)
276+
line += literal(text=metavar_text)
272277
else:
273278
self._mk_option_name(line, prefix, as_key)
274279

@@ -282,6 +287,7 @@ def _mk_option_line(self, action: Action, prefix: str) -> list_item:
282287
line += content
283288
if (
284289
"no_default_values" not in self.options
290+
and action.default is not None
285291
and action.default != SUPPRESS
286292
and not re.match(r".*[ (]default[s]? .*", (action.help or ""))
287293
and not isinstance(action, _StoreTrueAction | _StoreFalseAction)

ā€Žtests/complex.txtā€Ž

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ complex options
1717
* **"--no-help"**
1818

1919
* **"--outdir"** "OUT_DIR", **"-o"** "OUT_DIR" - output directory
20-
(default: "None")
2120

2221
* **"--in-dir"** "IN_DIR", **"-i"** "IN_DIR" - input directory
23-
(default: "None")
2422

2523

2624
complex Exclusive
@@ -44,7 +42,7 @@ a-first-desc
4442
complex first positional arguments
4543
----------------------------------
4644

47-
* **"one"** - first positional argument (default: "None")
45+
* **"one"** - first positional argument
4846

4947
* **"pos_two"** - second positional argument (default: "1")
5048

@@ -68,7 +66,7 @@ complex second
6866
complex second positional arguments
6967
-----------------------------------
7068

71-
* **"one"** - first positional argument (default: "None")
69+
* **"one"** - first positional argument
7270

7371
* **"pos_two"** - second positional argument (default: "green")
7472

ā€Žtests/complex_pre_310.txtā€Ž

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ complex optional arguments
1717
* **"--no-help"**
1818

1919
* **"--outdir"** "OUT_DIR", **"-o"** "OUT_DIR" - output directory
20-
(default: "None")
2120

2221
* **"--in-dir"** "IN_DIR", **"-i"** "IN_DIR" - input directory
23-
(default: "None")
2422

2523

2624
complex Exclusive
@@ -44,7 +42,7 @@ a-first-desc
4442
complex first positional arguments
4543
----------------------------------
4644

47-
* **"one"** - first positional argument (default: "None")
45+
* **"one"** - first positional argument
4846

4947
* **"pos_two"** - second positional argument (default: "1")
5048

@@ -68,7 +66,7 @@ complex second
6866
complex second positional arguments
6967
-----------------------------------
7068

71-
* **"one"** - first positional argument (default: "None")
69+
* **"one"** - first positional argument
7270

7371
* **"pos_two"** - second positional argument (default: "green")
7472

ā€Žtests/test_logic.pyā€Ž

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,9 @@ def test_nargs(build_outcome: str) -> None:
366366
assert "pos_optional" in build_outcome
367367
assert "pos_zero_or_more" in build_outcome
368368
assert "pos_one_or_more" in build_outcome
369-
assert "KEY" in build_outcome
370-
assert "VALUE" in build_outcome
369+
assert "KEY VALUE" in build_outcome
370+
assert "(default: " in build_outcome # default_val is not None, should show
371+
assert 'default: "None"' not in build_outcome
371372

372373

373374
@pytest.mark.sphinx(buildername="text", testroot="choices")
@@ -381,3 +382,11 @@ def test_actions(build_outcome: str) -> None:
381382
assert "increase verbosity" in build_outcome
382383
assert "paths to include" in build_outcome
383384
assert "a required optional argument" in build_outcome
385+
386+
387+
@pytest.mark.sphinx(buildername="text", testroot="tuple-metavar")
388+
def test_tuple_metavar(build_outcome: str) -> None:
389+
assert '"A B"' in build_outcome or "A B" in build_outcome
390+
assert "select a pair" in build_outcome
391+
assert "default: None" not in build_outcome
392+
assert '"VAL"' in build_outcome

0 commit comments

Comments
Ā (0)