diff --git a/CHANGELOG.md b/CHANGELOG.md index 92ff8e7..745b6b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ ## 0.24 +### 0.24.1 + +- feat: Add Group.section to enable ordering of groups separately from the items within them. +- fix: invoke(help_formatter=...) not applying to explicitly decorated commands. + ### 0.24.0 - feat: Support native inference parser for dataclass-like annotation s. diff --git a/pyproject.toml b/pyproject.toml index 36c12f5..b7a8e58 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "cappa" -version = "0.24.0" +version = "0.24.1" description = "Declarative CLI argument parser." urls = {repository = "https://github.com/dancardin/cappa"} diff --git a/src/cappa/command.py b/src/cappa/command.py index 02f91c3..6521142 100644 --- a/src/cappa/command.py +++ b/src/cappa/command.py @@ -81,17 +81,23 @@ class Command(typing.Generic[T]): def get( cls, obj: type[T] | Command[T], help_formatter: HelpFormatable | None = None ) -> Command[T]: + help_formatter = help_formatter or HelpFormatter.default + + instance = None if isinstance(obj, cls): - return obj + instance = obj + else: + obj = class_inspect.get_command_capable_object(obj) + if getattr(obj, "__cappa__", None): + instance = obj.__cappa__ # type: ignore - obj = class_inspect.get_command_capable_object(obj) - if getattr(obj, "__cappa__", None): - return obj.__cappa__ # type: ignore + if instance: + return dataclasses.replace(instance, help_formatter=help_formatter) assert not isinstance(obj, Command) return cls( obj, - help_formatter=help_formatter or HelpFormatter.default, + help_formatter=help_formatter, ) def real_name(self) -> str: diff --git a/tests/arg/test_show_default.py b/tests/arg/test_show_default.py index 0f90e69..9c4034a 100644 --- a/tests/arg/test_show_default.py +++ b/tests/arg/test_show_default.py @@ -6,7 +6,7 @@ from typing_extensions import Annotated import cappa -from tests.utils import TestOutput, backends, parse +from tests.utils import CapsysOutput, backends, parse @backends @@ -20,7 +20,7 @@ class Command: with pytest.raises(cappa.HelpExit): parse(Command, "--help", backend=backend) - output = TestOutput.from_capsys(capsys) + output = CapsysOutput.from_capsys(capsys) assert "(Default: False)" in output.stdout @@ -35,7 +35,7 @@ class Command: with pytest.raises(cappa.HelpExit): parse(Command, "--help", backend=backend) - output = TestOutput.from_capsys(capsys) + output = CapsysOutput.from_capsys(capsys) assert "(Default: False)" not in output.stdout @@ -50,7 +50,7 @@ class Command: with pytest.raises(cappa.HelpExit): parse(Command, "--help", backend=backend) - stdout = TestOutput.from_capsys(capsys).stdout.replace(" ", "") + stdout = CapsysOutput.from_capsys(capsys).stdout.replace(" ", "") assert "[--foo](Default:True)" not in stdout assert "[--no-foo](Default:False)" in stdout @@ -66,6 +66,6 @@ class Command: with pytest.raises(cappa.HelpExit): parse(Command, "--help", backend=backend) - stdout = TestOutput.from_capsys(capsys).stdout.replace(" ", "") + stdout = CapsysOutput.from_capsys(capsys).stdout.replace(" ", "") assert "[--foo](Default:True)" in stdout assert "[--no-foo](Default:False)" not in stdout diff --git a/tests/help/test_help_formatter.py b/tests/help/test_help_formatter.py index d3304f1..3853322 100644 --- a/tests/help/test_help_formatter.py +++ b/tests/help/test_help_formatter.py @@ -8,7 +8,7 @@ from typing_extensions import Annotated import cappa -from tests.utils import parse, strip_trailing_whitespace +from tests.utils import CapsysOutput, parse, strip_trailing_whitespace @dataclass @@ -83,7 +83,10 @@ def test_override_help_format(capsys): "--help", completion=False, help_formatter=cappa.HelpFormatter.default.with_arg_format( - ("{default}", "{help}") + ( + "{default}", + "{help}", + ) ), ) @@ -175,7 +178,10 @@ def help_formatter(arg: cappa.Arg) -> str | None: "--help", completion=False, help_formatter=cappa.HelpFormatter().with_arg_format( - ("{help}", help_formatter) + ( + "{help}", + help_formatter, + ) ), ) @@ -193,3 +199,17 @@ def help_formatter(arg: cappa.Arg) -> str | None: [-h, --help] Show this message and exit. """ ) + + +def test_explicitly_wrapped_formatter(capsys): + @dataclass + class Args: + name: Annotated[str, cappa.Arg(help="Optional.")] = "arg" + + help_formatter = cappa.HelpFormatter(default_format="Default - {default}!") + with pytest.raises(cappa.HelpExit) as e: + parse(Args, "--help", help_formatter=help_formatter) + + assert e.value.code == 0 + output = CapsysOutput.from_capsys(capsys) + assert "Optional. Default - arg!\n" in output.stdout diff --git a/tests/utils.py b/tests/utils.py index 04fcf97..1ea0126 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -61,7 +61,7 @@ def strip_trailing_whitespace(text): @dataclass -class TestOutput: +class CapsysOutput: stdout: str stderr: str