Commandpost CLI Parser

1.4.0 · active · verified Tue Apr 21

Commandpost is a command-line option parser library for Node.js, designed with a strong emphasis on TypeScript developer experience while remaining fully compatible with JavaScript projects. It provides a structured, chainable API for defining root commands, sub-commands, various types of options (e.g., flags, parameters), and arguments (required, optional, variadic). Inspired by the popular `commander` library, Commandpost differentiates itself by addressing `commander`'s limitations regarding robust TypeScript type inference and usage, offering a more type-safe approach to CLI development. The current stable version is 1.4.0, with releases historically following an irregular cadence based on community contributions and author availability, rather than fixed schedules. Key features include automatic help message generation, versioning, and descriptive capabilities for commands and options. Its core strength lies in enabling developers to build type-safe CLI applications with a familiar, fluid API.

Common errors

Warnings

Install

Imports

Quickstart

Sets up a basic CLI command `dinner <food>` with an optional `--spice` flag. This example demonstrates defining commands, parsing options and arguments, and handling execution errors.

import * as commandpost from 'commandpost';

let root = commandpost
	.create<{ spice: string[]; }, { food: string; }>( "dinner <food>")
	.version("1.0.0", "-v, --version")
	.description("today's dinner!")
	.option("-s, --spice <name>", "What spice do you want? default: pepper")
	.action((opts, args) => {
		console.log(`Your dinner is ${args.food} with ${opts.spice[0] || "pepper"}!`);
	});

commandpost
	.exec(root, process.argv)
	.catch(err => {
		if (err instanceof Error) {
			console.error(err.stack);
		} else {
			console.error(err);
		}
		process.exit(1);
	});

view raw JSON →