Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/binarylane/console/parser/help_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ def _split_lines(self, text: str, width: int) -> typing.List[str]:

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

def _get_help_string(self, action: argparse.Action) -> str:
"""Escape help text for argparse.

Argparse uses %-formatting for help strings, so literal % must be escaped as %%
while %(...)s needs to be left as-is.
"""
return (super()._get_help_string(action) or "").replace("%", "%%").replace("%%(", "%(")

def add_usage(
self,
usage: Optional[str],
Expand Down
11 changes: 11 additions & 0 deletions tests/integration/test_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,14 @@ def test_required_argument_help(app: App, capsys: CaptureFixture[str]) -> None:
captured = capsys.readouterr()
assert "\nArguments:\n" in captured.out
assert "\nParameters:\n" in captured.out


def test_help_with_percent_in_description(app: App, capsys: CaptureFixture[str]) -> None:
# Help text containing "100%" should not cause argparse format string errors
# The % character must be escaped as %% to prevent interpretation as format specifier
with pytest.raises(SystemExit):
app.run(["server", "alert", "get", "--help"])

captured = capsys.readouterr()
assert "usage: bl server alert get" in captured.out
assert "100%" in captured.out
Loading