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.
import argparsenv
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
.envfile):
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.
The development environment can be set up using nix and devenv:
- Install nix package manager:
bash <(curl -L https://nixos.org/nix/install) --no-daemon - Make sure, your
~/.config/nix/nix.confcontains the following lines:experimental-features = nix-command flakes
- Install
devenvby running:nix profile install --accept-flake-config 'nixpkgs#devenv'
- Install nix-direnv by running:
nix profile install 'nixpkgs#nix-direnv'
Then add nix-direnv to$HOME/.config/direnv/direnvrc:source $HOME/.nix-profile/share/nix-direnv/direnvrc
- Hook direnv into your shell by adding a line to your shell's configuration file (e.g.
~/.bashrc), as described in the direnv documentation: - You might have to open a new shell to make the changes take effect.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file argparsenv-1.0.8.tar.gz.
File metadata
- Download URL: argparsenv-1.0.8.tar.gz
- Upload date:
- Size: 3.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.3.2 CPython/3.12.1 Linux/6.14.0-1017-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f66d0d47f3df3a7232c1297d5b045b561272f9ae51320e262e065d61a99b583a
|
|
| MD5 |
db8dacf6783fdeed1781272e1ec369e5
|
|
| BLAKE2b-256 |
c080421e2833b581634588c8c13c2c6bf41ba603c0d19e878ace2e26e06c8655
|
File details
Details for the file argparsenv-1.0.8-py3-none-any.whl.
File metadata
- Download URL: argparsenv-1.0.8-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.3.2 CPython/3.12.1 Linux/6.14.0-1017-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a0c78a1f7fb3c0c79dd2a0832de0110fe1af5a3653d2f5e104b33783d0204de1
|
|
| MD5 |
03be02d7acb04b3dc13f0fb7f5d3a2c0
|
|
| BLAKE2b-256 |
6550b69ba8f2165cee93e289d53d2edd6976008bea932c4c97389af05eca55d1
|