[Python-ideas] argparse - add support for environment variables

Terry Reedy tjreedy at udel.edu
Tue Mar 19 05:03:09 CET 2013

On 3/18/2013 7:02 PM, Miki Tebeka wrote:
> OK, so what's next in process? Got some +1 and some -1, how do we
> proceed? (or not).

Seems not.

> On Tuesday, February 19, 2013 8:03:16 AM UTC-8, Miki Tebeka wrote:
>     Greetings,
>     The usual way of resolving configuration is command line ->
>     environment -> default.
>     Currently argparse supports only command line -> default, I'd like
>     to suggest an optional "env" keyword to add_argument that will also
>     resolve from environment. (And also optional env dictionary to the
>     ArgumentParser __init__ method [or to parse_args], which will
>     default to os.environ).
>     Example:
>               [spam.py]

from os import environ as env

>         parser = ArgumentParser()
>           parser.add_argument('--spam', env='SPAM', default=7)

parser.add_argument('--spam', default = env.get('SPAM', 7))

This is about the same number of chars to type and I believe it is 
available now in all versions. I like it better because it puts the e.v. 
name and default right together.

Terry Jan Reedy

More information about the Python-ideas mailing list