详解Node.js怎么开发命令行工具?
前言
Node 给前端开发带来了很大的改变,促进了前端开发的自动化,我们可以简化开发工作,然后利用各种工具包生成生产环境。如运行sass src/sass/main.scss dist/css/main.css即可编译 Sass 文件。
在实际的开发过程中,我们可能会有自己的特定需求,
那么我们得学会如何创建一个Node命令行工具。
hello orld
老规矩第一个程序为hello orld。在工程中新建bin目录,在该目录下创建名为helper的文件,具体内容如下
#!/usr/bin/env node
console.log('hello orld');
修改helper文件的权限
$ chmod 755 ./bin/helper
执行helper文件,终端将会显示hello orld
$ ./bin/helper hello orld
符号链接
接下来我们创建一个符号链接,在全局的node_modules目录之中,生成一个符号链接,指向模块的本地目录,使我们可以直接使用helper命令。
在工程的package.json文件中添加bin字段
{
"name": "helper",
"bin": {
"helper": "bin/helper"
}
}
在当前工程目录下执行npm link命令,为当前模块创建一个符号链接
$ npm link /node_path/bin/helper -> /node_path/lib/node_modules/myModule/bin/helper /node_path/lib/node_modules/myModule -> /Users/ipluser/myModule
现在我们可以直接使用helper命令
$ helper hello orld
mander模块
为了更高效的编写命令行工具,我们使用TJ大神的mander模块。
$ npm install --save mander
helper文件内容修改为
#!/usr/bin/env node
var program = require('mander');
program
.version('1.0.0')
.parse(process.argv);
执行helper -h和helper -V命令
$ helper -h Usage: helper [options] Options: -h, --help output usage information -V, --version output the version number $ helper -V 1.0.0
mander模块提供-h, --help和-V, --version两个内置命令。
创建命令
创建一个helper hello 的命令,当用户输入helper hello ipluser时,终端显示hello ipluser。修改helper文件内容
#!/usr/bin/env node
var program = require('mander');
program
.version('1.0.0')
.usage(' [options]')
.mand('hello', 'hello the author') // 添加hello命令
.parse(process.argv);
在bin目录下新建helper-hello文件
#!/usr/bin/env node
console.log('hello author');
执行helper hello命令
$ helper hello ipluser hello author
解析输入信息
我们电脑维修网希望author是由用户输入的,终端应该显示为hello ipluser。修改helper-hello文件内容,解析用户输入信息
#!/usr/bin/env node
var program = require('mander');
program.parse(process.argv);
const author = program.args[0];
console.log('hello', author);
再执行helper hello ipluser命令
$ helper hello ipluser hello ipluser
哦耶,终于达到完成了,但作为程序员,这还远远不够。当用户没有输入author时,我们电脑维修网希望终端能提醒用户输入信息。
提示信息
在helper-hello文件中添加提示信息
#!/usr/bin/env node
var program = require('mander');
program.usage('');
// 用户输入`helper hello -h`或`helper hello --helper`时,显示命令使用例子
program.on('--help', function() {
console.log(' Examples:');
console.log(' $ helper hello ipluser');
console.log();
});
program.parse(process.argv);
(program.args.length < 1) && program.help(); // 用户没有输入信息时,调用`help`方法显示帮助信息
const author = program.args[0];
console.log('hello', author);
执行helper hello或helper hello -h命令,终端将会显示帮助信息
$ helper hello Usage: helper-hello Options: -h, --help output usage information Examples: $ helper hello ipluser $ helper hello -h Usage: helper-hello Options: -h, --help output usage information Examples: $ helper hello ipluser
到此我们编写了一个helper命令行工具,并且具有helper hello 命令。刚兴趣的朋友们快快自己动手实践起来,只有自己做了才能算真正的学习了,电脑维修网希望本文对大家能有所帮助。
空调维修
- 我的世界电脑版运行身份怎么弄出来(我的世界
- 空调抽湿是什么意思,设置抽湿的温度有什么意
- 方太燃气灶有一个打不着火 怎么修复与排查方法
- 夏季免费清洗汽车空调的宣传口号
- 清洗完空调后出现漏水现象
- iphone6能玩什么游戏(iphone6游戏)
- 如何设置电脑密码锁屏(如何设置电脑密码锁屏
- win10删除开机密码提示不符合密码策略要求
- 电脑w7显示不是正版(w7不是正版怎么解决)
- 万家乐z8热水器显示e7解决 怎么修复与排查方法
- 1匹空调多少瓦数(1匹空调多少瓦)
- 安卓手机连接电脑用什么软件好(关于安卓手机
- 电脑网页看视频卡是什么原因(爱拍看视频卡)
- 华帝燃气灶点火器一直响然后熄火怎么办:问题
- 电脑壁纸怎么换(关于电脑壁纸怎么换的介绍)
- 冬天空调的出风口应该朝什么方向(冬天空调风