From d5da406ef7f8684115ef69acd85f973bef7a11aa Mon Sep 17 00:00:00 2001 From: Brent Yi Date: Sun, 2 Jan 2022 02:58:36 -0800 Subject: [PATCH] Fix `%` in docstrings, version bump --- dcargs/_arguments.py | 3 +++ setup.py | 2 +- tests/test_docstrings.py | 7 +++++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/dcargs/_arguments.py b/dcargs/_arguments.py index db5900be..8ae91b73 100644 --- a/dcargs/_arguments.py +++ b/dcargs/_arguments.py @@ -357,6 +357,9 @@ def _generate_helptext(arg: ArgumentDefinition) -> _ArgumentTransformOutput: arg.parent_class, arg.field.name ) if docstring_help is not None: + # Note that the percent symbol needs some extra handling in argparse. + # https://stackoverflow.com/questions/21168120/python-argparse-errors-with-in-help-string + docstring_help = docstring_help.replace("%", "%%") help_parts.append(docstring_help) if arg.default is not None and hasattr(arg.default, "name"): diff --git a/setup.py b/setup.py index 826b6166..cffd2ca8 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setup( name="dcargs", - version="0.0.9", + version="0.0.10", description="Portable, reusable, strongly typed CLIs from dataclass definitions", long_description=long_description, long_description_content_type="text/markdown", diff --git a/tests/test_docstrings.py b/tests/test_docstrings.py index 3741ad21..9fac2831 100644 --- a/tests/test_docstrings.py +++ b/tests/test_docstrings.py @@ -80,16 +80,19 @@ class HelptextHardString: x: str = ( "This docstring may be tougher to parse!" ) - """Helptext.""" + """Helptext. 2% milk.""" # fmt: on + # Note that the percent symbol needs some extra handling in argparse. + # https://stackoverflow.com/questions/21168120/python-argparse-errors-with-in-help-string + f = io.StringIO() with pytest.raises(SystemExit): with contextlib.redirect_stdout(f): dcargs.parse(HelptextHardString, args=["--help"]) helptext = f.getvalue() assert ( - "--x STR Helptext. (default: This docstring may be tougher to parse!)\n" + "--x STR Helptext. 2% milk. (default: This docstring may be tougher to parse!)\n" in helptext )