Wrapper for docopt to allow ENV variables to override default arguments.
Example docopt pydoc:
"""
This is an example.
Usage:
python my_example.py [options]
Options:
-a --a-opt OPT # An example long option with magic default
-b OPT # An example short option with normal default [default: buzz]
--cee # An example long switch option
-d # An example short switch option
"""
- The value of
--a-opt
will be taken using command line, or from theENV
variable$A_OPT
. If neither is provided the value will beNone
- The value of
-b
will be taken from the command line, from theENV
variable$B
. If neither is provided thedefault
value"buzz"
will be used - The value of
--cee
will be taken from the command line, or from theENV
variable$CEE
. If neither is provided the flag will be set toFalse
- The value of
-d
will be taken from the command line, or from theENV
variable$D
. If neither is provided the flag will be set toFalse
Notice the -a-opt
option has been defined using a "magic" default. Because the default has been defined as the string describing the corresponding ENV
variable, the value will not only be taken from the ENV
at execution time, but will also print when the --help
flag is provided.
So, given the following ENV
configuration:
A_OPT='Hello, World!'
The help message of this script would be:
This is an example.
Usage:
python my_example.py [options]
Options:
-a --a-opt OPT # An example long option with magic default [default: Hello, World!]
-b OPT # An example short option with normal default [default: buzz]
--cee # An example long switch option
-d # An example short switch option
Specifying a prefix
value in the envopt()
call will prefix ENV
variables with this prefix right-padded with an underscore:
import os
from envopt import envopt
os.environ['MY_A_OPT'] = 'fizz'
os.environ['MY_B'] = '1'
os.environ['MY_CEE'] = 'buzz'
os.environ['MY_D'] = '1'
print envopt(__doc__, prefix='MY_')
{ '--a-opt' : 'fizz',
'-b' : 'buzz',
'--cee' : True,
'-d' : True }