Skip to main content

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:

  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.

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

argparsenv-1.0.6.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

argparsenv-1.0.6-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file argparsenv-1.0.6.tar.gz.

File metadata

  • Download URL: argparsenv-1.0.6.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.12.1 Linux/6.8.0-1021-azure

File hashes

Hashes for argparsenv-1.0.6.tar.gz
Algorithm Hash digest
SHA256 e5f1edf05491f9b8dc62820ec2c1120580e408cba98f0b7cb7a68deddea7419e
MD5 7d1099a36aa1e99ded9d8439372309bc
BLAKE2b-256 2fdb09ab845c6e89b2dfe13bba57191183aade0b6cc5e00bcbd97f7296e89335

See more details on using hashes here.

File details

Details for the file argparsenv-1.0.6-py3-none-any.whl.

File metadata

  • Download URL: argparsenv-1.0.6-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.12.1 Linux/6.8.0-1021-azure

File hashes

Hashes for argparsenv-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 fce5ba1a5b9afc535d901f252ebc834552cb284f1716f76ef1ae1f2d06c5a304
MD5 0094b3c7edf566915cd8fe36daa1ceb6
BLAKE2b-256 4cfa42e911113186f8f43b296af3b33bcf57948a5e9273a81bac57a21884e982

See more details on using hashes here.

Supported by

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