Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix escaping in help text when rich is installed but not used #1089

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions tests/test_rich_markup_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def main(arg: str):
assert "Hello World" in result.stdout

result = runner.invoke(app, ["--help"])
assert "ARG [required]" in result.stdout
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fails with master, as pointed out in #1058 (it'll have a slash)

assert all(c not in result.stdout for c in rounded)


Expand Down
5 changes: 5 additions & 0 deletions typer/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@ def docs(
if not typer_obj:
typer.echo("No Typer app found", err=True)
raise typer.Abort()
if hasattr(typer_obj, "rich_markup_mode"):
if not hasattr(ctx, "obj") or ctx.obj is None:
ctx.ensure_object(dict)
if isinstance(ctx.obj, dict):
ctx.obj["TYPER_RICH_MARKUP_MODE"] = typer_obj.rich_markup_mode
click_obj = typer.main.get_command(typer_obj)
docs = get_docs_for_click(obj=click_obj, ctx=ctx, name=name, title=title)
clean_docs = f"{docs.strip()}\n"
Expand Down
15 changes: 13 additions & 2 deletions typer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,10 @@ def get_help_record(self, ctx: click.Context) -> Optional[Tuple[str, str]]:
if extra:
extra_str = "; ".join(extra)
extra_str = f"[{extra_str}]"
if rich is not None:
rich_markup_mode = None
if hasattr(ctx, "obj") and isinstance(ctx.obj, dict):
rich_markup_mode = ctx.obj.get("TYPER_RICH_MARKUP_MODE", None)
if rich is not None and rich_markup_mode == "rich":
# This is needed for when we want to export to HTML
extra_str = rich.markup.escape(extra_str).strip()

Expand Down Expand Up @@ -565,7 +568,11 @@ def _write_opts(opts: Sequence[str]) -> str:
if extra:
extra_str = "; ".join(extra)
extra_str = f"[{extra_str}]"
if rich is not None:

rich_markup_mode = None
if hasattr(ctx, "obj") and isinstance(ctx.obj, dict):
rich_markup_mode = ctx.obj.get("TYPER_RICH_MARKUP_MODE", None)
if rich is not None and rich_markup_mode == "rich":
# This is needed for when we want to export to HTML
extra_str = rich.markup.escape(extra_str).strip()

Expand Down Expand Up @@ -690,6 +697,10 @@ def main(

def format_help(self, ctx: click.Context, formatter: click.HelpFormatter) -> None:
if not rich or self.rich_markup_mode is None:
if not hasattr(ctx, "obj") or ctx.obj is None:
ctx.ensure_object(dict)
if isinstance(ctx.obj, dict):
ctx.obj["TYPER_RICH_MARKUP_MODE"] = self.rich_markup_mode
return super().format_help(ctx, formatter)
return rich_utils.rich_format_help(
obj=self,
Expand Down
Loading