Skip to main content

yaml-arg-configer

A Python library for combining YAML configuration files with command-line arguments. It provides a flexible configuration management system with support for multiple config files, context variables, module imports, and function calls.


Table of Contents


Installation

# Install from PyPI (when available)
pip install yaml-arg-configer

# Install from source (editable)
pip install -e .

Overview

The YamlArgParser class bridges YAML configuration files and command-line arguments. It:

  1. Loads default values from a YAML file
  2. Merges with user-provided configurations
  3. Selects which config files to load via the command line
  4. Supports dynamic value resolution via custom YAML tags

Scope (by design):

  • Flat key=value configs: each YAML key is one parameter. No deep merge, no dot-keys — nested values are used as-is, not overridden per sub-key.
  • CLI selects files, YAML sets values: config keys appear in --help with their help text, but values always come from config files, never the command line.
  • Hydra-lite: PyYAML-only, deliberately not growing into Hydra-style nested configs / config groups.

Features

Feature Description
Default Configuration Load base settings from YAML files with default and help fields
Command-Line Selection Pick which config files to load (-c, -d, --dc, -ctx)
Multiple Configurations Combine multiple YAML config files in priority order
Context Resolution Reference context variables using !ctx tag
Module Imports Import attributes from Python modules using !ipt tag
Function Calls Execute Python functions using !call tag
Path Operations Use !join and !chain2path for path manipulation
Config Saving Export current configuration back to YAML

Quick Start

Basic Example

from yaml_arg_configer import YamlArgParser

# Initialize parser with default YAML file
yaml_parser = YamlArgParser(default_yaml='defaults.yaml')

# Parse command-line arguments
cmd_args = yaml_parser.parse_cmd_args()

# Parse configurations from YAML files
final_args = yaml_parser.parse_args(cmd_args)

# Access results
print(yaml_parser.get_args())           # argparse.Namespace
print(yaml_parser.args)                 # dict
print(yaml_parser['arg_name'])           # dict-like access
print(yaml_parser.get('arg_name'))       # dict-like access with default value, return None by default if arg not exist
yaml_parser.save_config('output.yaml')   # Save to YAML

Programmatic Usage

from yaml_arg_configer import YamlArgParser

# Initialize parser
yaml_parser = YamlArgParser(default_yaml='defaults.yaml')

# Parse defaults
yaml_parser.parse_default('defaults.yaml')

# Update from additional YAML files
yaml_parser.update_from_yaml('config1.yaml', strict=True)
yaml_parser.update_from_yaml('config2.yaml', strict=True)

# Get final arguments as dict
final_args = yaml_parser.args

# Or as argparse.Namespace
final_args = yaml_parser.get_args()

Saving Configuration

# Save current configuration to YAML
yaml_parser.save_config('output_config.yaml')

Parsing Configurations from Directory

The parse_cfgs() method is convenient for loading configurations from a directory:

# Initialize parser
yaml_parser = YamlArgParser(default_yaml='defaults.yaml')

# Parse configurations from files in a directory
# Loads: config1.yaml, config2.yaml, etc.
final_args = yaml_parser.parse_cfgs(
    cfgs=['config1', 'config2'],  # Config file names (without .yaml)
    cfg_dir='configs',             # Directory containing config files
    strict=True                    # Enforce validation
)

# Access results
print(final_args)  # Returns dict with parsed arguments

Key Features:

  • Automatically constructs file paths: {cfg_dir}/{name}.yaml
  • Loads context from ctx.yaml if specified
  • Parses defaults before applying config files
  • Returns merged configuration as dictionary

Configuration Structure

Default Configuration (defaults.yaml)

arg1:
  default: ~                    # ~ means None
  help: "Description for arg1"
arg2:
  default: 42
  help: "Number of iterations"
arg3: True                      # Simple format: value directly
arg4: "string value"            # Simple format: string value

User Configuration (config1.yaml)

arg1: value1                    # Override default value
arg2: 100                       # Override another value

Context Management

Context YAML files support dynamic context management:

# ctx.yaml
import: !ipt
  math: [sin, cos]

