Skip to content

Commit 081910e

Browse files
authored
fix: escape percent signs in argparse help text (#76)
## Summary - Fix argparse formatting error when help text contains literal `%` characters (e.g., "100%") - Override `_get_help_string` in `CommandHelpFormatter` to escape `%` as `%%` while preserving valid `%(...)s` format specifiers Resolves the issue where `bl server alert get --help` would crash with: ``` TypeError: %i format: a real number is required, not dict ```
1 parent f7fc13c commit 081910e

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

src/binarylane/console/parser/help_formatter.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ def _split_lines(self, text: str, width: int) -> typing.List[str]:
2727

2828
return [text for text in text.splitlines() for text in textwrap.wrap(text, width)]
2929

30+
def _get_help_string(self, action: argparse.Action) -> str:
31+
"""Escape help text for argparse.
32+
33+
Argparse uses %-formatting for help strings, so literal % must be escaped as %%
34+
while %(...)s needs to be left as-is.
35+
"""
36+
return (super()._get_help_string(action) or "").replace("%", "%%").replace("%%(", "%(")
37+
3038
def add_usage(
3139
self,
3240
usage: Optional[str],

tests/integration/test_help.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,14 @@ def test_required_argument_help(app: App, capsys: CaptureFixture[str]) -> None:
3737
captured = capsys.readouterr()
3838
assert "\nArguments:\n" in captured.out
3939
assert "\nParameters:\n" in captured.out
40+
41+
42+
def test_help_with_percent_in_description(app: App, capsys: CaptureFixture[str]) -> None:
43+
# Help text containing "100%" should not cause argparse format string errors
44+
# The % character must be escaped as %% to prevent interpretation as format specifier
45+
with pytest.raises(SystemExit):
46+
app.run(["server", "alert", "get", "--help"])
47+
48+
captured = capsys.readouterr()
49+
assert "usage: bl server alert get" in captured.out
50+
assert "100%" in captured.out

0 commit comments

Comments
 (0)