CLI Argument Parsing
Parse argv with a typed schema so flags become typed values, unknown flags fail, and a usage message prints on a parse error.
On this page
Introduction
This guide covers typed CLI argument parsing, a recurring concern in the cli-tooling scenario. Hand-rolled argv parsing breaks the moment a flag takes a number or a user passes an unknown option. A typed parser turns argv into a validated object, rejects unknown flags, and prints usage on error, which removes a class of bugs that otherwise surface as undefined values deep in a handler.
Declare typed flags
Give each flag a type, a description, and a default, so the parser coerces strings to numbers or booleans and the help text is generated from the declaration.
The trade-off worth naming is that give each flag a type, a description, and a default, so the parser coerces strings to numbers or booleans and the help text is generated from the declaration 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 yargs from 'yargs';
const opts = yargs(process.argv.slice(2))
.option('port', { type: 'number', default: 3000, describe: 'Listen port' })
.option('verbose', { type: 'boolean', default: false })
.strict().fail((msg) => { console.error(msg); process.exit(1); }).parse();
Fail on unknown
Use strict mode so an unknown flag exits non-zero with usage, because a typo should not silently produce default behavior.
The trade-off worth naming is that use strict mode so an unknown flag exits non-zero with usage, because a typo should not silently produce default 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.
Print usage on error
On a parse error, print the message and the usage and exit non-zero, so the user learns what was wrong and how to fix it in one step.
The trade-off worth naming is that on a parse error, print the message and the usage and exit non-zero, so the user learns what was wrong and how to fix it in one step 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.
Coerce types
Let the parser coerce port to a number and verbose to a boolean, so the handler receives typed values and does not re-parse strings.
The trade-off worth naming is that let the parser coerce port to a number and verbose to a boolean, so the handler receives typed values and does not re-parse strings 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.
Positional args
Declare positional arguments with names so they appear in help and are not indexed by magic numbers in the handler, which is clearer and safer.
The trade-off worth naming is that declare positional arguments with names so they appear in help and are not indexed by magic numbers in the handler, which is clearer and safer 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.
cli.command('build <src> <out>').action((src, out) => build(src, out));
Test parsing
Parse a known argv and assert the typed object, then parse an unknown flag and assert a non-zero exit, so the contract holds.
The trade-off worth naming is that parse a known argv and assert the typed object, then parse an unknown flag and assert a non-zero exit, so the contract holds 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
FAQ
- Use a library or hand-roll?
- Use a library like Commander or yargs; hand-rolled parsing misses edge cases like combined short flags, equals values, and unknown option detection, which a library has already solved.
- Should unknown flags fail or warn?
- Fail, because a typo in a flag should not silently produce default behavior the user did not ask for; a non-zero exit with usage is the safe default.
Report a problem with this articleOpens a pre-filled issue on GitHub.