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

作者:daisy 时间:2024-05-05 09:21:19 

前言

Node 给前端开发带来了很大的改变,促进了前端开发的自动化,我们可以简化开发工作,然后利用各种工具包生成生产环境。如运行sass src/sass/main.scss dist/css/main.css即可编译 Sass 文件。

在实际的开发过程中,我们可能会有自己的特定需求,

那么我们得学会如何创建一个Node命令行工具。

hello world

老规矩第一个程序为hello world。在工程中新建bin目录,在该目录下创建名为helper的文件,具体内容如下:


#!/usr/bin/env node

console.log('hello world');

修改helper文件的权限:


$ chmod 755 ./bin/helper

执行helper文件,终端将会显示hello world


$ ./bin/helper
hello world

符号链接

接下来我们创建一个符号链接,在全局的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 world

commander模块

为了更高效的编写命令行工具,我们使用TJ大神的commander模块。


$ npm install --save commander

helper文件内容修改为:


#!/usr/bin/env node

var program = require('commander');

program
.version('1.0.0')
.parse(process.argv);

执行helper -hhelper -V命令:


$ helper -h

Usage: helper [options]

Options:

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

$ helper -V
1.0.0

commander模块提供-h, --help-V, --version两个内置命令。

创建命令

创建一个helper hello <author>的命令,当用户输入helper hello ipluser时,终端显示hello ipluser。修改helper文件内容:


#!/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);

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('commander');

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('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);

执行helper hellohelper hello -h命令,终端将会显示帮助信息:


$ 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

总结

到此我们编写了一个helper命令行工具,并且具有helper hello <author>命令。刚兴趣的朋友们快快自己动手实践起来,只有自己做了才能算真正的学习了,希望本文对大家能有所帮助。

标签:nodejs,命令行工具
0
投稿

猜你喜欢

  • ASP+ajax注册即时提示程序代码

    2011-02-05 11:25:00
  • MySQL 复制详解及简单实例

    2024-01-15 12:50:28
  • 教你一分钟在win10终端成功安装Pytorch的方法步骤

    2023-09-01 19:32:38
  • Python实现的计算器功能示例

    2023-02-16 22:25:30
  • Python简单获取网卡名称及其IP地址的方法【基于psutil模块】

    2022-10-07 19:52:15
  • Python Celery定时任务详细讲解

    2022-08-12 08:25:21
  • Python基于进程池实现多进程过程解析

    2023-05-08 10:52:24
  • sql server Bulk Insert命令详细

    2024-01-13 00:54:04
  • 解决Python3.8用pip安装turtle-0.0.2出现错误问题

    2021-04-07 03:51:20
  • 用 Schema 约束 XML 数据

    2010-08-24 18:21:00
  • Python while、for、生成器、列表推导等语句的执行效率测试

    2021-03-05 02:17:54
  • vue.js刷新当前页面的实例讲解

    2023-04-03 07:39:27
  • Python基础之常用库常用方法整理

    2022-10-30 10:43:26
  • Python Socket编程之多线程聊天室

    2021-03-23 21:01:02
  • Python根据指定日期计算后n天,前n天是哪一天的方法

    2022-12-27 19:13:50
  • fso对象CreateTextFile方法调用时“无效的过程调用或参数”错误

    2009-05-26 15:39:00
  • Python2中的raw_input() 与 input()

    2022-05-16 07:42:06
  • python 抓包保存为pcap文件并解析的实例

    2023-04-03 03:52:04
  • 在Python编程过程中用单元测试法调试代码的介绍

    2023-12-10 02:16:46
  • HTML头部属性全接触

    2007-09-05 19:09:00
  • asp之家 网络编程 m.aspxhome.com