Skip to content
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

Accept environment variables named by convention when envvar is not set #519

Open
7 tasks done
jruizaranguren opened this issue Dec 19, 2022 · 4 comments
Open
7 tasks done
Labels
feature New feature, enhancement or request

Comments

@jruizaranguren
Copy link

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the Typer documentation, with the integrated search.
  • I already searched in Google "How to X in Typer" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to Typer but to Click.

Commit to Help

  • 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

@jruizaranguren jruizaranguren added the feature New feature, enhancement or request label Dec 19, 2022
@raphaelauv
Copy link

could you try this -> #157 (comment)

@alexreg
Copy link

alexreg commented Dec 27, 2022

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.

@jruizaranguren
Copy link
Author

@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.

  • I already checked if it is not related to Typer but to Click, but I'm not so sure now 😅

@alexreg, thanks also for your code example. I prefer though to stick to maintained branches of libraries.

@alexreg
Copy link

alexreg commented Dec 27, 2022

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature, enhancement or request
Projects
None yet
Development

No branches or pull requests

3 participants