CAC CLI Framework

7.0.0 · active · verified Sun Apr 19

CAC (Command And Conquer) is a lightweight, dependency-free JavaScript library for building robust command-line interface (CLI) applications. The current stable version, 7.0.0, released in 2024, marks a significant transition to being ESM-only and requires Node.js 20.19.0 or newer. CAC differentiates itself by providing a minimal API surface (four core methods for basic CLIs) while offering powerful features such as default commands, Git-like subcommands, argument and option validation, variadic arguments, and automated help message generation. Its development is TypeScript-first, ensuring type safety and an enhanced developer experience. Release cadence is typically feature-driven, with breaking changes carefully documented in major version bumps and minor/patch releases focusing on stability and compatibility.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize CAC, define options and commands, set a version, and enable automatic help message generation. It includes a basic error handling block for parsing issues.

import cac from 'cac'

const cli = cac()

cli.option('--type [type]', 'Choose a project type', {
  default: 'node',
})
cli.option('--name <name>', 'Provide your name')

cli.command('lint [...files]', 'Lint files').action((files, options) => {
  console.log(`Linting files: ${files.join(', ')} with options: ${JSON.stringify(options)}`)
})

// Display help message when `-h` or `--help` appears
cli.help()
// Display version number when `-v` or `--version` appears
cli.version('1.0.0') // Set your CLI's version

try {
  cli.parse()
} catch (error) {
  // Handle errors like unknown commands or options
  console.error(`CLI Error: ${error.message}`)
  process.exit(1)
}

// To run this: save as e.g. cli.mjs and execute with `node cli.mjs --help` or `node cli.mjs lint src/*.js`

view raw JSON →