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

🐛 Ensure that options_metavar is passed through correctly #816

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions tests/test_others.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: no cover

@app.command(options_metavar="[OPTS]")
def c2():
pass # pragma: no cover

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
20 changes: 14 additions & 6 deletions typer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = Default(None),
add_help_option: bool = Default(True),
hidden: bool = Default(False),
deprecated: bool = Default(False),
Expand All @@ -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,
svlandeg marked this conversation as resolved.
Show resolved Hide resolved
add_help_option=add_help_option,
hidden=hidden,
deprecated=deprecated,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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] = Default(None),
add_help_option: bool = Default(True),
hidden: bool = Default(False),
deprecated: bool = Default(False),
Expand All @@ -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,
svlandeg marked this conversation as resolved.
Show resolved Hide resolved
add_help_option=add_help_option,
hidden=hidden,
deprecated=deprecated,
Expand Down Expand Up @@ -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(
Expand Down
Loading