TypeScript icon, indicating that this package has built-in type declarations

6.3.6 • Public • Published

2017-07-26 9 27 05

NPM version NPM downloads CircleCI donate chat install size


Command And Conquer is a JavaScript library for building CLI apps.


  • Super light-weight: No dependency, just a single file.
  • Easy to learn. There're only 4 APIs you need to learn for building simple CLIs: cli.option cli.version cli.parse.
  • Yet so powerful. Enable features like default command, git-like subcommands, validation for required arguments and options, variadic arguments, dot-nested options, automated help message generation and so on.
  • Developer friendly. Written in TypeScript.

Table of Contents


yarn add cac


Simple Parsing

Use CAC as simple argument parser:

// examples/basic-usage.js
const cli = require('cac')()

cli.option('--type [type]', 'Choose a project type', {
  default: 'node'

const parsed = cli.parse()

console.log(JSON.stringify(parsed, null, 2))

2018-11-26 12 28 03

Display Help Message and Version

// examples/help.js
const cli = require('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(files, options)

// Display help message when `-h` or `--help` appears
// Display version number when `-h` or `--help` appears


2018-11-25 8 21 14

Command-specific Options

You can attach options to a command.

const cli = require('cac')()

  .command('rm <dir>', 'Remove a dir')
  .option('-r, --recursive', 'Remove recursively')
  .action((dir, options) => {
    console.log('remove ' + dir + (options.recursive ? ' recursively' : ''))


A command's options are validated when the command is used. Any unknown options will be reported as an error. However, if an action-based command does not define an action, then the options are not validated. If you really want to use unknown options, use command.allowUnknownOptions.

command options


When using brackets in command name, angled brackets indicate required command arguments, while sqaure bracket indicate optional arguments.

When using brackets in option name, angled brackets indicate that the option value is required, while sqaure bracket indicate that the value is optional.

const cli = require('cac')()

  .command('deploy <folder>', 'Deploy a folder to AWS')
  .option('--scale [level]', 'Scaling level')
  .action((folder, options) => {


Variadic Arguments

The last argument of a command can be variadic, and only the last argument. To make an argument variadic you have to add ... to the start of argument name, just like the rest operator in JavaScript. Here is an example:

const cli = require('cac')()

  .command('build <entry> [...otherFiles]', 'Build your app')
  .option('--foo', 'Foo option')
  .action((entry, otherFiles, options) => {


2018-11-25 8 25 30

Dot-nested Options

Dot-nested options will be merged into a single option.

const cli = require('cac')()

  .command('build', 'desc')
  .option('--env <env>', 'Set envs')
  .example('--env.API_SECRET xxx')
  .action(options => {


2018-11-25 9 37 53

Default Command

Register a command that will be used when no other command is matched.

const cli = require('cac')()

  // Simply omit the command name, just brackets
  .command('[...files]', 'Build files')
  .option('--minimize', 'Minimize output')
  .action((files, options) => {


With TypeScript

First you need @types/node to be installed as a dev dependency in your project:

yarn add @types/node --dev

Then everything just works out of the box:

const cac = require('cac')
// OR ES modules
import cac from 'cac'

Projects Using CAC

Projects that use CAC:

  • VuePress: 📝 Minimalistic Vue-powered static site generator.
  • SAO: ⚔️ Futuristic scaffolding tool.
  • DocPad: 🏹 Powerful Static Site Generator.
  • Poi: ⚡️ Delightful web development.
  • bili: 🥂 Schweizer Armeemesser for bundling JavaScript libraries.
  • lass: 💁🏻 Scaffold a modern package boilerplate for Node.js.
  • Feel free to add yours here...


💁 Check out the generated docs from source code if you want a more in-depth API references.

Below is a brief overview.

CLI Instance

CLI instance is created by invoking the cac function:

const cac = require('cac')
const cli = cac()

cli.command(name, description, config?)

  • Type: (name: string, description: string) => Command

Create a command instance.

The option also accepts a third argument config for addtional command config:

  • config.allowUnknownOptions: boolean Allow unknown options in this command.
  • config.ignoreOptionDefaultValue: boolean Don't use the options's default value in parsed options, only display them in help message.

cli.option(name, description, config?)

  • Type: (name: string, description: string, config?: OptionConfig) => CLI

Add a global option.

The option also accepts a third argument config for addtional option config:

  • config.default: Default value for the option.
  • config.coerce: (value: any) => newValue A function to process the option value.


  • Type: (argv = process.argv) => ParsedArgv
interface ParsedArgv {
  args: string[]
  options: {
    [k: string]: any

When this method is called, cli.rawArgs cli.args cli.options cli.matchedCommand will also be available.

cli.version(version, customFlags?)

  • Type: (version: string, customFlags = '-v, --version') => CLI

Output version number when -v, --version flag appears.

  • Type: (callback?: HelpCallback) => CLI

Output help message when -h, --help flag appears.

Optional callback allows post-processing of help text before it is displayed:

type HelpCallback = (sections: HelpSection[]) => void

interface HelpSection {
  title?: string
  body: string


  • Type: (subCommand?: boolean) => CLI

Output help message. Optional subCommand argument if you want to output the help message for the matched sub-command instead of the global help message.

Command Instance

Command instance is created by invoking the cli.command method:

const command = cli.command('build [...files]', 'Build given files')


Basically the same as cli.option but this adds the option to specific command.


  • Type: (callback: ActionCallback) => Command

Use a callback function as the command action when the command matches user inputs.

type ActionCallback = (
  // Parsed CLI args
  // The last arg will be an array if it's an varadic argument
  ...args: string | string[] | number | number[]
  // Parsed CLI options
  options: Options
) => any

interface Options {
  [k: string]: any


  • Type: (name: string) => Command

Add an alias name to this command, the name here can't contain brackets.


  • Type: () => Command

Allow unknown options in this command, by default CAC will log an error when unknown options are used.


  • Type: (example: CommandExample) => Command

Add an example which will be displayed at the end of help message.

type CommandExample = ((bin: string) => string) | string


Listen to commands:

// Listen to the `foo` command
cli.on('command:foo', () => {
  // Do something

// Listen to the default command
cli.on('command:!', () => {
  // Do something

// Listen to unknown commands
cli.on('command:*', () => {
  console.error('Invalid command: %', cli.args.join(' '))


How is the name written and pronounced?

CAC, or cac, pronounced C-A-C.

This project is dedicated to our lovely C.C. sama. Maybe CAC stands for C&C as well :P

Why not use Commander.js?

CAC is very similar to Commander.js, while the latter does not support dot nested options, i.e. something like --env.API_SECRET foo. Besides, you can't use unknown options in Commander.js either.

And maybe more...

Basically I made CAC to fulfill my own needs for building CLI apps like Poi, SAO and all my CLI apps. It's small, simple but powerful :P


  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D


CAC © EGOIST, Released under the MIT License.
Authored and maintained by egoist with help from contributors (list).

Website · GitHub @egoist · Twitter @_egoistlily




Package Sidebar






Unpacked Size

45.1 kB

Total Files


Last publish


  • kchan
  • egoist
  • rem