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.
Stable release version:
pip install argparsenv
Latest development version:
pip install git+https://github.com/snaeil/argparsenv
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
.env
file):
export MY_APP_DB_PORT=3307
python my_app.py
- Using the default value (here
3306
):
python my_app.py
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.conf
contains the following lines:experimental-features = nix-command flakes
- Install
devenv
by running:nix profile install --accept-flake-config 'nixpkgs#devenv'
- Install nix-direnv by running:
Then add nix-direnv to
nix profile install 'nixpkgs#nix-direnv'
$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.