Skip to main content

Wrap docopt to allow ENV variables as argument defaults

Project description

# envopt
Wrapper for [docopt](https://github.com/docopt/docopt) to allow ENV variables to override default arguments.


## Usage

Example docopt pydoc:

```python
__doc__ = \
""" This is an example.

Usage:
python my_example.py [options]

Options:
-a --a-opt OPT # An example long option [default: foo]
-b OPT # An example short option [default: bar]
--cee # An example long switch option
-d # An example short switch option """
```

Parsing this example without any `ENV` variables set would yield:

```python
from envopt import envopt

print envopt(__doc__)

{ '--a-opt' : 'foo',
'-b' : 'bar',
'--cee' : False,
'-d' : False }
```

However, setting `ENV` variables `A_OPT`, `B`, `CEE`, or `D` will override the given defaults:

```python
import os
from envopt import envopt

os.environ['A_OPT'] = 'fizz'
os.environ['B'] = '1'
os.environ['CEE'] = 'buzz'
os.environ['D'] = '1'

print envopt(__doc__)

{ '--a-opt' : 'fizz',
'-b' : 'buzz',
'--cee' : True,
'-d' : True }
```

For switch-style arguments, the existence of the `ENV` variable is enough to set the value to `True`. Unset the variable to return the default value to `False`.

Specifying a `env_prefix` value in the `envopt()` call will prefix `ENV` variables with this prefix right-padded with an underscore:

```python
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__, env_prefix='MY')

{ '--a-opt' : 'fizz',
'-b' : 'buzz',
'--cee' : True,
'-d' : True }
```

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

envopt-0.0.1.tar.gz (3.2 kB view details)

Uploaded Source

File details

Details for the file envopt-0.0.1.tar.gz.

File metadata

  • Download URL: envopt-0.0.1.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for envopt-0.0.1.tar.gz
Algorithm Hash digest
SHA256 77febf0dfd9f85acf2c620811b1b257658943ab95fb926a7f9b6d315faa5bdc8
MD5 ecb06e7a191fb4a9d88a4521babf73dd
BLAKE2b-256 88e002178647ab4c6d77934e6d2df993d3d7522fab39ce389f1fbbf56b876704

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page