Gathering detailed insights and metrics for cac
Gathering detailed insights and metrics for cac
Gathering detailed insights and metrics for cac
Gathering detailed insights and metrics for cac
complitech-sdk
CompliTech SDK for business registration with CAC Nigeria
@bronya.js/cli
type-safe cli builder inspired by cac
@awsless/weak-cache
[![npm version](https://img.shields.io/npm/dw/@awsless/weak-cache)](https://www.npmjs.org/package/@awsless/weak-cache) [![npm version](https://img.shields.io/npm/v/@awsless/weak-cache.svg?style=flat-square)](https://www.npmjs.org/package/@awsless/weak-cac
@cac/required-option
Mark an option as required for specific command.
Simple yet powerful framework for building command-line apps.
npm install cac
99.4
Supply Chain
99.6
Quality
79.9
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
2,654 Stars
124 Commits
104 Forks
16 Watching
27 Branches
34 Contributors
Updated on 26 Nov 2024
TypeScript (94.29%)
JavaScript (5.71%)
Cumulative downloads
Total Downloads
Last day
3%
1,616,239
Compared to previous day
Last week
4.8%
8,264,591
Compared to previous week
Last month
14.8%
33,524,311
Compared to previous month
Last year
140.6%
282,789,077
Compared to previous year
28
Command And Conquer is a JavaScript library for building CLI apps.
cli.option
cli.version
cli.help
cli.parse
.1yarn add cac
Use CAC as simple argument parser:
1// examples/basic-usage.js 2const cli = require('cac')() 3 4cli.option('--type <type>', 'Choose a project type', { 5 default: 'node', 6}) 7 8const parsed = cli.parse() 9 10console.log(JSON.stringify(parsed, null, 2))
1// examples/help.js 2const cli = require('cac')() 3 4cli.option('--type [type]', 'Choose a project type', { 5 default: 'node', 6}) 7cli.option('--name <name>', 'Provide your name') 8 9cli.command('lint [...files]', 'Lint files').action((files, options) => { 10 console.log(files, options) 11}) 12 13// Display help message when `-h` or `--help` appears 14cli.help() 15// Display version number when `-v` or `--version` appears 16// It's also used in help message 17cli.version('0.0.0') 18 19cli.parse()
You can attach options to a command.
1const cli = require('cac')() 2 3cli 4 .command('rm <dir>', 'Remove a dir') 5 .option('-r, --recursive', 'Remove recursively') 6 .action((dir, options) => { 7 console.log('remove ' + dir + (options.recursive ? ' recursively' : '')) 8 }) 9 10cli.help() 11 12cli.parse()
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
.
Options in kebab-case should be referenced in camelCase in your code:
1cli 2 .command('dev', 'Start dev server') 3 .option('--clear-screen', 'Clear screen') 4 .action((options) => { 5 console.log(options.clearScreen) 6 })
In fact --clear-screen
and --clearScreen
are both mapped to options.clearScreen
.
When using brackets in command name, angled brackets indicate required command arguments, while square bracket indicate optional arguments.
When using brackets in option name, angled brackets indicate that a string / number value is required, while square bracket indicate that the value can also be true
.
1const cli = require('cac')() 2 3cli 4 .command('deploy <folder>', 'Deploy a folder to AWS') 5 .option('--scale [level]', 'Scaling level') 6 .action((folder, options) => { 7 // ... 8 }) 9 10cli 11 .command('build [project]', 'Build a project') 12 .option('--out <dir>', 'Output directory') 13 .action((folder, options) => { 14 // ... 15 }) 16 17cli.parse()
To allow an option whose value is false
, you need to manually specify a negated option:
1cli 2 .command('build [project]', 'Build a project') 3 .option('--no-config', 'Disable config file') 4 .option('--config <path>', 'Use a custom config file')
This will let CAC set the default value of config
to true, and you can use --no-config
flag to set it to false
.
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:
1const cli = require('cac')() 2 3cli 4 .command('build <entry> [...otherFiles]', 'Build your app') 5 .option('--foo', 'Foo option') 6 .action((entry, otherFiles, options) => { 7 console.log(entry) 8 console.log(otherFiles) 9 console.log(options) 10 }) 11 12cli.help() 13 14cli.parse()
Dot-nested options will be merged into a single option.
1const cli = require('cac')() 2 3cli 4 .command('build', 'desc') 5 .option('--env <env>', 'Set envs') 6 .example('--env.API_SECRET xxx') 7 .action((options) => { 8 console.log(options) 9 }) 10 11cli.help() 12 13cli.parse()
Register a command that will be used when no other command is matched.
1const cli = require('cac')() 2 3cli 4 // Simply omit the command name, just brackets 5 .command('[...files]', 'Build files') 6 .option('--minimize', 'Minimize output') 7 .action((files, options) => { 8 console.log(files) 9 console.log(options.minimize) 10 }) 11 12cli.parse()
1node cli.js --include project-a 2# The parsed options will be: 3# { include: 'project-a' } 4 5node cli.js --include project-a --include project-b 6# The parsed options will be: 7# { include: ['project-a', 'project-b'] }
To handle command errors globally:
1try { 2 // Parse CLI args without running the command 3 cli.parse(process.argv, { run: false }) 4 // Run the command yourself 5 // You only need `await` when your command action returns a Promise 6 await cli.runMatchedCommand() 7} catch (error) { 8 // Handle error here.. 9 // e.g. 10 // console.error(error.stack) 11 // process.exit(1) 12}
First you need @types/node
to be installed as a dev dependency in your project:
1yarn add @types/node --dev
Then everything just works out of the box:
1const { cac } = require('cac') 2// OR ES modules 3import { cac } from 'cac'
1import { cac } from 'https://unpkg.com/cac/mod.ts' 2 3const cli = cac('my-program')
Projects that use CAC:
💁 Check out the generated docs from source code if you want a more in-depth API references.
Below is a brief overview.
CLI instance is created by invoking the cac
function:
1const cac = require('cac') 2const cli = cac()
Create a CLI instance, optionally specify the program name which will be used to display in help and version message. When not set we use the basename of argv[1]
.
(name: string, description: string) => Command
Create a command instance.
The option also accepts a third argument config
for additional 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.(name: string, description: string, config?: OptionConfig) => CLI
Add a global option.
The option also accepts a third argument config
for additional option config:
config.default
: Default value for the option.config.type
: any[]
When set to []
, the option value returns an array type. You can also use a conversion function such as [String]
, which will invoke the option value with String
.(argv = process.argv) => ParsedArgv
1interface ParsedArgv { 2 args: string[] 3 options: { 4 [k: string]: any 5 } 6}
When this method is called, cli.rawArgs
cli.args
cli.options
cli.matchedCommand
will also be available.
(version: string, customFlags = '-v, --version') => CLI
Output version number when -v, --version
flag appears.
(callback?: HelpCallback) => CLI
Output help message when -h, --help
flag appears.
Optional callback
allows post-processing of help text before it is displayed:
1type HelpCallback = (sections: HelpSection[]) => void 2 3interface HelpSection { 4 title?: string 5 body: string 6}
() => CLI
Output help message.
(text: string) => CLI
Add a global usage text. This is not used by sub-commands.
Command instance is created by invoking the cli.command
method:
1const command = cli.command('build [...files]', 'Build given files')
Basically the same as cli.option
but this adds the option to specific command.
(callback: ActionCallback) => Command
Use a callback function as the command action when the command matches user inputs.
1type ActionCallback = ( 2 // Parsed CLI args 3 // The last arg will be an array if it's a variadic argument 4 ...args: string | string[] | number | number[] 5 // Parsed CLI options 6 options: Options 7) => any 8 9interface Options { 10 [k: string]: any 11}
(name: string) => Command
Add an alias name to this command, the name
here can't contain brackets.
() => Command
Allow unknown options in this command, by default CAC will log an error when unknown options are used.
(example: CommandExample) => Command
Add an example which will be displayed at the end of help message.
1type CommandExample = ((name: string) => string) | string
(text: string) => Command
Add a usage text for this command.
Listen to commands:
1// Listen to the `foo` command 2cli.on('command:foo', () => { 3 // Do something 4}) 5 6// Listen to the default command 7cli.on('command:!', () => { 8 // Do something 9}) 10 11// Listen to unknown commands 12cli.on('command:*', () => { 13 console.error('Invalid command: %s', cli.args.join(' ')) 14 process.exit(1) 15})
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
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
git checkout -b my-new-feature
git commit -am 'Add some feature'
git push origin my-new-feature
CAC © EGOIST, Released under the MIT License.
Authored and maintained by egoist with help from contributors (list).
Website · GitHub @egoist · Twitter @_egoistlily
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 13/30 approved changesets -- score normalized to 4
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
19 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More