fastapi / typer

Typer, build great CLIs. Easy to code. Based on Python type hints.
https://typer.tiangolo.com/
MIT License
15.43k stars 656 forks source link

[FEATURE] Add support for allow_from_autoenv in CLI arguments #157

Open frederikaalund opened 4 years ago

frederikaalund commented 4 years ago

Currently, allow_from_autoenv is not supported for CLI arguments. I'd like to see that support added.

I guess that we also need to add auto_envvar_prefix to the Typer constructor for it to make sense.


Ideally, I'd like to have a global option (Typer constructor argument) that simply enables the autoenv functionality for all Typer.Option instances. I guess that is a separate feature request.

jonohill commented 3 years ago

I didn't read your request carefully enough at first, and assumed it wasn't possible to use Click's autoenv at all in Typer.

I'd also like to be able to enable autoenv for arguments, but for any other googlers like me, here's how you can use autoenv with options at least.

app = typer.Typer(add_completion=False)

@app.command(context_settings={ 'auto_envvar_prefix': 'TEST' })
def main(hello = typer.Option('world')):
    print(hello)

app()

Results in

Usage: test.py [OPTIONS]

Options:
  --hello TEXT  [env var: TEST_HELLO; default: world]
  --help        Show this message and exit.