源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

详解Node.js如何开发命令行工具

  • 时间:2022-04-22 01:45 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:详解Node.js如何开发命令行工具
[b]前言[/b] Node 给前端开发带来了很大的改变,促进了前端开发的自动化,我们可以简化开发工作,然后利用各种工具包生成生产环境。如运行[code]sass src/sass/main.scss dist/css/main.css[/code]即可编译 Sass 文件。 在实际的开发过程中,我们可能会有自己的特定需求, 那么我们得学会如何创建一个Node命令行工具。 [b]hello world [/b] 老规矩第一个程序为[code]hello world[/code]。在工程中新建[b]bin[/b]目录,在该目录下创建名为[b]helper[/b]的文件,具体内容如下:
#!/usr/bin/env node

console.log('hello world');
修改helper文件的权限:
$ chmod 755 ./bin/helper
执行[b]helper[/b]文件,终端将会显示[code]hello world[/code]:
$ ./bin/helper
hello world
[b]符号链接[/b] 接下来我们创建一个符号链接,在全局的[b]node_modules[/b]目录之中,生成一个符号链接,指向模块的本地目录,使我们可以直接使用[code]helper[/code]命令。 在工程的[b]package.json[/b]文件中添加[b]bin[/b]字段:
{
 "name": "helper",
 "bin": {
 "helper": "bin/helper"
 }
}
在当前工程目录下执行[code]npm link[/code]命令,为当前模块创建一个符号链接:
$ npm link

/node_path/bin/helper -> /node_path/lib/node_modules/myModule/bin/helper
/node_path/lib/node_modules/myModule -> /Users/ipluser/myModule
现在我们可以直接使用[code]helper[/code]命令:
$ helper
hello world
[b]commander模块[/b] 为了更高效的编写命令行工具,我们使用TJ大神的[b]commander[/b]模块。
$ npm install --save commander
[b]helper[/b]文件内容修改为:
#!/usr/bin/env node

var program = require('commander');

program
 .version('1.0.0')
 .parse(process.argv);
执行[code]helper -h[/code]和[code]helper -V[/code]命令:
$ helper -h

 Usage: helper [options]

 Options:

 -h, --help  output usage information
 -V, --version output the version number

$ helper -V
1.0.0
[b]commander[/b]模块提供[code]-h[/code],[code] --help[/code]和[code]-V[/code], [code]--version[/code]两个内置命令。 [b]创建命令[/b] 创建一个[code]helper hello <author>[/code]的命令,当用户输入[code]helper hello ipluser[/code]时,终端显示[code]hello ipluser[/code]。修改[b]helper[/b]文件内容:
#!/usr/bin/env node

var program = require('commander');

program
 .version('1.0.0')
 .usage('<command> [options]')
 .command('hello', 'hello the author') // 添加hello命令
 .parse(process.argv);
在[b]bin[/b]目录下新建[b]helper-hello[/b]文件:
#!/usr/bin/env node

console.log('hello author');
执行[code]helper hello[/code]命令:
$ helper hello ipluser
hello author
[b]解析输入信息[/b] 我们希望[b]author[/b]是由用户输入的,终端应该显示为[code]hello ipluser[/code]。修改[code]helper-hello[/code]文件内容,解析用户输入信息:
#!/usr/bin/env node

var program = require('commander');

program.parse(process.argv);

const author = program.args[0];

console.log('hello', author);
再执行[code]helper hello ipluser[/code]命令:
$ helper hello ipluser
hello ipluser
哦耶,终于达到完成了,但作为程序员,这还远远不够。当用户没有输入[b]author[/b]时,我们希望终端能提醒用户输入信息。 提示信息 在[b]helper-hello[/b]文件中添加提示信息:
#!/usr/bin/env node

var program = require('commander');

program.usage('<author>');

// 用户输入`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);
执行[code]helper hello[/code]或[code]helper hello -h[/code]命令,终端将会显示帮助信息:
$ helper hello

 Usage: helper-hello <author>

 Options:

 -h, --help output usage information

 Examples:
 $ helper hello ipluser

$ helper hello -h

 Usage: helper-hello <author>

 Options:

 -h, --help output usage information

 Examples:
 $ helper hello ipluser
[b]总结[/b] 到此我们编写了一个helper命令行工具,并且具有helper hello <author>命令。刚兴趣的朋友们快快自己动手实践起来,只有自己做了才能算真正的学习了,希望本文对大家能有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部