詳解Node.js如何開發(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 -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
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 hello
或helper 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)文章
基于NodeJS的前后端分離的思考與實(shí)踐(六)Nginx + Node.js + Java 的軟件棧部署實(shí)踐
關(guān)于前后端分享的思考,我們已經(jīng)有五篇文章闡述思路與設(shè)計(jì)。本文介紹淘寶網(wǎng)收藏夾將 Node.js 引入傳統(tǒng)技術(shù)棧的具體實(shí)踐。2014-09-09node.js利用socket.io實(shí)現(xiàn)多人在線匹配聯(lián)機(jī)五子棋
這篇文章主要介紹了node.js利用socket.io實(shí)現(xiàn)多人在線匹配聯(lián)機(jī)五子棋的操作方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-05-05nodejs版本過高導(dǎo)致vue2版本的項(xiàng)目無法正常啟動(dòng)的解決方案
這篇文章主要給大家介紹了關(guān)于nodejs版本過高導(dǎo)致vue2版本的項(xiàng)目無法正常啟動(dòng)的解決方案,本文小編給大家詳細(xì)介紹了如何解決這個(gè)問題,如有遇到同樣問題的朋友可以參考下2023-11-11Nodejs中使用phantom將html轉(zhuǎn)為pdf或圖片格式的方法
這篇文章主要介紹了Nodejs中使用phantom將html轉(zhuǎn)為pdf或圖片格式的方法,需要的朋友可以參考下2017-09-09Node.js模擬發(fā)起http請求從異步轉(zhuǎn)同步的5種用法
這篇文章主要介紹了Node.js模擬發(fā)起http請求從異步轉(zhuǎn)同步的5種方法,下面總結(jié)了幾個(gè)常見的庫 API 從異步轉(zhuǎn)同步的幾種方法。需要的朋友可以參考下2018-09-09nodejs獲取表單數(shù)據(jù)的三種方法實(shí)例
在開發(fā)中經(jīng)常需要獲取form表單的數(shù)據(jù),這篇文章主要給大家介紹了關(guān)于nodejs獲取表單數(shù)據(jù)的三種方法,方法分別是form表單傳遞、ajax請求傳遞以及表單序列化,需要的朋友可以參考下2021-06-06