ConfigArgParse

1.7.5 · active · verified Sun Apr 05

ConfigArgParse is a drop-in replacement for Python's standard `argparse` module, enhancing it with the ability to load configuration options from command-line arguments, environment variables, and configuration files (INI, YAML, TOML formats). It offers a unified API to define, document, and parse settings from multiple sources with a clear precedence order (command line > environment variables > config file values > defaults). The library is actively maintained, with its current version being 1.7.5.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define arguments and have them automatically loaded from a default config file and environment variables, with command-line arguments taking the highest precedence. It sets up a basic `ArgumentParser`, defines a config file and an environment variable, then parses the arguments to show the effective values based on precedence. It also shows how to add an argument to specify an alternative config file.

import configargparse
import os

# Simulate an environment variable
os.environ['MYAPP_HOST'] = 'localhost'

# Create a dummy config file
config_content = """
host = 127.0.0.1
port = 8080
debug = false
"""
with open('my_config.ini', 'w') as f:
    f.write(config_content)

# Initialize the parser
p = configargparse.ArgumentParser(
    default_config_files=['./my_config.ini'],
    auto_env_var_prefix='MYAPP_'
)

# Add arguments
p.add('--host', help='Host address')
p.add('--port', type=int, help='Port number')
p.add('--debug', action='store_true', help='Enable debug mode')
p.add('-c', '--config', is_config_file_arg=True, help='Path to config file')

# Parse arguments (simulate command line: --port 9000)
# `parse_args` can take a list of args, e.g., ['--port', '9000']
# For this example, we'll let it parse from system args (or defaults/env/config)
# If running as a script, try: python your_script.py --port 9000
# You can also set MYAPP_PORT=9001 in your shell before running.
# For the demo, let's explicitly provide some command-line args.
args = p.parse_args(['--port', '9002'])

print(f"Host: {args.host} (from config file, overridden by env var if present) ")
print(f"Port: {args.port} (command line > env var > config > default)")
print(f"Debug: {args.debug} (from config file)")

# Cleanup (optional)
os.remove('my_config.ini')
del os.environ['MYAPP_HOST'] # Clean up the simulated env var

# Expected precedence: command line > environment variables > config file values > defaults
# In this example:
# - Host: 'localhost' (from MYAPP_HOST env var, overrides 127.0.0.1 in config)
# - Port: 9002 (from explicit command line, overrides 8080 and any MYAPP_PORT)
# - Debug: False (from config file 'debug=false' which becomes --debug false; action='store_true' needs --debug to be present for True)
# Actually, `debug = false` in INI for `action='store_true'` will effectively NOT set the flag, resulting in False. If you want true, it should be `debug=true` or just `debug`. 
# Let's verify debug: `debug = false` in INI would mean the flag `--debug` is NOT present, so `args.debug` defaults to `False` for `action='store_true'`. If `debug=true` it would be `True`.

view raw JSON →