第一部分能夠生成一個自定義命令,例如常見的」express」,yargs和commander則能夠在生成的自定義命令上作擴展,yargs將命令擴展成相似express --l xx
的形式;而commander則能夠擴展成相似 ‘express install xx’形式,也能夠擴展成express -e xx
的形式,前者寫法簡單,後者擴展性更好。javascript
test
,並進入;npm init
生成package.json
文件;hello js
,內容以下:#! /usr/bin/env node
'use strict';
console.log('123');
package.json
裏添加內容"bin": {"hello": "hello.js"}
://package.json
{
"name": "hello",
"version": "1.0.0",
"description": "",
"main": "index.js",
"bin": {"hello": "hello.js"},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
執行命令npm link
;java
命令行輸入hello
可看到效果 ——————123;node
yargs所實現的功能是能夠根據用戶命令行輸入選項參數的不一樣而達到改變node環境變量修改全局變量global,從而實現個性定製的功能express
//index.js
const argv = require('yargs').argv;
if (argv.l == 'zh-cn') {
console.log('Chinese site!')
} else if (argv.l == 'en') {
console.log('English site!')
}
//命令行
node yangs.js --l=zh-cn //Chinese site!
node yangs.js --l=en //English site!
生成自定義命令裏將文件的啓動轉換爲一個自定義命令,而commander則能夠在自定義命令基礎上作命令的擴展(帶參數等);npm
API
總體代碼自上到下執行,若是沒有碰到parse,則其前面的options不會觸發,因此要注意process.xx
的寫法。json
總結
我在command,action和option,program.xx這兩個組合裏繞了不少彎路,總結以下ui
#!/usr/bin/env node
'use strict';
const program = require('commander');
program
.version('0.0.1')
.usage('例子')
.description('this is a lizi of commander')
program
.command('hello [st]')
.action(function(st,value){
hello(st,value);
})
function hello(val,o){
console.log(val);
console.log(1);
console.log(o)
}
program
.option('-s --save [value]','保存')
program.parse(process.argv);
if (program.save){
console.log(program.save);
}
lizi hello 23 -s 25
是錯誤的;lizi -s 23
在命令行輸出true而非值的時候,是沒有在長選項後跟上[xx]
;program.s
可能會有三種狀況出現,一是當輸入其餘可選項的時候,當嘗試lizi -t (xx)
時,program.s
爲undefined
;二是當輸入lizi -s
時,program.s
爲默認值,不設置默認值則爲true;三是當輸入lizi -s xx
時,program.s
爲xx;//package.json
{
"name": "lizi",
"version": "1.0.0",
"description": "",
"main": "index.js",
"bin": {
"lizi1":"lizi1.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"commander": "^2.9.0"
}
}
//lizi1.js
#!/usr/bin/env node
'use strict';
console.log(1);
const argv = require('yargs').argv;
if (argv.l == 'zh-cn') {
console.log('Chinese site!')
} else if (argv.l == 'en') {
console.log('English site!')
}
例子2: commander和生成自定義命令的組合命令行
//package.json
{
"name": "lizi",
"version": "1.0.0",
"description": "",
"main": "index.js",
"bin": {
"lizi":"lizi.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"commander": "^2.9.0"
}
}
//lizi.js
#!/usr/bin/env node
'use strict';
const program = require('commander');
program
.version('0.0.1')
.usage('例子')
.description('this is a lizi of commander')
program
.command('hello [st]')
.action(function(st,value){
hello(st,value);
})
function hello(val,o){
console.log(val);
console.log(1);
console.log(o)
}
program
.option('-f --flag [value]','保存','ha')
.option('-t --tale [value]','保存')
program.parse(process.argv);
if (program.flag){
global.flag = program.flag;
}
console.log(global.flag);