CLI Config File
Load CLI config from a file with layered overrides for flags and env, validate it with a schema, and fail closed on an invalid config.
On this page
Introduction
This guide covers CLI config file with layered overrides, a recurring concern in the cli-tooling scenario. A CLI with many options is easier to drive from a config file than from flags, but the flags and env should still override it for one-off runs. Layering file, env, and flags with explicit precedence, then validating the merge, gives a predictable config that fails closed on a bad value.
Load the file
Read the config from a default path or a –config flag, and tolerate a missing file by falling back to defaults, so the CLI works with no config.
The trade-off worth naming is that read the config from a default path or a –config flag, and tolerate a missing file by falling back to defaults, so the cli works with no config adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
import { cosmiconfig } from 'cosmiconfig';
const found = await cosmiconfig('mycli').search();
const file = found?.config ?? {};
Layer overrides
Merge defaults, file, env, and flags in that order so the most specific wins, which is the precedence users expect from a CLI.
The trade-off worth naming is that merge defaults, file, env, and flags in that order so the most specific wins, which is the precedence users expect from a cli adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
const cfg = { ...defaults, ...file, ...envOverrides, ...flags };
Validate the merge
Run the merged config through a schema so an invalid value fails before the CLI starts work, with an error that names the field.
The trade-off worth naming is that run the merged config through a schema so an invalid value fails before the cli starts work, with an error that names the field adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Fail closed
On an invalid config, exit non-zero with the field and the reason, so the CLI never runs with a half-valid config that produces surprising output.
The trade-off worth naming is that on an invalid config, exit non-zero with the field and the reason, so the cli never runs with a half-valid config that produces surprising output adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Locate the file
Search up the directory tree for the config so a user can run the CLI from a subdirectory and still pick up the project config, which is the cosmiconfig behavior.
The trade-off worth naming is that search up the directory tree for the config so a user can run the cli from a subdirectory and still pick up the project config, which is the cosmiconfig behavior adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Test the layers
Assert a flag overrides env overrides file overrides defaults, and an invalid config fails closed, so the precedence and validation hold.
The trade-off worth naming is that assert a flag overrides env overrides file overrides defaults, and an invalid config fails closed, so the precedence and validation hold adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Sources
Sources: cosmiconfig, Zod.
FAQ
- What precedence should flags, env, and file have?
- Flags highest, then env, then file, then defaults, so a one-off flag always wins and an env override beats the file, which is the least surprising order.
- JSON, YAML, or TOML for the config?
- Pick one and stick to it; TOML is common for CLIs because it handles nested tables cleanly, but JSON needs no extra parser, so choose by your dependency budget.
Report a problem with this articleOpens a pre-filled issue on GitHub.