[Tools] Create a Simple CLI Tool in Node.js with CAC

Command-line tools can help you with all sorts of tasks. This lesson covers the very basics of setting up a CLI tool in Node.js by creating your project with npm, setting up your bin script, and using CAC to parse a single argument.

Create a new project, change the "name" porp‘s value to "hi", then add a "bin" prop, so next time, when we invoke "hi", it will run the command in "bin".

package.json

{
  "name": "hi",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "bin": "./index.js",
  "devDependencies": {
    "cac": "6.3.12"
  }
}

Install:

npm i -D cac

Create index.js file:

  • Make sure you have ‘

    #!/usr/bin/env node

    ‘ on the top of the file, then it knows should run in node env.

  • Using cacto build commad, you can define ‘option‘, ‘command‘
  • Last you should always call cli.parse() to run the command
#!/usr/bin/env node

const cli = require(‘cac‘)();
cli.option(‘--type <type>‘, ‘Provide type, [date|foo]‘)
// name is a required field
cli.command(‘<name>‘, ‘Provide your name‘)
    .action((name, options) => {
        const {type} = options;
        if (type === ‘date‘) {
            console.log(`Hi ${name}, Today is ${new Date().toDateString()}`)
        } else if (type === ‘foo‘) {
            console.log(`Hi ${name}, you should take a rest!`)
        } else {
            console.log(`Hi ${name}, Good job!`)
        }

    })

cli.help()
// Display version number when `-h` or `--help` appears
cli.version(‘0.0.0‘)
cli.parse()

Run:

原文地址:https://www.cnblogs.com/Answer1215/p/10220555.html

时间: 2024-11-05 19:27:48

[Tools] Create a Simple CLI Tool in Node.js with CAC的相关文章

Node.js Tools 1.2 for Visual Studio 2015 released

https://blogs.msdn.microsoft.com/visualstudio/2016/07/28/node-js-tools-1-2-visual-studio-2015/ What time is it?! Time to announce that our next stable release of Node.js Tools 1.2 for Visual Studio (NTVS) is available for download! NTVS 1.2 supports

A chatroom for all! Part 1 - Introduction to Node.js(转发)

项目组用到了 Node.js,发现下面这篇文章不错.转发一下.原文地址:<原文>. ------------------------------------------- A chatroom for all! Part 1 - Introduction to Node.js Rami Sayar 4 Sep 2014 11:00 AM 7 This node.js tutorial series will help you build a node.js powered real-time

[Node.js] Testing ES6 Promises in Node.js using Mocha and Chai

Writing great ES6 style Promises for Node.js is only half the battle. Your great modules must include tests as well to ensure future iterations don't break them. In this lesson, I show you a simple ES6 Promise in Node.js, then walk you through creati

[Node.js] OAuth 2 和 passport框架

原文地址:http://www.moye.me/?p=592 OAuth是什么 OAuth(开放授权)是一个开放标准,允许用户让第三方应用访问该用户在某一网站上存储的私密的资源(如照片,视频,联系人列表),而无需将用户名和密码提供给第三方应用. OAuth 2.0   OAuth的版本有v1.0, v1.0a 和 v2.0.OAuth 2.0 的出现主要是解决1.0+中的几个问题,提升开发简易度和应用安全性: 更好的支持非浏览器APP(移动和桌面客户端,可以省略v1.0的交换token过程,增强

[CLI] Create a Single-Command Node.js CLI with Oclif, TypeScript and Yarn Workspaces

The fastest way to create a robust, cross-platform compatible Node.js CLI (optionally typed with TypeScript) is by running npx oclif single mycli. Here we explain what this means and how and why to integrate this with Yarn Workspaces for an ideal dev

[Tools] Batch Create Markdown Files from a Template with Node.js and Mustache

Creating Markdown files from a template is a straightforward process with Node.js and Mustache. You can define a template, load it into your script, then push whatever data you have into your template, then write the files back out. Node.js built-in

A Simple MVC Framework With Node and Express

I love frameworks. As soon as I dropped my programmer's ego and learned to embrace well conceived conventions over configuration my development and deployment times felt the benefit. On the other hand, I like understanding what is going on underneath

NTVS Node.js Tools for Visual Studio 安装各种奇葩问题汇总。

首先是正确的安装方式.以vs 2012为例子,操作系统windows server 2012 R2,建议用虚拟机搞起. 1.安装vs 2012 2.升级2012 到 update 4 注意:一定要升级,不然安装1.0 Alpha后,在创建项目的时候会提示“未将对象引用到实例”.安装1.0 Beta后F5无法debug调试.总之升级就是了不要废话. 3.安装node.msi 也就是node windows 安装包 http://nodejs.org/download/ 选择对应的操作系统下载,安装

Node.js tools for visual studio 在vs中使用Node.js

简单介绍 PTVS开发团队又开发出一款可以在VS里编写Node.js应用程序的插件——NTVS(Node.js Tools for Visual Studio),开发者可以在VS里轻松开发Node.js应用. NTVS是一款开源工具,遵循Apache开源许可,由微软和社区维护.适用于Node.js 0.10.20版或更高的版本上.NTVS具有可编辑.智能提示.分析.NPM.调式(本地和远程)等功能,并且还可以发布在Azure网站和Cloud服务上. Node.js 可在32位和64位架构上运行,