diff --git a/tests/test_others.py b/tests/test_others.py index b1ca840fcb..d2cc8696f1 100644 --- a/tests/test_others.py +++ b/tests/test_others.py @@ -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() @@ -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( diff --git a/typer/core.py b/typer/core.py index bb8390d8ac..c7320c459b 100644 --- a/typer/core.py +++ b/typer/core.py @@ -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: