国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

詳解Node.js如何開發命令行工具

2019-11-20 09:14:58
字體:
來源:轉載
供稿:網友

前言

Node 給前端開發帶來了很大的改變,促進了前端開發的自動化,我們可以簡化開發工作,然后利用各種工具包生成生產環境。如運行sass src/sass/main.scss dist/css/main.css即可編譯 Sass 文件。

在實際的開發過程中,我們可能會有自己的特定需求,

那么我們得學會如何創建一個Node命令行工具。

hello world

老規矩第一個程序為hello world。在工程中新建bin目錄,在該目錄下創建名為helper的文件,具體內容如下:

#!/usr/bin/env nodeconsole.log('hello world');

修改helper文件的權限:

$ chmod 755 ./bin/helper

執行helper文件,終端將會顯示hello world

$ ./bin/helperhello 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命令:

$ helperhello world

commander模塊

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

$ npm install --save commander

helper文件內容修改為:

#!/usr/bin/env nodevar 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 -V1.0.0

commander模塊提供-h, --help-V, --version兩個內置命令。

創建命令

創建一個helper hello <author>的命令,當用戶輸入helper hello ipluser時,終端顯示hello ipluser。修改helper文件內容:

#!/usr/bin/env nodevar 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 nodeconsole.log('hello author');

執行helper hello命令:

$ helper hello ipluserhello author

解析輸入信息

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

#!/usr/bin/env nodevar program = require('commander');program.parse(process.argv);const author = program.args[0];console.log('hello', author);

再執行helper hello ipluser命令:

$ helper hello ipluserhello ipluser

哦耶,終于達到完成了,但作為程序員,這還遠遠不夠。當用戶沒有輸入author時,我們希望終端能提醒用戶輸入信息。

提示信息

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

#!/usr/bin/env nodevar 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>命令。剛興趣的朋友們快快自己動手實踐起來,只有自己做了才能算真正的學習了,希望本文對大家能有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鄂托克前旗| 瑞丽市| 尼木县| 蕲春县| 葫芦岛市| 阿克陶县| 友谊县| 沙河市| 陇南市| 通江县| 麻城市| 延庆县| 宜春市| 津南区| 广州市| 金川县| 武隆县| 花莲市| 卢龙县| 文水县| 福清市| 定远县| 慈利县| 东乌珠穆沁旗| 壶关县| 枞阳县| 德阳市| 许昌县| 新干县| 莱阳市| 库尔勒市| 乌拉特后旗| 台南县| 红桥区| 铜鼓县| 炎陵县| 布拖县| 江西省| 淅川县| 长宁县| 龙里县|