Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 15 additions & 1 deletion tests/test_others.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ def main(
assert "[]" in result.output


def test_option_uses_envvar_when_required():
def test_option_envvar():
app = typer.Typer()

@app.command()
Expand All @@ -337,6 +337,20 @@ def main(user: Annotated[str, typer.Option(envvar="ME")]):
assert "Hello rick" in result.output


def test_option_envvar_list():
app = typer.Typer()

@app.command()
def main(users: Annotated[list[str], typer.Option(envvar="ME")]):
for u in users:
print(f"Hello {u}")

result = runner.invoke(app, env={"ME": "rick morty"})
assert result.exit_code == 0
assert "Hello rick" in result.output
assert "Hello morty" in result.output


def test_completion_argument():
file_path = Path(__file__).parent / "assets/completion_argument.py"
result = subprocess.run(
Expand Down
5 changes: 5 additions & 0 deletions typer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,11 +667,16 @@ def prompt_for_value(self, ctx: _click.Context) -> Any:
)

def value_from_envvar(self, ctx: _click.Context) -> Any:
# TODO: clean up
rv = self.resolve_envvar_value(ctx)

# Absent environment variable or an empty string is interpreted as unset.
if rv is None:
return None

if self.nargs != 1 or self.multiple:
return self.type.split_envvar_value(rv)

return rv

def resolve_envvar_value(self, ctx: _click.Context) -> str | None:
Expand Down
Loading