Skip to content

Commit

Permalink
fix/exception-in-console
Browse files Browse the repository at this point in the history
  • Loading branch information
sankarngrjn committed Nov 24, 2024
1 parent 6c31320 commit af7b401
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/rich_click/rich_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ def main(
if not standalone_mode:
raise
if ctx is not None:
config = ctx.help_config
formatter = ctx.formatter
else:
config = self._generate_rich_help_config()
formatter = self.context_class.formatter_class(config=config, file=sys.stderr)
formatter = self.context_class.formatter_class(config=config, file=sys.stderr)
from rich_click.rich_help_rendering import rich_format_error

rich_format_error(e, formatter)
Expand All @@ -192,10 +192,10 @@ def main(
raise
try:
if ctx is not None:
config = ctx.help_config
formatter = ctx.formatter
else:
config = self._generate_rich_help_config()
formatter = self.context_class.formatter_class(config=config)
formatter = self.context_class.formatter_class(config=config)
except Exception:
click.echo("Aborted!", file=sys.stderr)
else:
Expand Down
2 changes: 2 additions & 0 deletions src/rich_click/rich_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ def __init__(
else:
self.help_config = rich_help_config

self.formatter = self.make_formatter()

def make_formatter(self) -> RichHelpFormatter:
"""Create the Rich Help Formatter."""
formatter = self.formatter_class(
Expand Down
8 changes: 4 additions & 4 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def test_global_config_equal_config_defaults() -> None:

def test_config_from_globals_behavior() -> None:
original_style_option_value = rc.STYLE_OPTION
rc.STYLE_OPTION = "new-value"
rc.STYLE_OPTION = "bold cyan"

@command()
def cli1() -> None:
Expand All @@ -80,7 +80,7 @@ def cli1() -> None:
with cli1.make_context("pytest-example", []) as ctx:
if TYPE_CHECKING:
assert isinstance(ctx, RichContext)
assert ctx.help_config.style_option == "new-value"
assert ctx.help_config.style_option == "bold cyan"

@command()
@rich_config(help_config=RichHelpConfiguration())
Expand All @@ -100,7 +100,7 @@ def cli3() -> None:
with cli3.make_context("pytest-example", []) as ctx:
if TYPE_CHECKING:
assert isinstance(ctx, RichContext)
assert ctx.help_config.style_option == "new-value"
assert ctx.help_config.style_option == "bold cyan"

@command()
@rich_config(help_config={})
Expand All @@ -110,7 +110,7 @@ def cli4() -> None:
with cli4.make_context("pytest-example", []) as ctx:
if TYPE_CHECKING:
assert isinstance(ctx, RichContext)
assert ctx.help_config.style_option == "new-value"
assert ctx.help_config.style_option == "bold cyan"


def test_config_is_serializable_and_invertible() -> None:
Expand Down

0 comments on commit af7b401

Please sign in to comment.