From 4126443210228ab9315539fe4e9c160667321b9d Mon Sep 17 00:00:00 2001 From: Garrett Smith Date: Tue, 30 Apr 2024 13:07:29 -0500 Subject: [PATCH 1/4] Fix use of options_metavar Typer was ignoring this setting when provided to commands. Refer to added test for details. --- tests/test_others.py | 18 ++++++++++++++++++ typer/main.py | 20 ++++++++++++++------ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/tests/test_others.py b/tests/test_others.py index 8c78520029..b828fbd36b 100644 --- a/tests/test_others.py +++ b/tests/test_others.py @@ -256,3 +256,21 @@ def test_split_opt(): prefix, opt = _split_opt("verbose") assert prefix == "" assert opt == "verbose" + + +def test_options_metadata_typer_default(): + app = typer.Typer(options_metavar="[options]") + + @app.command() + def c1(): + pass # pragma: nocover + + @app.command(options_metavar="[OPTS]") + def c2(): + pass # pragma: nocover + + result = runner.invoke(app, ["c1", "--help"]) + assert "Usage: root c1 [options]" in result.stdout + + result = runner.invoke(app, ["c2", "--help"]) + assert "Usage: root c2 [OPTS]" in result.stdout diff --git a/typer/main.py b/typer/main.py index 9db26975ca..c241b4936f 100644 --- a/typer/main.py +++ b/typer/main.py @@ -181,7 +181,7 @@ def callback( help: Optional[str] = Default(None), epilog: Optional[str] = Default(None), short_help: Optional[str] = Default(None), - options_metavar: str = Default("[OPTIONS]"), + options_metavar: Optional[str] = None, add_help_option: bool = Default(True), hidden: bool = Default(False), deprecated: bool = Default(False), @@ -202,7 +202,7 @@ def decorator(f: CommandFunctionType) -> CommandFunctionType: help=help, epilog=epilog, short_help=short_help, - options_metavar=options_metavar, + options_metavar=options_metavar or self.info.options_metavar, add_help_option=add_help_option, hidden=hidden, deprecated=deprecated, @@ -221,7 +221,7 @@ def command( help: Optional[str] = None, epilog: Optional[str] = None, short_help: Optional[str] = None, - options_metavar: str = "[OPTIONS]", + options_metavar: Optional[str] = None, add_help_option: bool = True, no_args_is_help: bool = False, hidden: bool = False, @@ -242,7 +242,9 @@ def decorator(f: CommandFunctionType) -> CommandFunctionType: help=help, epilog=epilog, short_help=short_help, - options_metavar=options_metavar, + options_metavar=( + options_metavar or self._info_val_str("options_metavar") + ), add_help_option=add_help_option, no_args_is_help=no_args_is_help, hidden=hidden, @@ -272,7 +274,7 @@ def add_typer( help: Optional[str] = Default(None), epilog: Optional[str] = Default(None), short_help: Optional[str] = Default(None), - options_metavar: str = Default("[OPTIONS]"), + options_metavar: Optional[str] = None, add_help_option: bool = Default(True), hidden: bool = Default(False), deprecated: bool = Default(False), @@ -294,7 +296,7 @@ def add_typer( help=help, epilog=epilog, short_help=short_help, - options_metavar=options_metavar, + options_metavar=options_metavar or self.info.options_metavar, add_help_option=add_help_option, hidden=hidden, deprecated=deprecated, @@ -325,6 +327,12 @@ def __call__(self, *args: Any, **kwargs: Any) -> Any: ) raise e + def _info_val_str(self, name: str) -> str: + val = getattr(self.info, name) + val_str = val.value if isinstance(val, DefaultPlaceholder) else val + assert isinstance(val_str, str) + return val_str + def get_group(typer_instance: Typer) -> TyperGroup: group = get_group_from_info( From 15202f11ccc9e3a5e13c758e8e78ac86cf434ca1 Mon Sep 17 00:00:00 2001 From: Garrett Smith Date: Tue, 30 Apr 2024 13:35:59 -0500 Subject: [PATCH 2/4] Fix coverage tests --- tests/test_others.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_others.py b/tests/test_others.py index b828fbd36b..d47dc3775f 100644 --- a/tests/test_others.py +++ b/tests/test_others.py @@ -263,11 +263,11 @@ def test_options_metadata_typer_default(): @app.command() def c1(): - pass # pragma: nocover + pass # pragma: no cover @app.command(options_metavar="[OPTS]") def c2(): - pass # pragma: nocover + pass # pragma: no cover result = runner.invoke(app, ["c1", "--help"]) assert "Usage: root c1 [options]" in result.stdout From f997ade77523dae2a61a3a7a42e370fa3d5f4bfa Mon Sep 17 00:00:00 2001 From: Garrett Smith Date: Thu, 2 May 2024 16:22:55 -0500 Subject: [PATCH 3/4] Fix typing for options_metavar --- typer/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/typer/main.py b/typer/main.py index c241b4936f..6884e77a45 100644 --- a/typer/main.py +++ b/typer/main.py @@ -181,7 +181,7 @@ def callback( help: Optional[str] = Default(None), epilog: Optional[str] = Default(None), short_help: Optional[str] = Default(None), - options_metavar: Optional[str] = None, + options_metavar: Optional[str] = Default(None), add_help_option: bool = Default(True), hidden: bool = Default(False), deprecated: bool = Default(False), @@ -274,7 +274,7 @@ def add_typer( help: Optional[str] = Default(None), epilog: Optional[str] = Default(None), short_help: Optional[str] = Default(None), - options_metavar: Optional[str] = None, + options_metavar: Optional[str] = Default(None), add_help_option: bool = Default(True), hidden: bool = Default(False), deprecated: bool = Default(False), From bc013c7c4ed64ffd151711e08a9053b9da54d398 Mon Sep 17 00:00:00 2001 From: Garrett Smith Date: Fri, 17 May 2024 14:52:45 -0500 Subject: [PATCH 4/4] Use _info_val_str for default options_metavar val --- typer/main.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/typer/main.py b/typer/main.py index 6884e77a45..4d5b2ac973 100644 --- a/typer/main.py +++ b/typer/main.py @@ -202,7 +202,9 @@ def decorator(f: CommandFunctionType) -> CommandFunctionType: help=help, epilog=epilog, short_help=short_help, - options_metavar=options_metavar or self.info.options_metavar, + options_metavar=( + options_metavar or self._info_val_str("options_metavar") + ), add_help_option=add_help_option, hidden=hidden, deprecated=deprecated, @@ -296,7 +298,9 @@ def add_typer( help=help, epilog=epilog, short_help=short_help, - options_metavar=options_metavar or self.info.options_metavar, + options_metavar=( + options_metavar or self._info_val_str("options_metavar") + ), add_help_option=add_help_option, hidden=hidden, deprecated=deprecated,