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

Ian Cordasco graffatcolmingov at gmail.com
Tue Feb 19 17:39:24 CET 2013


---------- Forwarded message ----------
From: Ian Cordasco <graffatcolmingov at gmail.com>
Date: Tue, Feb 19, 2013 at 11:35 AM
Subject: Re: [Python-ideas] argparse - add support for environment variables
To: Miki Tebeka <miki.tebeka at gmail.com>
Cc: python-ideas at googlegroups.com


Why not:

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

This way if SPAM isn't set, your default is 7. If spam is set, your
default becomes that.

On Tue, Feb 19, 2013 at 11:03 AM, Miki Tebeka <miki.tebeka at gmail.com> 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]
>
> parser = ArgumentParser()
>
>      parser.add_argument('--spam', env='SPAM', default=7)
>      args = parser.parse_args()
>      print(args.spam)
>
>      ./spam.py  -> 7
>      ./spam.py --spam=12 -> 12
>      SPAM=9 ./spam.py -> 9
>      SPAM=9 ./spam.py --spam=12 -> 12
>
> What do you think?
> --
> Miki

GMail decided to reply to python-ideas at googlegroups.com, but this
was my response which I shamefully did not bottom post.



More information about the Python-ideas mailing list