备注:
  比较简单就是使用nodejs 的两个类库帮助我们进行开发而已,具体的使用参考类库文档
1. 项目初始化
a. 安装依赖
yarn init -y
yarn add commander inquirer ├── README.md
├── bin
│ └── index.js
├── package.json
└── yarn.lock
2. 项目代码
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);
3. 使用
a. 安装
npm install -g mynode-cli
b. 使用
mynode-cli -i init
mynode-cli -i login
mynode-cli -h
参考界面如下:
 
 
 
4. 总结
还是比较简单的,同时有一个npm 包create-new-cli也是一个不错的选择
5.参考资料
https://www.npmjs.com/package/commander
https://www.npmjs.com/package/inquirer
https://github.com/rongfengliang/mynode-cli
 
 
 
 

使用 commander && inquirer 构建专业的node cli的更多相关文章

  1. Node & CLI

    Node & CLI cli 生成文件的原理是什么 https://nodejs.org/api/cli.html http://nodejs.cn/api/cli.html CLI & ...

  2. 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 ...

  3. node cli & emoji

    node cli & emoji cli $ yarn add node-emoji $ npm i node-emoji https://github.com/omnidan/node-em ...

  4. 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 ...

  5. linux & node & cli & exit(0) & exit(1)

    linux & node & cli & exit(0) & exit(1) exit(0) & exit(1) demo exit(0) === OK exi ...

  6. 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 ...

  7. commander.js 制作简易的 MINA CLI 脚手架

    出发点并不是小程序本身,是想要做一个脚手架(command-line interface),看过 VUE / REACT 脚手架,觉得很厉害,但是并不太知道里面是怎么做成的,所以最近研究了研究,看能不 ...

  8. 用commander.js构建自己的脚手架工具

    随着前端技术的发展,工程化逐渐成为了一种趋势.但在实际开发时,搭建项目是一件很繁琐的事情,尤其是在对一个框架的用法还不熟悉的时候.于是很多框架都自带一套脚手架工具,在初始化前端项目的时候就可以不用自己 ...

  9. 使用distillery 构建专业的 phoenix 项目软件包

    备注:     首先需要安装 elixir 环境   1. 基本项目说明 参考项目: https://github.com/rongfengliang/phoenix-rest-demo 2. 项目说 ...

随机推荐

  1. SSM 框架搭建 idea环境

    参考: https://www.cnblogs.com/toutou/p/ssm_springmvc.html https://www.cnblogs.com/toutou/p/ssm_springm ...

  2. 【BZOJ1061】【NOI2008】志愿者招募

    [BZOJ1061][NOI2008]志愿者招募 题面 BZOJ 题解 我们设每类志愿者分别招募了\(B[i]\)个 那么,我们可以得到一系列的方程 \[\sum_{S[i]\leq x\leq T[ ...

  3. 【BZOJ4819】新生舞会(分数规划,网络流)

    [BZOJ4819]新生舞会(分数规划,网络流) 题面 BZOJ Description 学校组织了一次新生舞会,Cathy作为经验丰富的老学姐,负责为同学们安排舞伴.有n个男生和n个女生参加舞会 买 ...

  4. CodeForces - 91B单调队列

    有一个数列,对于每一个数,求比它小的在他右边距离他最远的那个数和他的距离 用单调队列做,维护单调队列时可采用如下方法,对于每一个数,如果队列中没有数,则加入队列,如果队列头的数比当前数大,则舍弃该数 ...

  5. uva 12086 线段树or树状数组练习

    题目链接   https://vjudge.net/problem/34215/origin 这个题就是线段树裸题,有两种操作,实现单点更新和区间和的查找即可,这里第一次学习使用树状数组完成. 二者相 ...

  6. C# - Generics泛型,一图话c#泛型

    一.一篇好文 https://www.cnblogs.com/yueyue184/p/5032156.html 二.一幅好图

  7. logback配置日志输出

    <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> & ...

  8. 跨域问题Java方式解决及Nginx方式解决【亲测可行】

    这两天和前端同事调试微信公众号项目,就遇到了跨域问题:网上相关博客也挺多的,但有很多细节没有点到,在此呢我也再次记录一下解决方式: (算是踩坑日记吧~ ~ ~)   !问题发现: 页面加载不出来,控制 ...

  9. Markdown_00_资源帖

    一.官方资料 1.原始官方文档 daringfireball.net 2.官方文档中文版 Markdown 语法说明 (简体中文版) Markdown: Basics (快速入门) 3.Typora官 ...

  10. DRF中序列化器定义及使用

    首先需要明白序列化和反序列化的定义及作用: 序列化是将程序语言转换为JSON/XML; 反序列化是将JSON/XML转换为程序语言; 对应到Django中,序列化即把模型对象转换为字典形式, 在返回给 ...