Socket
Socket
Sign inDemoInstall

cac

Package Overview
Dependencies
46
Maintainers
1
Versions
120
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    cac

Command & Conquer, the command-line queen.


Version published
Weekly downloads
5.9M
increased by3.06%
Maintainers
1
Install size
554 kB
Created
Weekly downloads
 

Package description

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

Readme

Source

cac NPM version NPM downloads Build Status

Command And Conquer, the queen living in your command line.

Install

$ npm install --save cac

Usage

const cac = require('cac')

// initialize your cli program
const cli = cac()

// add your very first command
cli.command('hi', 'Say hi!', (input) => {
  console.log(`hi ${input[1] || 'boy'}!`)
})

// parse arguments and bootstrap
cli.parse()

Default commands and options

  • Options: --help -h --version -v
  • Commands: help

API

.option(options, description, defaultValue)

  • options: string, it could be option or option, alias or alias, option, the order does not matter. eg: .option('install, i')
  • description: string, option description, will be used to output cli usage
  • defaultValue: any, give a default value to this option

.command(commands, description, fn)

  • commands: string, it could be command or command, alias or alias, command, the order does not matter. eg: .command('clone, c'). It can also be a wildcard symbol *, which means always been triggered if no command was specified by user.
  • description: string, command description, will be used to output cli usage
  • fn: function, command function, will be triggered when this command matches user's input, the function takes two arguments:
    • input: non-flag arguments
    • flags: flags converted to camelCase

.parse(argv)

  • argv: array, default is process.argv.slice(2)

.onError(handleError)

  • handleError: function, triggered when your program throws an error or was rejected by a Promise call.

License

MIT © egoist

Keywords

FAQs

Last updated on 28 Aug 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc