Skip to content

Commit

Permalink
Merge pull request #174 from DanCardin/dc/command-override-help-forma…
Browse files Browse the repository at this point in the history
…tter

fix: invoke(help_formatter=...) not applying to explicitly decorated commands.
  • Loading branch information
DanCardin authored Nov 7, 2024
2 parents 1ba955b + 7405bef commit fd2fb92
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"}
Expand Down
16 changes: 11 additions & 5 deletions src/cappa/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions tests/arg/test_show_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


Expand All @@ -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


Expand All @@ -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

Expand All @@ -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
26 changes: 23 additions & 3 deletions tests/help/test_help_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}",
)
),
)

Expand Down Expand Up @@ -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,
)
),
)

Expand All @@ -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
2 changes: 1 addition & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def strip_trailing_whitespace(text):


@dataclass
class TestOutput:
class CapsysOutput:
stdout: str
stderr: str

Expand Down

0 comments on commit fd2fb92

Please sign in to comment.