使用 commander && inquirer 构建专业的node cli
a. 安装依赖
yarn init -y
yarn add commander inquirer
├── README.md
├── bin
│ └── index.js
├── package.json
└── yarn.lock
a. package.json
{
"name": "mynode-cli",
"version": "1.0.2",
"main": "bin/index.js",
"license": "MIT",
"dependencies": {
"commander": "^2.12.2",
"inquirer": "^4.0.2"
},
"bin": {
"mynode-cli":"bin/index.js"
},
"scripts": {
"publish":"npm publish",
"run-local":"node bin/index"
}
}
b. bin/index.js
备注:主要操作
#!/usr/bin/env node
const Program = require("commander");
const Prompt = require("inquirer");
const initQuestions = [{
type: 'list',
name: 'plattype',
message: '请选择平台类型?',
choices: [
'pass',
'sass',
'iaas'
]
},
{
type: 'list',
name: 'vmCounts',
message: '请选择您包含的虚拟机数量?',
choices: ['100', '200', '500', '1000']
}
];
const loginQuestions = [{
type: 'input',
name: 'username',
message: '请输入用户名',
},
{
type: 'password',
name: 'password',
message: '请输入用户密码'
}
];
Program
.version("0.1.0")
.description("系统平台初始化工具")
.option("-i, --init","平台初始化")
Program
.command("init")
.alias("i")
.description("初始化平台")
.action(() => {
Prompt.prompt(initQuestions).then(result => {
console.log("您选择的平台类型信息如下:");
console.log(JSON.stringify(result));
})
});
Program
.command("login")
.alias("l")
.description("登陆平台")
.action(() => {
Prompt.prompt(loginQuestions).then(result => {
console.log("您登陆的账户信息如下:");
console.log(JSON.stringify(result));
})
})
Program.parse(process.argv);
a. 安装
npm install -g mynode-cli
b. 使用
mynode-cli -i init
mynode-cli -i login
mynode-cli -h
参考界面如下:



还是比较简单的,同时有一个npm 包create-new-cli也是一个不错的选择
https://www.npmjs.com/package/commander
https://www.npmjs.com/package/inquirer
https://github.com/rongfengliang/mynode-cli
使用 commander && inquirer 构建专业的node cli的更多相关文章
- Node & CLI
Node & CLI cli 生成文件的原理是什么 https://nodejs.org/api/cli.html http://nodejs.cn/api/cli.html CLI & ...
- how to read the system information by using the node cli tool?
how to read the system information by using the node cli tool? node cli & get system info demos ...
- node cli & emoji
node cli & emoji cli $ yarn add node-emoji $ npm i node-emoji https://github.com/omnidan/node-em ...
- write a node cli tools, step by step
write a node cli tools, step by step how to write a node cli tools node cli tools, step by step, nod ...
- linux & node & cli & exit(0) & exit(1)
linux & node & cli & exit(0) & exit(1) exit(0) & exit(1) demo exit(0) === OK exi ...
- how to write a node cli tool
how to write a node cli tool https://www.google.com/search?q=how+to+node+cli+tool&oq=how+to+node ...
- commander.js 制作简易的 MINA CLI 脚手架
出发点并不是小程序本身,是想要做一个脚手架(command-line interface),看过 VUE / REACT 脚手架,觉得很厉害,但是并不太知道里面是怎么做成的,所以最近研究了研究,看能不 ...
- 用commander.js构建自己的脚手架工具
随着前端技术的发展,工程化逐渐成为了一种趋势.但在实际开发时,搭建项目是一件很繁琐的事情,尤其是在对一个框架的用法还不熟悉的时候.于是很多框架都自带一套脚手架工具,在初始化前端项目的时候就可以不用自己 ...
- 使用distillery 构建专业的 phoenix 项目软件包
备注: 首先需要安装 elixir 环境 1. 基本项目说明 参考项目: https://github.com/rongfengliang/phoenix-rest-demo 2. 项目说 ...
随机推荐
- 配置Eclipse可以查看JDK类库源码
一.配置方法 配置Eclipse可以查看JDK类库源码 Window->Preferences->Java->Installed JREs 若没有JRE,需要自己添加进来,有的话,点 ...
- 如何高效的使用 Git
-- 代码昨天还是运行好好的今天就不行了. 代码被删了. 突然出现了一个奇怪的 bug,但是没人知道怎么回事. 如果你出现过上面的任何一种情况,那本篇文章就是为你准备的. 除了知道 git add, ...
- python 数组中如何根据值,获取索引,如何根据索引删除值 , 以及如何根据值删除值
假设有一数组 s = [1,2,3,4,5,6,7,8,9] (1)如何根据值获取索引 ,如果值为5 , 那对应的索引为? (2)如何根据索引删除值 , 删除数组中索引5对应的值: (3)根据数组中的 ...
- HDU5521-最短路-建图
Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- CodeForces 297C Splitting the Uniqueness (脑补构造题)
题意 Split a unique array into two almost unique arrays. unique arrays指数组各个数均不相同,almost unique arrays指 ...
- 暗网 tor溯源困难根因——用户的请求会在分布全球的主机随机跳转三次,最终才到达服务器,这就造成了溯源的极其困难
Tor(The Onion Router)可以说是目前最为流行的网络匿名访问技术,用户的请求会在分布全球的主机随机跳转三次,最终才到达服务器,这就造成了溯源的极其困难,从而使得所有的访问者完全没有身份 ...
- hdu4685
题解: 二分图匹配 对于每一个单身狗 见一个虚拟的人 然后就可以做了 代码: #include<cstdio> #include<cstring> #include<al ...
- LeetCode OJ:Remove Duplicates from Sorted List II(链表去重II)
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- 什么是web?什么是web服务器?什么是应用服务器?
1.什么是Web? 简单来说,Web就是在Http协议基础之上,利用浏览器进行访问的网站.目前来看最常用的意义是指在 Intenet 上和 HTML 相关的部分.换句话说,目前在 Intenet 上通 ...
- Spring整合Hibernate:1、annotation方式管理SessionFactory
1.在applicationContext.xml文件中初始化SessionFactory(annotation方式) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 ...