-
-
Notifications
You must be signed in to change notification settings - Fork 7
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
[Feature Request] Add auto_env_var_prefix #171
Comments
I think we could/should support something like this! Does typer do any auto-namespacing for this? Or are all parameters just directly derived, regardless of the command hierarchy? In your example you have What do we think about this proposed API:
Design decisions and behavior:
Misc open-ended questions:
EDIT: Sorry, just noticed your comment in your initial post:
So then to confirm, the following command:
Should actually have env var Something like
will result in |
Re: naming - I don't think we are bound to Typer's naming.
Re: edit - correct |
Can you elaborate on 2? I might be a bit confused. Regardless, I can begin working on this once I'm done with #165 |
So actually, this might play super nicely with #165 as a config. It would look something like:
|
I mean that if an |
oh gotcha! Yeah I think that makes sense. The I almost have #165 in a good state, I'll be going with the approach mentioned above. I should have something in a few days. |
Alright, great! Thank you for your work! |
Just an update on this: I haven't forgotten about it. I've been moving and that's been sucking up a lot of my time 😅 |
Completely understand, moving right now myself, can relate |
This feature is implemented in the newly released v2.7.0. See the example in the docs and the API docs for usage. |
@nordmtr fyi |
Hey @BrianPugh, thanks for implementing this! I have just a small question. I just took a look at the docs and realized the current implementation of this feature is mutually exclusive with the I wonder if it's necessarily should be the case? Shouldn't the user be able to use both at the same time? I mean defining some arguments in the |
you can! simply provide a list of callables to
Here the environment variables will get precedence since they are defined earlier in the |
Oh, thanks, didn't realize this was a thing. |
I just spent about 20 minutes trying to figure out why this wasn't working and I think this is the answer, but it's not very intuitive. What I was trying to do was something like this: app = App(config=cyclopts.config.Env("OTF_"))
OPT_TOKENS = Annotated[str, Parameter(show=False, allow_leading_hyphen=True)]
OPT_USERNAME = Annotated[str, Parameter(help="Username for the OTF API")]
OPT_PASSWORD = Annotated[str, Parameter(help="Password for the OTF API")]
OPT_LOG_LEVEL = Annotated[str, Parameter("CRITICAL", help="Log level", env_var="LOG_LEVEL")]
@app.meta.default
async def launcher(*tokens: OPT_TOKENS, username: OPT_USERNAME, password: OPT_PASSWORD):
api = await Api.create(username, password)
command, bound = app.parse_args(tokens)
return command(*bound.args, **bound.kwargs, api=api)
@app.command
def test(*,api:Annotated[Api,Parameter(parse=False)]):
print("test") But it seems multiple things go wrong here. This doesn't seem to work with I got this to work the way I wanted eventually by just setting the |
investigating; thanks for the report! |
Quickly testing the following python script: import cyclopts
from cyclopts import App
app = App(config=cyclopts.config.Env("OTF_"))
@app.default
def main(foo="python-default"):
print(foo)
if __name__ == "__main__":
app() $ python issue171.py
python-default
$ python issue171.py cli-value
cli-value
$ OTF_FOO=env-value python issue171.py
env-value So it certainly works with |
Opened #184 which should address the unintuitive behavior. |
Bit of unsolicited advice, but I'd recommend using docstrings to minimize the number of parameters you need to
|
First of all, this project looks great!
I would love to replace
Typer
withcyclopts
in my CLIs.However, there is an important
Click
feature currently lacking incyclopts
:auto_env_var_prefix
(calledauto_envvar_prefix
inClick
).Feature request
Allow setting
auto_env_var_prefix
incyclopts.App
(orapp.command
) which would generate aenv_var
for every parameter.For example, this CLI command
could read the
my_arg
environment variable fromMY_APP_MY_ARG
.This is extremely useful when building CLIs for deployment in various remote environments.
Nested commands should insert their names in the generated env variable.
The text was updated successfully, but these errors were encountered: