[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 解释器,它将改变服务器应该如何工作的概念.它的目标是帮助程序员构建高度可伸缩的应用 ...
随机推荐
- .ini配置读取
ini文件的格式 格式 INI文件由节.键.值组成. 节 [section] 参数(键=值) name=value 注解 注解使用分号表示(;).在分号后面的文字,直到该行结尾都全部为注解. ...
- Balanced Lineup(RMQ)
原题传送门 就是裸RMQ啊.. 求区间最大值和区间最小值,一看就像RMQ,当然线段树貌似也可以. 至于算法嘛.自己学~(好吧,放个传送门...) 然后就是最后把maxsum-minsum就好啦233~ ...
- C++ 播放音频流(PCM裸流)--改善
直接上代码,如果有需要可以直接建一个win32控制台程序然后将代码拷过去改个文件名就可以用了(注意将声道和频率与你自己的文件对应).当然我自己也用VS2008写了个例子上传了,如果有需要下载地址如下: ...
- Dom4J读写xml
解析读取XML public static void main(String[] args) { //1获取SaxReader对象 SAXReader reader=new SAXReader(); ...
- ws2s函数
std::string ws2s(const std::wstring& str) { char* pElementText; int iTextLen; // wide char to mu ...
- Fiddler抓包11-HTTPS证书Actions无法导出问题【转载】
本篇转自博客:上海-悠悠 原文地址:http://www.cnblogs.com/yoyoketang/tag/fiddler/ 前言 在点Actions时候出现Export Failed:The r ...
- python每日一类(3):os和sys
os与sys模块的官方解释如下: os: This module provides a portable way of using operating system dependent functio ...
- hdu 4081(次小生成树)
Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3 ...
- ORA-01157:无法标识/锁定数据文件,ORA-01110:表空间丢失错误
https://blog.csdn.net/u014432433/article/details/51051854
- DotNetCore 笔记
最近公司框架升级到DotNetCore了,但是在用post请求的时候,发现页面的post data后台并没有接收到数据,经过调查发现,netcore用法和之前的MVC5不一样,想要接收post里的da ...