CLI Parameter Reducers
parameter-reducers is a JavaScript/TypeScript library designed for building robust and type-safe command-line interface (CLI) parameter parsers. Its current stable version is 2.1.0, and it appears to follow an active development cadence with recent feature additions and a significant breaking change in version 2.0.0. A key differentiator is its immutable, reducer-based chain API, which allows for flexible composition and reuse of parameter definitions. This approach facilitates highly customizable parsing logic while leveraging TypeScript to provide strong type guarantees for parsed arguments, reducing runtime errors associated with CLI argument processing. It focuses on core parsing capabilities, intentionally leaving documentation generation to user-defined template literals.
Common errors
-
🚨 Unrecognized option --some-unexpected-flag.
cause The CLI was invoked with an option or argument that was not defined in the parameter chain, and the `parseResult.rest` array was not properly handled or `extract()` was not used.fixEnsure all possible CLI arguments are covered by `addParam` calls in your chain. If unexpected arguments are acceptable, explicitly check `parseResult.rest.length` after parsing and decide how to handle them. Using `parseResult.extract()` will automatically detect and report unrecognized options. -
TypeError: Cannot read properties of undefined (reading 'message')
cause Attempting to access a parsed parameter (e.g., `message`) that was not provided on the command line, and no default value was assigned during destructuring.fixProvide a default value when destructuring the parsed parameters, e.g., `const { message = 'default value' } = parse(...).extract();`. Alternatively, ensure the parameter is always present in CLI invocations or add validation checks.
Warnings
- breaking In version 2.0.0, the order of precedence for parameters was reversed. If you previously defined a parameter twice (e.g., a string and then a flag with the same name), the last defined parameter now takes precedence, whereas previously it was the first. This can change the effective type and value of parameters.
- gotcha The `parse()` function returns an object that can represent either a valid parsing result or an error. Users must explicitly check `parseResult.valid` and `parseResult.rest.length` to ensure all arguments were parsed successfully and without errors. Failing to do so can lead to silent errors or incorrect processing of unhandled arguments.
- gotcha Default values for parameters are not set within the `param` definition itself. Instead, they are applied through JavaScript's object destructuring assignment when extracting the parsed results.
- gotcha The library explicitly does not provide built-in functionality for generating help documentation for your CLI. Developers are expected to manage and render help text manually.
Install
-
npm install parameter-reducers -
yarn add parameter-reducers -
pnpm add parameter-reducers
Imports
- startChain
const { startChain } = require('parameter-reducers');import { startChain } from 'parameter-reducers'; - param
import param from 'parameter-reducers';
import { param } from 'parameter-reducers'; - parse
import parse from 'parameter-reducers';
import { parse } from 'parameter-reducers';
Quickstart
import {startChain, param, parse} from 'parameter-reducers';
const params = startChain()
.addParam(param.flag(['-h', '--help'], 'help'))
.addParam(param.string(['-m', '--message'], 'message'))
.addParam(param.integer(['-c', '--count'], 'count'));
const {help = false, message = 'hello world', count = 1} = parse(
params,
process.argv.slice(2),
).extract();
if (help) {
console.log(`Usage: repeat -m "My Message" -c 10\n\nparameters:\n\n-h --help Print this help\n-m --message The message to print\n-c --count How many times to print the message`);
}
for (let i = 0; i < count; i++) {
console.log(message);
}
// Example execution: node your-script.ts -m "Hello CLI" -c 3