Skip to main content

Load environment vars, and command line arguments in a predictable, standardized way

Project description

cfgur (configure)

Load environment vars, and command line arguments in a predictable, standardized way.

This package wraps argparse, loading config from environment variables as well as the usual command line arguments / flags. A config object is passed into init(), configuring argparse. The environment variables are implied from the arg name (stripped, uppercased and underscored), but can be manually defined using the key "env_var" in args config. Once args are loaded, config is accessable using a getter function "get()". To get all variables, use get_all(). The args object keys are passed to parser.add_argument().

Installation

The cfgur package is available on PyPI which means installation should be as simple as:

$ pip install cfgur

Usage

import cfgur

# Initialize once, pass in config
cfgur.init({
    "parser": {
        "description": "this is the service description"
    },
    "args": [
        {
            "name": "-test",
            "help": "Description of test",
            "default": "test-db",
            "required": False,
        }, {
            "name": "--foo",
            "required": True,
        }, {
            "name": "-f",
            "required": False,
            "env_var": "LETTER_F",
        }
    ]
})

# Return value of config. If missing, return None
my_config = cfgur.get_all()

# Return value of config. If missing, return None
test_val = cfgur.get("test")

# Return value of config. If missing, raise KeyError
test_val = cfgur.get("test", True)

License

This software is licensed under the MIT license.

© 2018 Bray Almini.

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

cfgur-0.1.3.tar.gz (2.8 kB view hashes)

Uploaded Source

Supported by

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