Skip to content

Commit 47869f6

Browse files
authored
Fix %(default)s style when help markup is deactivated (#123)
Fixes #121 (comment)
1 parent 7af07e1 commit 47869f6

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

CHANGELOG.md

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

33
## Unreleased
44

5+
### Fixes
6+
- [GH-121](https://github.com/hamdanal/rich-argparse/issues/115),
7+
[PR-123](https://github.com/hamdanal/rich-argparse/pull/116)
8+
Fix `%(default)s` style when help markup is deactivated.
9+
510
## 1.5.0 - 2024-06-01
611

712
### Features

rich_argparse/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,13 +370,14 @@ def _rich_expand_help(self, action: Action) -> r.Text:
370370
# raise ValueError if needed
371371
help_string % params # pyright: ignore[reportUnusedExpression]
372372
parts = []
373+
default_span: r.Span | None = None
373374
last = 0
374375
for m in self._printf_style_pattern.finditer(help_string):
375376
start, end = m.span()
376377
parts.append(help_string[last:start])
377378
sub = r.escape(help_string[start:end] % params)
378379
if m.group("mapping") == "default":
379-
sub = f"[argparse.default]{sub}[/argparse.default]"
380+
default_span = r.Span(start, start + len(sub), "argparse.default")
380381
parts.append(sub)
381382
last = end
382383
parts.append(help_string[last:])
@@ -385,6 +386,8 @@ def _rich_expand_help(self, action: Action) -> r.Text:
385386
if self.help_markup
386387
else r.Text("".join(parts), style="argparse.help")
387388
)
389+
if default_span:
390+
rich_help.spans.append(default_span)
388391
for highlight in self.highlights:
389392
rich_help.highlight_regex(highlight, style_prefix="argparse.")
390393
return rich_help

tests/test_argparse.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,7 @@ def test_disable_help_markup():
10061006
parser = ArgumentParser(
10071007
prog="PROG", formatter_class=RichHelpFormatter, description="[red]Description text.[/]"
10081008
)
1009-
parser.add_argument("--foo", help="[red]Help text.[/]")
1009+
parser.add_argument("--foo", default="def", help="[red]Help text (default: %(default)s).[/]")
10101010
with patch.object(RichHelpFormatter, "help_markup", False):
10111011
help_text = parser.format_help()
10121012
expected_help_text = """\
@@ -1016,7 +1016,7 @@ def test_disable_help_markup():
10161016
10171017
Optional Arguments:
10181018
-h, --help show this help message and exit
1019-
--foo FOO [red]Help text.[/]
1019+
--foo FOO [red]Help text (default: def).[/]
10201020
"""
10211021
assert help_text == clean(expected_help_text)
10221022

0 commit comments

Comments
 (0)