Python Layered Config Precedence Resolver Prompt
Build a Python config resolver that merges defaults, config file, environment variables, and CLI flags into a typed dataclass with deterministic precedence
- Target user
- engineers who automate ops with Bash and Python
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a senior automation engineer who has debugged too many incidents caused by config that resolved differently than the operator expected. I will provide: - The settings my tool needs (name, type, default, whether secret) - The sources in play (built-in defaults, a config file, env vars, CLI flags) - The precedence order I want and any settings that are required Your job: 1. **Model the schema** — define a typed dataclass (or equivalent) capturing every setting, its type, default, and required/optional status. 2. **Implement the layered merge** — load each source into a partial mapping and merge them in an explicit, documented precedence order (e.g. CLI > env > file > default). 3. **Coerce and validate** — convert strings from env/CLI into the declared types, validate required fields, and fail with a clear message naming the offending key and source. 4. **Make precedence observable** — provide a way to print the effective config and, for each key, which source won. 5. **Handle secrets** — mark secret fields so they are redacted in any dump or log output. Output as: the dataclass and resolver in one module, a usage example, and a sample "effective config" dump showing the winning source per key. Default to explicit, debuggable precedence and loud validation; never silently fall back to a default when a provided value fails to parse.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Multi-Command Python CLI Tool Scaffold Prompt
Design a production-ready Python CLI tool with subcommands, global flags, config-file precedence, machine-readable output, and clean exit codes that an ops team can extend without rework.
-
Python argparse Parent Parsers and Shared-Flag Layering Prompt
Compose argparse parsers with parents=[...] to share common flags across subcommands and layer config-file, env, and flag precedence
-
Python Click CliRunner Test Suite Prompt
Design a pytest suite that exercises a Click-based CLI end to end using CliRunner, covering exit codes, stdout/stderr separation, prompts, isolated filesystems, and env-var precedence without touching real systems.
-
Bash getopts Long-Options Parser Prompt
Build a robust Bash option parser that supports both short flags and GNU-style long options with validation and a generated usage block
More Bash & Python Automation prompts & error guides
Browse every Bash & Python Automation prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.