Extends python's argparser with environment variables
Project description
argparsenv
This project extends python's argparse module to allow for environment variable overrides of command line arguments.
The rule for environment variable overrides is as follows:
- If the argument is provided on the command line, the environment variable is ignored.
- If the argument is not provided on the command line, the environment variable is used if it is set.
- If the argument is not provided on the command line and the environment variable is not set, the default value is used.
- If the argument is environment variable is set both on the command line and in the environment, the command line value is used.
Installation
Stable release version:
pip install argparsenv
Latest development version:
pip install git+https://github.com/snaeil/argparsenv
Usage
This module builds on top of python's standard library argparse.
env_arg_parser = ArgumentParser()
env_arg_parser.add_argument(
"--port",
dest="port",
env_var="MY_APP_DB_PORT",
default="3306",
)
With this ArgumentParser instance, the port argument can be set in three ways:
- On the command line:
python my_app.py --port 3307
- As an environment variable (this can also be done by using a
.env
file):
export MY_APP_DB_PORT=3307
python my_app.py
- Using the default value (here
3306
):
python my_app.py
Contributing
Contributions are welcome! For feature requests, bug reports or questions, please open an issue. For code contributions, please open a pull request.
Please make sure to install pre-commit
before making changes to the code (this enables
checks for code formatting, linting, etc. before committing changes):
pip install pre-commit
cd path/to/argparsenv
pre-commit install
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file argparsenv-1.0.0.tar.gz
.
File metadata
- Download URL: argparsenv-1.0.0.tar.gz
- Upload date:
- Size: 3.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.1 Linux/6.5.0-1018-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a1918c6b72a0ba0c1d006e9bdcc4b41c580e39c4bd60497c4206468f2d4ebfa6 |
|
MD5 | 23f5a783cf949859e70779c36d1c341a |
|
BLAKE2b-256 | 2cdaf3bd1126dc80690150d485fa0e7432e10c4c01e537760a590ef50c53b7a8 |
File details
Details for the file argparsenv-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: argparsenv-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.1 Linux/6.5.0-1018-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc132322d7ab9fd264369c3d2cc10d8b12a59f016236a38ce4fff78b8fbfdca8 |
|
MD5 | 1603b7b48ebb16e1f225a0c50b922b87 |
|
BLAKE2b-256 | 80d53ab85fe61a366e4b60bbed6e15849e215afc746d46514f78bf17607324d7 |