What is cac?
The cac npm package is a simple yet powerful framework for building command-line applications. It allows developers to parse arguments, generate help messages, and create commands with options and sub-commands.
What are cac's main functionalities?
Command Parsing
This feature allows you to define commands with required and optional arguments. The code sample demonstrates how to create a command 'init' that requires a project name and accepts an optional type argument.
{"const cac = require('cac');\nconst cli = cac();\ncli.command('init <name>', 'Initialize a project')\n .option('--type <type>', 'Project type')\n .action((name, options) => {\n console.log(`Initializing project: ${name} with type: ${options.type}`);\n });\ncli.help();\ncli.parse();"}
Help Generation
Automatically generates help information for the defined commands and options. The code sample shows how to define a 'build' command and an option to minify the output, with automatic help generation.
{"const cac = require('cac');\nconst cli = cac();\ncli.command('build', 'Build the project')\n .option('--minify', 'Minify the output')\ncli.help();\ncli.parse();"}
Sub-commands
Supports the creation of sub-commands for more complex CLI structures. The code sample illustrates how to create a 'deploy' command with sub-commands for different deployment providers like AWS and Azure.
{"const cac = require('cac');\nconst cli = cac();\nconst deploy = cli.command('deploy <provider>', 'Deploy your project');\ndeploy.command('aws', 'Deploy to AWS')\n .action(() => {\n console.log('Deploying to AWS...');\n });\ndeploy.command('azure', 'Deploy to Azure')\n .action(() => {\n console.log('Deploying to Azure...');\n });\ncli.help();\ncli.parse();"}
Other packages similar to cac
commander
Commander is one of the most popular npm packages for building command-line interfaces. It offers similar functionalities to cac, such as command parsing, option management, and automatic help generation. Commander has a more extensive feature set and a larger community, but cac is known for its simplicity and ease of use.
yargs
Yargs is another well-known package for building command-line tools. It provides a rich API for argument parsing, command chaining, and validation. Yargs is more verbose and configurable compared to cac, which might be preferred for more complex CLI applications.
meow
Meow is a lightweight CLI helper with a minimalistic approach. It is less feature-rich than cac but is very straightforward to use for simple command-line applications. Meow is a good choice for those who need something simpler than cac and do not require complex command structures.
CAC
Command And Conquer, the queen living in your command line.
Features
Install
$ npm install --save cac
Usage
Start your first CLI app in example.js
:
const cac = require('cac')
const fs = require('fs-promise')
const cli = cac(`
Usage:
node example.js create <filename> -m [content]
Commands:
c, create Create a file with specific content
Options:
-m, --message File content
-h, --help Print help (You are here!)
`, {
alias: {
m: message,
h: help
}
})
cli.command('c, create', function* () {
const fileName = this.input[1]
const content = this.flags.message
yield fs.createFile(fileName, 'hello')
console.log('Done!')
})
cli.command('*', function () {
console.log('Everything else')
}}
cli.parse()
All set! Just run:
$ node example.js create lol.txt -m "just lol 😂"
API
cac([help], [options])
help
Type: string
array
The help info.
options
The minimist options.
License
MIT © EGOIST