diff --git a/docs_src/arguments/help/tutorial006.py b/docs_src/arguments/help/tutorial006.py index e502a506f4..262d475c77 100644 --- a/docs_src/arguments/help/tutorial006.py +++ b/docs_src/arguments/help/tutorial006.py @@ -1,7 +1,7 @@ import typer -def main(name: str = typer.Argument("World", metavar="✨username✨")): +def main(name: str = typer.Argument("World", metavar="✨user✨")): print(f"Hello {name}") diff --git a/docs_src/arguments/help/tutorial006_an.py b/docs_src/arguments/help/tutorial006_an.py index 4d745278b4..ae44b8d48a 100644 --- a/docs_src/arguments/help/tutorial006_an.py +++ b/docs_src/arguments/help/tutorial006_an.py @@ -2,7 +2,7 @@ from typing_extensions import Annotated -def main(name: Annotated[str, typer.Argument(metavar="✨username✨")] = "World"): +def main(name: Annotated[str, typer.Argument(metavar="✨user✨")] = "World"): print(f"Hello {name}") diff --git a/tests/test_rich_utils.py b/tests/test_rich_utils.py index d31dbafb5c..484be8b112 100644 --- a/tests/test_rich_utils.py +++ b/tests/test_rich_utils.py @@ -73,7 +73,7 @@ def main( result = runner.invoke(app, ["--help"]) assert "Usage" in result.stdout - assert "name" in result.stdout + assert "NAME" in result.stdout assert "option-1" in result.stdout assert "option-2" in result.stdout assert result.stdout.count("[default: None]") == 0 @@ -99,3 +99,45 @@ def main(bar: str): result = runner.invoke(app, ["--help"]) assert "Usage" in result.stdout assert "BAR" in result.stdout + + +def test_rich_help_metavar(): + app = typer.Typer(rich_markup_mode="rich") + + @app.command() + def main( + *, + arg1: int, + arg2: int = 42, + arg3: int = typer.Argument(...), + arg4: int = typer.Argument(42), + arg5: int = typer.Option(...), + arg6: int = typer.Option(42), + arg7: int = typer.Argument(42, metavar="meta7"), + arg8: int = typer.Argument(metavar="ARG8"), + arg9: int = typer.Argument(metavar="arg9"), + ): + pass # pragma: no cover + + result = runner.invoke(app, ["--help"]) + # assert "Usage: main [OPTIONS] ARG1 ARG3 [ARG4] [meta7] ARG8 arg9" in result.stdout + assert "Usage: main [OPTIONS] ARG1 ARG3 [ARG4] meta7 ARG8 arg9" in result.stdout + + out_nospace = result.stdout.replace(" ", "") + + # arguments + assert "ARG1INTEGER" in out_nospace + assert "ARG3INTEGER" in out_nospace + assert "[ARG4]INTEGER" in out_nospace + assert "meta7INTEGER" in out_nospace + # assert "[meta7]INTEGER" in out_nospace + assert "ARG8INTEGER" in out_nospace + assert "arg9INTEGER" in out_nospace + + assert "arg7" not in result.stdout.lower() + assert "ARG9" not in result.stdout + + # options + assert "arg2INTEGER" in out_nospace + assert "arg5INTEGER" in out_nospace + assert "arg6INTEGER" in out_nospace diff --git a/tests/test_tutorial/test_arguments/test_help/test_tutorial006.py b/tests/test_tutorial/test_arguments/test_help/test_tutorial006.py index 4c8abb8a86..9c7fe4679d 100644 --- a/tests/test_tutorial/test_arguments/test_help/test_tutorial006.py +++ b/tests/test_tutorial/test_arguments/test_help/test_tutorial006.py @@ -8,16 +8,17 @@ runner = CliRunner() -app = typer.Typer() +app = typer.Typer(rich_markup_mode=None) app.command()(mod.main) def test_help(): result = runner.invoke(app, ["--help"]) assert result.exit_code == 0 - assert "[OPTIONS] ✨username✨" in result.output + assert "[OPTIONS] ✨user✨" in result.output assert "Arguments" in result.output - assert "✨username✨" in result.output + assert "✨user✨" in result.output + assert "name" not in result.output assert "[default: World]" in result.output diff --git a/tests/test_tutorial/test_arguments/test_help/test_tutorial006_an.py b/tests/test_tutorial/test_arguments/test_help/test_tutorial006_an.py index e60ec51666..fd8c9fe0c1 100644 --- a/tests/test_tutorial/test_arguments/test_help/test_tutorial006_an.py +++ b/tests/test_tutorial/test_arguments/test_help/test_tutorial006_an.py @@ -8,16 +8,17 @@ runner = CliRunner() -app = typer.Typer() +app = typer.Typer(rich_markup_mode=None) app.command()(mod.main) def test_help(): result = runner.invoke(app, ["--help"]) assert result.exit_code == 0 - assert "[OPTIONS] ✨username✨" in result.output + assert "[OPTIONS] ✨user✨" in result.output assert "Arguments" in result.output - assert "✨username✨" in result.output + assert "✨user✨" in result.output + assert "name" not in result.output assert "[default: World]" in result.output diff --git a/tests/test_tutorial/test_arguments/test_help/test_tutorial006_an_rich.py b/tests/test_tutorial/test_arguments/test_help/test_tutorial006_an_rich.py new file mode 100644 index 0000000000..fbfd99a627 --- /dev/null +++ b/tests/test_tutorial/test_arguments/test_help/test_tutorial006_an_rich.py @@ -0,0 +1,37 @@ +import subprocess +import sys + +import typer +from typer.testing import CliRunner + +from docs_src.arguments.help import tutorial006_an as mod + +runner = CliRunner() + +app = typer.Typer(rich_markup_mode="rich") +app.command()(mod.main) + + +def test_help(): + result = runner.invoke(app, ["--help"]) + assert result.exit_code == 0 + assert "[OPTIONS] ✨user✨" in result.output + assert "Arguments" in result.output + assert "✨user✨" in result.output + assert "name" not in result.output + assert "[default: World]" in result.output + + +def test_call_arg(): + result = runner.invoke(app, ["Camila"]) + assert result.exit_code == 0 + assert "Hello Camila" in result.output + + +def test_script(): + result = subprocess.run( + [sys.executable, "-m", "coverage", "run", mod.__file__, "--help"], + capture_output=True, + encoding="utf-8", + ) + assert "Usage" in result.stdout diff --git a/tests/test_tutorial/test_arguments/test_help/test_tutorial006_rich.py b/tests/test_tutorial/test_arguments/test_help/test_tutorial006_rich.py new file mode 100644 index 0000000000..fe1d29b281 --- /dev/null +++ b/tests/test_tutorial/test_arguments/test_help/test_tutorial006_rich.py @@ -0,0 +1,37 @@ +import subprocess +import sys + +import typer +from typer.testing import CliRunner + +from docs_src.arguments.help import tutorial006 as mod + +runner = CliRunner() + +app = typer.Typer(rich_markup_mode="rich") +app.command()(mod.main) + + +def test_help(): + result = runner.invoke(app, ["--help"]) + assert result.exit_code == 0 + assert "[OPTIONS] ✨user✨" in result.output + assert "Arguments" in result.output + assert "✨user✨" in result.output + assert "name" not in result.output + assert "[default: World]" in result.output + + +def test_call_arg(): + result = runner.invoke(app, ["Camila"]) + assert result.exit_code == 0 + assert "Hello Camila" in result.output + + +def test_script(): + result = subprocess.run( + [sys.executable, "-m", "coverage", "run", mod.__file__, "--help"], + capture_output=True, + encoding="utf-8", + ) + assert "Usage" in result.stdout diff --git a/tests/test_tutorial/test_commands/test_help/test_tutorial007.py b/tests/test_tutorial/test_commands/test_help/test_tutorial007.py index f262c251f5..d8c03bb7ea 100644 --- a/tests/test_tutorial/test_commands/test_help/test_tutorial007.py +++ b/tests/test_tutorial/test_commands/test_help/test_tutorial007.py @@ -24,10 +24,10 @@ def test_main_help(): def test_create_help(): result = runner.invoke(app, ["create", "--help"]) assert result.exit_code == 0 - assert "username" in result.output + assert "USERNAME" in result.output assert "The username to create" in result.output assert "Secondary Arguments" in result.output - assert "lastname" in result.output + assert "LASTNAME" in result.output assert "The last name of the new user" in result.output assert "--force" in result.output assert "--no-force" in result.output diff --git a/tests/test_tutorial/test_commands/test_help/test_tutorial007_an.py b/tests/test_tutorial/test_commands/test_help/test_tutorial007_an.py index 1a8c3d60a7..4c634c9856 100644 --- a/tests/test_tutorial/test_commands/test_help/test_tutorial007_an.py +++ b/tests/test_tutorial/test_commands/test_help/test_tutorial007_an.py @@ -24,10 +24,10 @@ def test_main_help(): def test_create_help(): result = runner.invoke(app, ["create", "--help"]) assert result.exit_code == 0 - assert "username" in result.output + assert "USERNAME" in result.output assert "The username to create" in result.output assert "Secondary Arguments" in result.output - assert "lastname" in result.output + assert "LASTNAME" in result.output assert "The last name of the new user" in result.output assert "--force" in result.output assert "--no-force" in result.output diff --git a/typer/rich_utils.py b/typer/rich_utils.py index d4c3676aea..4792c689a1 100644 --- a/typer/rich_utils.py +++ b/typer/rich_utils.py @@ -35,8 +35,8 @@ STYLE_SWITCH = "bold green" STYLE_NEGATIVE_OPTION = "bold magenta" STYLE_NEGATIVE_SWITCH = "bold red" -STYLE_METAVAR = "bold yellow" -STYLE_METAVAR_SEPARATOR = "dim" +STYLE_TYPES = "bold yellow" +STYLE_TYPES_SEPARATOR = "dim" STYLE_USAGE = "yellow" STYLE_USAGE_COMMAND = "bold" STYLE_DEPRECATED = "red" @@ -113,7 +113,7 @@ class OptionHighlighter(RegexHighlighter): highlights = [ r"(^|\W)(?P\-\w+)(?![a-zA-Z0-9])", r"(^|\W)(?P