← LeaderboardThe Skill Leaderboard
rust-clap-config-precedence
Keep Rust Clap parser defaults from overriding file configuration. Use ArgMatches value sources to preserve explicit flags, false values, and documented setting precedence.
Try your first task
A source-grounded starter task has not been verified for this skill yet.
SKILL.md
name: rust-clap-config-precedencedescription: Keep Rust Clap parser defaults from overriding file configuration. Use ArgMatches value sources to preserve explicit flags, false values, and documented setting precedence.category: developmentrisk: criticalsource: selfsource_type: selfdate_added: "2026-12-02"author: ShianMiketags: [rust, clap, cli, configuration]Rust Clap Configuration Precedence
When to Use
- A Rust CLI reads a configuration file but parser defaults overwrite its settings.
- A false boolean or an explicit value equal to a default needs to override file configuration.
- A CLI change adds environment variables or flags and needs a consistent precedence rule.
Prerequisites
- The Clap command declaration, current application defaults, and file configuration type.
- The existing precedence for explicit flags, environment values, file values, and built-in defaults.
- A test input where the file supplies values that differ from parser defaults.
1. Inspect what the parser populated
- Parse an invocation with the relevant options omitted. Check
ArgMatches::value_sourcealongside the typed getters. - A parsed value or a positive
contains_idcan come from a Clap default. Inspect its source before treating it as an operator override. - Check synthetically supplied values that equal the default. Value comparison cannot identify whether the operator provided the argument.
- For derive-based commands, retain the raw matches via
CommandFactorywhen the merge needs provenance, and use the project's typed conversion for validation.
2. Resolve each option with its documented precedence
- Treat
ValueSource::CommandLineas an explicit flag when flags have the highest precedence. - If the command uses Clap's environment integration, handle
ValueSource::EnvVariableaccording to the documented order. Keep environment changes scoped to fixture subprocesses when testing. - Apply file settings before
ValueSource::DefaultValuewhen a built-in value is the fallback. PreserveSome(false)and the zero or empty values the option's type accepts. - Keep missing fields optional until the merge. Resolve defaults in one place so help text and effective configuration agree.
- Return parse errors for invalid explicit flags through the normal CLI path.
3. Test the effective configuration
- Assert that an omitted flag leaves a conflicting file value in effect and a supplied flag overrides it.
- Set a flag to its built-in default and verify it still overrides the file. Check an explicit false when the option supports it.
- Confirm omitted flags use the built-in default when the file has no corresponding value.
- Verify a partial override leaves other settings at their documented precedence.
Example
Run Keep file values unless overridden. It compares a getter-only merge with a source-aware merge and checks two settings, including an explicit false.
Limitations
- The fixture supplies a typed file configuration in memory and models flag, file, and built-in precedence.
- Environment integration, repeated arguments, and conditional defaults need additional coverage when the command uses them.
Safety and Side Effects
- Run the example in a scratch Cargo project. It parses synthetic arguments and writes local build artifacts.
- Test precedence changes with disposable file settings before applying them to operational commands.
Related Skills and Documentation
- Rust systems programming project introduces Clap commands and subcommands.
- Rust Pro
- Clap ArgMatches::value_source
- Clap ValueSource
- Clap argument defaults