第一部分可以生成一個自定義命令,例如常見的”express”,yargs和commander則可以在生成的自定義命令上做擴展,yargs將命令擴展成類似express --l xx
的形式;而commander則可以擴展成類似 ‘express install xx’形式,也可以擴展成express -e xx
的形式,前者寫法簡單,后者擴展性更好。
- 生成自定義命令
- yargs
- commander
- 完整例子
生成自定義命令
- 新建文件夾
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
;命令行輸入
hello
可看到效果 ——————123;
yargs
yargs所實現的功能是可以根據用戶命令行輸入選項參數的不同而達到改變node環境變量修改全局變量global,從而實現個性定制的功能
//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
生成自定義命令里將文件的啟動轉換為一個自定義命令,而commander則可以在自定義命令基礎上做命令的擴展(帶參數等);
API
整體代碼自上到下執行,如果沒有碰到parse,則其前面的options不會觸發,所以要注意process.xx
的寫法。- version(‘0.0.1’) 版本號;
- usage(‘zhao’) 名字
- description(‘hello ,I\’m zhao’) 描述
- allowUnknownOption 取消接收到未定義option時報錯的機制,不報錯;
- command(‘ab’) 定義子命令;
- alias(‘a’) 定義子命令的短命令;
- option(‘-p, –peppers’,’Add oeooers’) 自定義選項參數和描述
- action(cb) 回調
- parse(process.argv); 至於末尾,解析命令行輸入的命令;
總結
我在command,action和option,program.xx這兩個組合里繞了很多彎路,總結如下
#!/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);
}- 保持兩個組合各自獨立,command/action組合,option/program.xx組合,
命令行中不能同時出現command子命令和option選項,即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;
- 保持兩個組合各自獨立,command/action組合,option/program.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);