[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:

[Tools] Create a Simple CLI Tool in Node.js with CAC的更多相关文章
- 【Tool】Node.js 安装
Node.js 安装 2019-07-29 14:56:14 by冲冲 1. 说明 电脑使用node.js存在两种方式: ① 第一种,下载(.exe)到本地电脑,不需要安装,下载后点击 node. ...
- [转]Getting Start With Node.JS Tools For Visual Studio
本文转自:http://www.c-sharpcorner.com/UploadFile/g_arora/getting-started-with-node-js-tools-for-visual-s ...
- 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 ...
- A chatroom for all! Part 1 - Introduction to Node.js(转发)
项目组用到了 Node.js,发现下面这篇文章不错.转发一下.原文地址:<原文>. ------------------------------------------- A chatro ...
- [转]Node.js tutorial in Visual Studio Code
本文转自:https://code.visualstudio.com/docs/nodejs/nodejs-tutorial Node.js tutorial in Visual Studio Cod ...
- Node.js之包与npm包管理工具
Node.js之包与npm包管理工具 1.Node.js中的包 1.1在一个包中包含如下内容: package.json:对包进行描述 在bin子目录中存放二进制文件 在lib子目录中存放JavaSc ...
- Node.js Web 开发框架大全《中间件篇》
这篇文章与大家分享优秀的 Node.js 中间件模块.Node 是一个服务器端 JavaScript 解释器,它将改变服务器应该如何工作的概念.它的目标是帮助程序员构建高度可伸缩的应用程序,编写能够处 ...
- 【特别推荐】Node.js 入门教程和学习资源汇总
这篇文章与大家分享一批很有用的 Node.js 入门教程和学习资源.Node 是一个服务器端的 JavaScript 解释器,它将改变服务器应该如何工作的概念.它的目标是帮助程序员构建高度可伸缩的应用 ...
- Node.js 入门教程和学习资源汇总
这篇文章与大家分享一批很有用的 Node.js 入门教程和学习资源.Node 是一个服务器端的 JavaScript 解释器,它将改变服务器应该如何工作的概念.它的目标是帮助程序员构建高度可伸缩的应用 ...
随机推荐
- 《c程序设计语言》-2.9
#include <stdio.h> /*int bitcount(unsigned x) { int b; for(b = 0;x != 0;x >>= 1) { if(x ...
- hdu 6114 百度之星复赛B T1
Chess Problem Description 車是中国象棋中的一种棋子,它能攻击同一行或同一列中没有其他棋子阻隔的棋子. 一天,小度在棋盘上摆起了许多車……他想知道,在一共N×M个点的矩形棋盘中 ...
- 一种提高Android应用进程存活率新方法
一.基础知识 1.Android 进程优先级 1.1 进程优先级等级一般分法:- Activte process- Visible Process- Service process- Backgrou ...
- jquery.uploadify不支持MVC的Authorize
原文发布时间为:2011-10-18 -- 来源于本人的百度文章 [由搬家工具导入] 为什么jquery.uploadify不支持MVC的Authorize呢,因为flash的cookie跟服务端的不 ...
- flexigrid 学习总结
最近看到了一款flexigrid表格组件,简单美观,在下载使用的过程中,发现缺少很多功能.<基于jQuery的GridView-Flexigrid(2)-扩展和修复>给我带来了很大的帮助, ...
- C# 使用文件流来读写ini文件
背景 之前采用ini文件作为程序的配置文件,觉得这种结构简单明了,配置起来也挺方便.然后操作方式是通过WindowsAPI,然后再网上找到一个基于WindowsAPI封装的help类,用起来倒也顺手. ...
- session和xsrf
1.pip install pycket 2.pip install redis 防止xsrf攻击只需在模板form标签加入: {% module xsrf_form_html() %} <!D ...
- C语言中函数strcpy ,strncpy ,strlcpy的用法【转】
转自:http://blog.chinaunix.net/uid-20797562-id-99311.html strcpy ,strncpy ,strlcpy的用法好多人已经知道利用strncpy替 ...
- vue-cli 3.0脚手架与vux的配合使用
在最近的项目中,引用了vux,在可拓展性以及复用性,都算是比较优秀的框架了.但是美中不足的是对于vux在对于vue-cli3.0的跟进还没有同步 需要自己做下修改,同比 有赞的vant 以及 ivie ...
- linux dev设备
/dev目录的文件都是设备,我们可以像操作文件一样操作设备.但是究竟什么才是设备文件哪?作为程序员不能用代码敲出来的概念我们都是不喜欢的,所以本文用shell来操作体验什么才是/dev文件. 我们执行 ...