Accept environment variables named by convention when envvar is not set
First Check
- [X] I added a very descriptive title to this issue.
- [X] I used the GitHub search to find a similar issue and didn't find it.
- [X] I searched the Typer documentation, with the integrated search.
- [X] I already searched in Google "How to X in Typer" and didn't find any information.
- [X] I already read and followed all the tutorial in the docs and didn't find an answer.
- [X] I already checked if it is not related to Typer but to Click.
Commit to Help
- [X] I commit to help with one of those options 👆
Example Code
import typer
cli = typer.Typer()
@cli.command()
def configure(file: str = typer.Argument("", envvar="FILE")):
print("Configure could be written more succintly if envvar=FILE is created by convention")
Description
We usually define arguments and options as environment variables. We tipically use a simple convention where param becomes PARAM and some_param becomes SOME_PARAM. That adds a bit of boilerplate to method definitions. I would like typer to accept environment variables that match argument and options names by conventions.
I can try to make a pull request if you find this behavior reasonable.
Wanted Solution
I would like that if envvar is not fullfilled in typer, it tries to get that environment variable by convention.
I could try to make a pull request with this idea if you find it reasonable.
Wanted Code
import typer
cli = typer.Typer()
@cli.command()
def configure(file: str)):
print("Configure accepts FILE as envvar")
Alternatives
No response
Operating System
Linux
Operating System Details
No response
Typer Version
0.7.0
Python Version
Python 3.8.10
Additional Context
No response
could you try this -> https://github.com/tiangolo/typer/issues/157#issuecomment-827299267
In case you're interested, I added support for this in my fork (by setting auto_envvar_prefix="", as well as a solution for issue #157.
@raphaelauv, thanks for the idea. I tried the approach, and still see two mean shortcomings:
- it only work for options
- it must be set per-command. I tried
typer.Typer(context_settings={ 'auto_envvar_prefix': 'TEST' })with no success.
Hence, no much boilerplate is avoided.
It might make sense to push the addition of auto_envar_prefix as a global setting for click. Then, typer would only have to expose that setting in Typer constructor.
- [x] I already checked if it is not related to Typer but to Click, but I'm not so sure now :sweat_smile:
@alexreg, thanks also for your code example. I prefer though to stick to maintained branches of libraries.
@alexreg No problem. I mean, mine is actively maintained, just not the official/original version, of course. I forked it when there had not been any activity here for a very long time. These days it's diverged a bit, though – the main point of its existence is to support Cloup as the foundation rather than merely Click. What you can perhaps do, if it suits you, is take my commit for this feature and try to merge it into the main Click branch, since that part of the codebase hadn't diverged anyway, as far as I recall.
I think this would be solved by introducing the feature described in #157 (specifying auto_envvar_prefix in the Typer constructor and supporting it for CLI arguments in addition to options).
I think we can close this as duplicate