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.

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 poetry
poetry install
cd path/to/argparsenv
pre-commit install

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.1.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

argparsenv-1.0.1-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: argparsenv-1.0.1.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.1 Linux/6.5.0-1021-azure

File hashes

Hashes for argparsenv-1.0.1.tar.gz
Algorithm Hash digest
SHA256 d8ab61b4fb2fcf0ce7c294f83ecc03276adcec78a94e1fec29a3b4538b6ff377
MD5 7ac2ff8ef469184ab7139533eeaac868
BLAKE2b-256 a8fb3eb3043b6fab21e87ddff0cb5b3401c0318ef79f2d39331fb62d4dd03482

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for argparsenv-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 312c6405346e5d4572975ec7fe13c2ec846930228f327da628b5c639a529947b
MD5 e8e0d1bd42f5f55bfcd3ad47eb83fb8f
BLAKE2b-256 93546c9bfb7f38fb5aa78da91e0ed53f0705719edbd1a6c7b911954fd90db8a1

See more details on using hashes here.

Supported by

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