欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Node.js如何開發(fā)命令行工具

 更新時(shí)間:2016年08月14日 16:02:28   投稿:daisy  
追求更高的效率是碼農(nóng)不斷的追求。選擇合適的工具,合理搭配使用,既能提高一部分開發(fā)效率,又能改善寫代碼時(shí)的心情。使用Node.js開發(fā)命令行工具是開發(fā)者應(yīng)該掌握的一項(xiàng)技能,適當(dāng)編寫命令行工具以提高開發(fā)效率。

前言

Node 給前端開發(fā)帶來了很大的改變,促進(jìn)了前端開發(fā)的自動(dòng)化,我們可以簡化開發(fā)工作,然后利用各種工具包生成生產(chǎn)環(huán)境。如運(yùn)行sass src/sass/main.scss dist/css/main.css即可編譯 Sass 文件。

在實(shí)際的開發(fā)過程中,我們可能會(huì)有自己的特定需求,

那么我們得學(xué)會(huì)如何創(chuàng)建一個(gè)Node命令行工具。

hello world

老規(guī)矩第一個(gè)程序?yàn)?code>hello world。在工程中新建bin目錄,在該目錄下創(chuàng)建名為helper的文件,具體內(nèi)容如下:

#!/usr/bin/env node

console.log('hello world');

修改helper文件的權(quán)限:

$ chmod 755 ./bin/helper

執(zhí)行helper文件,終端將會(huì)顯示hello world

$ ./bin/helper
hello world

符號鏈接

接下來我們創(chuàng)建一個(gè)符號鏈接,在全局的node_modules目錄之中,生成一個(gè)符號鏈接,指向模塊的本地目錄,使我們可以直接使用helper命令。

在工程的package.json文件中添加bin字段:

{
 "name": "helper",
 "bin": {
 "helper": "bin/helper"
 }
}

在當(dāng)前工程目錄下執(zhí)行npm link命令,為當(dāng)前模塊創(chuàng)建一個(gè)符號鏈接:

$ npm link

/node_path/bin/helper -> /node_path/lib/node_modules/myModule/bin/helper
/node_path/lib/node_modules/myModule -> /Users/ipluser/myModule

現(xiàn)在我們可以直接使用helper命令:

$ helper
hello world

commander模塊

為了更高效的編寫命令行工具,我們使用TJ大神的commander模塊。

$ npm install --save commander

helper文件內(nèi)容修改為:

#!/usr/bin/env node

var program = require('commander');

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

執(zhí)行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兩個(gè)內(nèi)置命令。

創(chuàng)建命令

創(chuàng)建一個(gè)helper hello <author>的命令,當(dāng)用戶輸入helper hello ipluser時(shí),終端顯示hello ipluser。修改helper文件內(nèi)容:

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

執(zhí)行helper hello命令:

$ helper hello ipluser
hello author

解析輸入信息

我們希望author是由用戶輸入的,終端應(yīng)該顯示為hello ipluser。修改helper-hello文件內(nèi)容,解析用戶輸入信息:

#!/usr/bin/env node

var program = require('commander');

program.parse(process.argv);

const author = program.args[0];

console.log('hello', author);

再執(zhí)行helper hello ipluser命令:

$ helper hello ipluser
hello ipluser

哦耶,終于達(dá)到完成了,但作為程序員,這還遠(yuǎn)遠(yuǎn)不夠。當(dāng)用戶沒有輸入author時(shí),我們希望終端能提醒用戶輸入信息。

提示信息

helper-hello文件中添加提示信息:

#!/usr/bin/env node

var program = require('commander');

program.usage('<author>');

// 用戶輸入`helper hello -h`或`helper hello --helper`時(shí),顯示命令使用例子
program.on('--help', function() {
 console.log(' Examples:');
 console.log(' $ helper hello ipluser');
 console.log();
});

program.parse(process.argv);
(program.args.length < 1) && program.help(); // 用戶沒有輸入信息時(shí),調(diào)用`help`方法顯示幫助信息

const author = program.args[0];

console.log('hello', author);

執(zhí)行helper hellohelper hello -h命令,終端將會(huì)顯示幫助信息:

$ 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

總結(jié)

到此我們編寫了一個(gè)helper命令行工具,并且具有helper hello <author>命令。剛興趣的朋友們快快自己動(dòng)手實(shí)踐起來,只有自己做了才能算真正的學(xué)習(xí)了,希望本文對大家能有所幫助。

相關(guān)文章

最新評論