The !ipt tag can import multiple modules and attributes, which are then available for !ctx references.


Custom YAML Tags

The library extends PyYAML with custom tags for dynamic value resolution: !ctx (context variables), !ipt (import module attributes), !call (function calls), !join (flatten lists), !chain2path (join paths).

Full reference with examples: docs/yaml-tags.md


Command-Line Options

Flag Description Example
-d, --cfg_dir Directory containing YAML config files -d configs
-c, --cfg Names of configuration files to load -c config1 config2
--dc Path to default YAML file (dest default_yaml) --dc defaults.yaml
-ctx Path to context YAML file (dest ctx_yaml) -ctx ctx.yaml
--strict Enforce user config keys exist in defaults --strict
--help Help for the built-in flags and all config keys (help text from the default YAML's help: fields) --help

Config keys are registered as help-only --<key> options: they show up in --help, but passing a value on the command line errors out — values come from config files. Pass register_help_keys=False to the constructor to disable this registration.

Example Commands

# Load defaults and config files
python examples/my_script.py --dc defaults.yaml -d configs -c config1 config2

# With context resolution
python examples/my_script.py --dc defaults.yaml -ctx ctx.yaml -d configs -c config1

# Show help (built-in flags + all config keys)
python examples/my_script.py --dc defaults.yaml --help

# Strict mode (enforce config validation)
python examples/my_script.py --dc defaults.yaml --strict -d configs -c config1

Documentation

  • API Reference — full Python API for YamlArgParser and the YAML loader
  • YAML Tags — custom tag reference with examples

Troubleshooting

Best Practices

  1. Start with defaults: Define comprehensive defaults in defaults.yaml
  2. Use context files: Centralize reusable values in ctx.yaml
  3. Document with help: Always provide help text for arguments
  4. Validate with strict: Use --strict to catch configuration errors
  5. Chain configs: Load multiple configs for modular configuration

Common Issues

Missing keys in strict mode

  • Symptom: AssertionError: Argument 'X' not defined in the default configuration.
  • Solution: Ensure all user config keys appear in defaults.yaml with proper structure (even if using minimal entries)

Context variables not resolving

  • Symptom: KeyError: xxx when loading the config
  • Check that ctx.yaml exists and is loaded via -ctx flag
  • Verify import paths are correct in context yaml (import: !ipt)
  • Variable names match between import and reference

Config merging order wrong

  • Symptom: Config file values overwritten by defaults instead of overriding them
  • Explanation: Priority order is: later-loaded configs > earlier-loaded configs > defaults. The command line only selects which files load; it does not set values.

License

MIT

Metadata

Release files for yaml-arg-configer 0.2.1

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

Source distribution (sdist)

Source distribution for yaml-arg-configer 0.2.1
File Size Uploaded Provenance Actions
yaml_arg_configer-0.2.1.tar.gz 33.9 kB Without provenance Details for yaml_arg_configer-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for yaml-arg-configer 0.2.1
File Interpreter ABI Platform Actions
yaml_arg_configer-0.2.1-py3-none-any.whl Python 3 none any Details for yaml_arg_configer-0.2.1-py3-none-any.whl

Total release size: 62.2 kB

Release files / yaml_arg_configer-0.2.1.tar.gz

Download URL yaml_arg_configer-0.2.1.tar.gz
Size 33.9 kB
Tags Source
SHA-256 checksum
How to use checksums
43e15545fdbe5564ee939b6aec26d14b40c585e078afaac6f4e7b375d6f5e101
BLAKE2b-256 checksum
How to use checksums
3117cb4b45a8cf24b7671e5c6537acaf0413939ee8cd291f0643a5f73d754a89
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release files / yaml_arg_configer-0.2.1-py3-none-any.whl

Download URL yaml_arg_configer-0.2.1-py3-none-any.whl
Size 28.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b912e7dfedd8bea47f4ff7d922c423cb7070490e8d183452cb80b245e7da0d22
BLAKE2b-256 checksum
How to use checksums
7520f1f5d49154369f7f254b865c0613608ee0087f3af2eb397151b19e9eca37
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Security for yaml-arg-configer 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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