Skip to main content

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:

  1. If the argument is provided on the command line, the environment variable is ignored.
  2. If the argument is not provided on the command line, the environment variable is used if it is set.
  3. If the argument is not provided on the command line and the environment variable is not set, the default value is used.
  4. 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:

  1. On the command line:
python my_app.py --port 3307
  1. As an environment variable (this can also be done by using a .env file):
export MY_APP_DB_PORT=3307
python my_app.py
  1. 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:

  1. Install nix package manager: bash <(curl -L https://nixos.org/nix/install) --no-daemon
  2. Make sure, your ~/.config/nix/nix.conf contains the following lines:
    experimental-features = nix-command flakes
    
  3. Install devenv by running:
    nix profile install --accept-flake-config 'nixpkgs#devenv'
    
  4. 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
    
  5. Hook direnv into your shell by adding a line to your shell's configuration file (e.g. ~/.bashrc), as described in the direnv documentation:
  6. You might have to open a new shell to make the changes take effect.

Metadata

Release files for argparsenv 1.0.11

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for argparsenv 1.0.11
File Size Uploaded
argparsenv-1.0.11.tar.gz 81.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for argparsenv 1.0.11
File Interpreter ABI Platform
argparsenv-1.0.11-py3-none-any.whl Python 3 none any Details

Total release size: 85.5 kB

Release files / argparsenv-1.0.11.tar.gz

Download URL argparsenv-1.0.11.tar.gz
Size 81.2 kB
Tags Source
SHA-256 checksum
How to use checksums
fe6d0e11fa198c0397ce1f7580010ccde1bacc1bfaa2f7ce07d42f86915e3793
BLAKE2b-256 checksum
How to use checksums
0124729ae7562fddfb008ef9f32839a6598594fe2f4958994132161270b8ff55
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.4.3 CPython/3.12.1 Linux/6.17.0-1022-azure

Release files / argparsenv-1.0.11-py3-none-any.whl

Download URL argparsenv-1.0.11-py3-none-any.whl
Size 4.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
125eae6187561a45741499b1fbb3f6602a04c0d1c22d48dfd6aab384d823f70f
BLAKE2b-256 checksum
How to use checksums
b4cecaa9274d367e461191dd08c96c120116f081a46a99a46c9873469b4a930b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.4.3 CPython/3.12.1 Linux/6.17.0-1022-azure

Release history Release notifications | RSS feed

This release

1.0.11 This release

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page