From f792570d2c40af11298e4dae76c43a14e430cfd8 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Mon, 17 Nov 2025 13:52:44 +0100 Subject: [PATCH 1/5] explicit test on usage string --- .../test_tutorial/test_arguments/test_help/test_tutorial005.py | 2 +- .../test_arguments/test_help/test_tutorial005_an.py | 2 +- .../test_tutorial/test_arguments/test_help/test_tutorial006.py | 3 +-- .../test_arguments/test_help/test_tutorial006_an.py | 3 +-- 4 files changed, 4 insertions(+), 6 deletions(-) diff --git a/tests/test_tutorial/test_arguments/test_help/test_tutorial005.py b/tests/test_tutorial/test_arguments/test_help/test_tutorial005.py index 64c99a5271..8dbfb25d74 100644 --- a/tests/test_tutorial/test_arguments/test_help/test_tutorial005.py +++ b/tests/test_tutorial/test_arguments/test_help/test_tutorial005.py @@ -15,7 +15,7 @@ def test_help(): result = runner.invoke(app, ["--help"]) assert result.exit_code == 0 - assert "[OPTIONS] [NAME]" in result.output + assert "Usage: main [OPTIONS] [NAME]" in result.output assert "Arguments" in result.output assert "Who to greet" in result.output assert "[default: (Deadpoolio the amazing's name)]" in result.output diff --git a/tests/test_tutorial/test_arguments/test_help/test_tutorial005_an.py b/tests/test_tutorial/test_arguments/test_help/test_tutorial005_an.py index 28d8ee57b9..b828138c2c 100644 --- a/tests/test_tutorial/test_arguments/test_help/test_tutorial005_an.py +++ b/tests/test_tutorial/test_arguments/test_help/test_tutorial005_an.py @@ -15,7 +15,7 @@ def test_help(): result = runner.invoke(app, ["--help"]) assert result.exit_code == 0 - assert "[OPTIONS] [NAME]" in result.output + assert "Usage: main [OPTIONS] [NAME]" in result.output assert "Arguments" in result.output assert "Who to greet" in result.output assert "[default: (Deadpoolio the amazing's name)]" in result.output 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..ebcfda7956 100644 --- a/tests/test_tutorial/test_arguments/test_help/test_tutorial006.py +++ b/tests/test_tutorial/test_arguments/test_help/test_tutorial006.py @@ -15,9 +15,8 @@ def test_help(): result = runner.invoke(app, ["--help"]) assert result.exit_code == 0 - assert "[OPTIONS] ✨username✨" in result.output + assert "Usage: main [OPTIONS] [✨username✨]" in result.output assert "Arguments" in result.output - assert "✨username✨" 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..bcdb03e5b1 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 @@ -15,9 +15,8 @@ def test_help(): result = runner.invoke(app, ["--help"]) assert result.exit_code == 0 - assert "[OPTIONS] ✨username✨" in result.output + assert "Usage: main [OPTIONS] [✨username✨]" in result.output assert "Arguments" in result.output - assert "✨username✨" in result.output assert "[default: World]" in result.output From ba011cee743e8091b2d25712b81822d114731792 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Mon, 17 Nov 2025 13:53:03 +0100 Subject: [PATCH 2/5] update docs --- docs/tutorial/arguments/help.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/tutorial/arguments/help.md b/docs/tutorial/arguments/help.md index 2935a3e35f..a55f229adc 100644 --- a/docs/tutorial/arguments/help.md +++ b/docs/tutorial/arguments/help.md @@ -178,10 +178,10 @@ Now the generated help text will have `✨username✨` instead of `NAME`: ```console $ python main.py --help -Usage: main.py [OPTIONS] ✨username✨ +Usage: main.py [OPTIONS] [✨username✨] Arguments: - ✨username✨ [default: World] + [✨username✨] [default: World] Options: --help Show this message and exit. From 3a95c15ae4b5682b11ed9cdbb3c188333b1a6dd1 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Mon, 17 Nov 2025 13:57:03 +0100 Subject: [PATCH 3/5] ensure brackets around an optional argument, even when metavar is set --- typer/core.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/typer/core.py b/typer/core.py index e9631e56cf..6bdfaccbbf 100644 --- a/typer/core.py +++ b/typer/core.py @@ -382,7 +382,10 @@ def make_metavar(self, ctx: Union[click.Context, None] = None) -> str: # Modified version of click.core.Argument.make_metavar() # to include Argument name if self.metavar is not None: - return self.metavar + var = self.metavar + if not self.required and not var.startswith("["): + return f"[{var}]" + return var var = (self.name or "").upper() if not self.required: var = f"[{var}]" @@ -496,9 +499,11 @@ def _extract_default_help_str( return _extract_default_help_str(self, ctx=ctx) def make_metavar(self, ctx: Union[click.Context, None] = None) -> str: + print("make meta var L504") signature = inspect.signature(super().make_metavar) if "ctx" in signature.parameters: # Click >= 8.2 + print("RETURNING option metavar", super().make_metavar(ctx=ctx)) return super().make_metavar(ctx=ctx) # type: ignore[arg-type] # Click < 8.2 return super().make_metavar() # type: ignore[call-arg] From be57faf377546f5e1d531ee97ceb533fae252b0f Mon Sep 17 00:00:00 2001 From: svlandeg Date: Mon, 17 Nov 2025 14:10:08 +0100 Subject: [PATCH 4/5] cleanup --- typer/core.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/typer/core.py b/typer/core.py index 6bdfaccbbf..166ed2be50 100644 --- a/typer/core.py +++ b/typer/core.py @@ -499,11 +499,9 @@ def _extract_default_help_str( return _extract_default_help_str(self, ctx=ctx) def make_metavar(self, ctx: Union[click.Context, None] = None) -> str: - print("make meta var L504") signature = inspect.signature(super().make_metavar) if "ctx" in signature.parameters: # Click >= 8.2 - print("RETURNING option metavar", super().make_metavar(ctx=ctx)) return super().make_metavar(ctx=ctx) # type: ignore[arg-type] # Click < 8.2 return super().make_metavar() # type: ignore[call-arg] From b482a50c4b220ce0ed0d5b9292214733ee5f81b8 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Mon, 17 Nov 2025 14:16:28 +0100 Subject: [PATCH 5/5] small refactor --- typer/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typer/core.py b/typer/core.py index 166ed2be50..f7fb42360d 100644 --- a/typer/core.py +++ b/typer/core.py @@ -384,7 +384,7 @@ def make_metavar(self, ctx: Union[click.Context, None] = None) -> str: if self.metavar is not None: var = self.metavar if not self.required and not var.startswith("["): - return f"[{var}]" + var = f"[{var}]" return var var = (self.name or "").upper() if not self.required: