CLI Parameter Reducers

2.1.0 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

Demonstrates basic CLI argument parsing for help flags, strings, and integers, including default values and help text output for a simple 'repeat' command.

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

view raw JSON →