Skip to content

Commit ef87d9d

Browse files
authored
Tweak warning message for an edge case of default in markup (#154)
1 parent 0afba0a commit ef87d9d

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
### Fixes
1616
- [GH-152](https://github.com/hamdanal/rich-argparse/issues/152),
17-
[PR-153](https://github.com/hamdanal/rich-argparse/pull/153)
17+
[PR-153](https://github.com/hamdanal/rich-argparse/pull/153),
18+
[PR-154](https://github.com/hamdanal/rich-argparse/pull/154)
1819
Fix `ValueError` when using `%(default)s` inside square brackets and `help_markup` is enabled.
1920
- [GH-141](https://github.com/hamdanal/rich-argparse/issues/141),
2021
[PR-142](https://github.com/hamdanal/rich-argparse/pull/142)

rich_argparse/_argparse.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -452,13 +452,14 @@ def _rich_expand_help(self, action: Action) -> r.Text:
452452
import warnings
453453

454454
action_id = next(iter(action.option_strings), action.dest)
455-
md = re.search(
456-
rf"\[([^\]]*{self._printf_style_pattern.pattern}[^\]]*)\]", help_string, re.X
457-
)
458-
repl = (
459-
repr(md.group(1))[1:-1]
460-
if md and md.group("mapping") == "default"
461-
else "default: %(default)s"
455+
printf_pat = self._printf_style_pattern.pattern
456+
repl = next(
457+
(
458+
repr(m.group(1))[1:-1]
459+
for m in re.finditer(rf"\[([^\]]*{printf_pat}[^\]]*)\]", help_string, re.X)
460+
if m.group("mapping") == "default"
461+
),
462+
"default: %(default)s", # pragma: >=3.9 cover # fails on Python 3.8!
462463
)
463464
msg = (
464465
f"Failed to process default value in help string of argument {action_id!r}."

tests/test_argparse.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,23 +1028,23 @@ def test_arg_default_in_markup():
10281028
"--foo",
10291029
default="def",
10301030
help=(
1031-
"(default: %(default)r)[default wrong: %(default)r] text [default wrong: %(default)s]"
1032-
"(default: %(default)s) [link default wrong %(default)s] %(default)s"
1031+
"[%(type)r type](good: %(default)r)[bad: %(default)r] text [bad: %(default)s]"
1032+
"(good: %(default)s) [link bad %(default)s] %(default)s"
10331033
),
10341034
)
10351035
expected_help_text = """\
10361036
Usage: PROG [-h] [--foo FOO]
10371037
10381038
Optional Arguments:
10391039
-h, --help show this help message and exit
1040-
--foo FOO (default: 'def') text (default: def) def
1040+
--foo FOO [None type](good: 'def') text (good: def) def
10411041
"""
10421042
with pytest.warns(
10431043
UserWarning,
10441044
match=re.escape(
10451045
"Failed to process default value in help string of argument '--foo'.\n"
10461046
"Hint: try disabling rich markup: `RichHelpFormatter.help_markup = False`\n"
1047-
" or replace brackets by parenthesis: `[default wrong: %(default)r]` -> `(default wrong: %(default)r)`"
1047+
" or replace brackets by parenthesis: `[bad: %(default)r]` -> `(bad: %(default)r)`"
10481048
),
10491049
):
10501050
help_text = parser.format_help()

0 commit comments

Comments
 (0)