commander.js
參考鏈接:
http://yijiebuyi.com/blog/2cd3833e8551a302b4ec645031bfd3d0.html
http://blog.gejiawen.com/2016/09/21/make-a-node-cli-program-by-commander-js/
https://github.com/tj/commander.js/blob/master/Readme_zh-CN.md
?
.option('-l, --langu', 'website langueage')
-l如果是命令的簡(jiǎn)寫,必須是一個(gè)字母,多了也會(huì)報(bào)錯(cuò)。
?
program .version('0.1.0') .option('-p, --peppers', 'Add peppers') .option('-P, --pineapple', 'Add pineapple') // 這個(gè)option里面參數(shù) // 命令簡(jiǎn)寫,命令全拼,命令描述 .option('-b, --bbq-sauce', 'Add bbq sauce') //??[type] 是option的第三個(gè)參數(shù)形參,option的第三個(gè)參數(shù)是實(shí)參 .option('-c, --cheese [type]', 'Add the specified type of cheese [marble]', 'marble') .parse(process.argv);console.log('you ordered a pizza with:'); if (program.peppers) console.log(' - peppers'); if (program.pineapple) console.log(' - pineapple'); if (program.bbqSauce) console.log(' - bbq'); console.log(' - %s cheese', program.cheese); 并且:
bosszhang@bogon bin $ node macaw.js c
program.cheese: marble
you ordered a pizza with:
- marble cheese
bosszhang@bogon bin $ node macaw.js -c
you ordered a pizza with:
- marble cheese
bosszhang@bogon bin $ node macaw.js cheese
you ordered a pizza with:
- marble cheese
bosszhang@bogon bin $ node macaw.js --cheese
you ordered a pizza with:
- marble cheese
這幾個(gè)的效果一樣。
?
var program = require('commander');program .version('0.1.0') .option('-p, --peppers', 'Add peppers') .option('-P, --pineapple', 'Add pineapple') .option('-b, --bbq-sauce', 'Add bbq sauce') .option('-n, --no-bbq-sauce', 'remove bbq sauce') .option('-c, --cheese [type]', 'Add the specified type of cheese [marble]', 'marble') .parse(process.argv);
console.log('you ordered a pizza with:'); if (program.peppers) console.log(' - peppers'); else console.log(' without peppers'); if (program.pineapple) console.log(' - pineapple'); else console.log(' without pineapple'); if (program.bbqSauce) console.log(' - bbq'); else console.log(' without bbq'); console.log(' - %s cheese', program.cheese);
bosszhang@bogon bin $ node macaw.js --no-bbq-sauce
you ordered a pizza with:
without peppers
without pineapple
without bbq
- marble cheese
bosszhang@bogon bin $ node macaw.js --bbq-sauce
you ordered a pizza with:
without peppers
without pineapple
without bbq
- marble cheese
說(shuō)明只要加上
.option('-n, --no-bbq-sauce', 'remove bbq sauce')
這條命令不管是
no-bbq-sauce 命令,還是no-bbq-sauce命令,都只會(huì)執(zhí)行
else console.log(' without bbq');
這個(gè)語(yǔ)句。
?
比如說(shuō):
var program = require('commander');program .option('--no-sauce', 'Remove sauce') .parse(process.argv);
console.log('you ordered a pizza'); if (program.sauce) console.log(' with sauce'); else console.log(' without sauce');
?
bosszhang@bogon bin $ node macaw.js
you ordered a pizza
with sauce
bosszhang@bogon bin $ node macaw.js no-sauce
you ordered a pizza
with sauce
bosszhang@bogon bin $ node macaw.js --no-sauce
you ordered a pizza
without sauce
bosszhang@bogon bin $ node macaw.js s
you ordered a pizza
with sauce
說(shuō)明:
不識(shí)別的命令執(zhí)行的也是
console.log(' without sauce');
?
/*****************************************/
?
var program = require('commander');program .option('--sauce', 'Remove sauce') .option('--no-sauce', 'Remove sauce') .parse(process.argv);
console.log('you ordered a pizza'); if (program.sauce) console.log(' with sauce'); else console.log(' without sauce');
bosszhang@bogon bin $ node macaw.js --sauce
you ordered a pizza
without sauce
bosszhang@bogon bin $ node macaw.js sauce
you ordered a pizza
with sauce
bosszhang@bogon bin $ node macaw.js no-sauce
you ordered a pizza
with sauce
說(shuō)明:
同時(shí)出現(xiàn),跟順序有關(guān),只會(huì)執(zhí)行后出現(xiàn)的那個(gè)命令。
--sauce 和?--no-sauce
命令
--sauce 將不起作用。
即使執(zhí)行也是執(zhí)行--no-sauce
?
/*****************************************/
?
var program = require('commander');program .command('rm <dir>') .option('-r, --recursive', 'Remove recursively') .action(function (dir, cmd) { console.log('remove ' + dir + (cmd.recursive ? ' recursively' : '')) })
program.parse(process.argv)
bosszhang@bogon bin $ node macaw.js rm dir
remove dir
bosszhang@bogon bin $ node macaw.js rm dir --recursive
remove dir recursively
說(shuō)明:
command可以接受輸入命令,反正都是process.arg數(shù)組的process.arg[2],以后的值
process.argv: [ '/Users/bosszhang/.nvm/versions/node/v6.2.0/bin/node',
'/Users/bosszhang/Documents/workspace_temp/test-cli/bin/macaw.js',
'rm',
'dir',
'--recursive' ]
當(dāng)一個(gè)Nodejs程序運(yùn)行時(shí),會(huì)有許多存在內(nèi)存中的全局變量,其中有一個(gè)叫做process,意為進(jìn)程對(duì)象。process對(duì)象中有一個(gè)叫做argv的屬性。命令行程序的第一個(gè)重頭戲就是解析這個(gè)process.argv屬性。
看起來(lái)process.argv好像是一個(gè)數(shù)組,其中第一個(gè)元素是node的執(zhí)行路徑,第二個(gè)元素是當(dāng)前執(zhí)行文件的路徑,從第三個(gè)元素開始,是執(zhí)行時(shí)帶入的參數(shù)。
?option
用戶:.option('-n, --name <name>', 'your name', 'GK')
- 第一個(gè)參數(shù)是選項(xiàng)定義,分為短定義和長(zhǎng)定義。用
|,,,連接。- 參數(shù)可以用
<>或者[]修飾,前者意為必須參數(shù),后者意為可選參數(shù)。
- 參數(shù)可以用
- 第二個(gè)參數(shù)為選項(xiàng)描述
- 第三個(gè)參數(shù)為選項(xiàng)參數(shù)默認(rèn)值,可選。
?
?
commander.version 可以指定當(dāng)前應(yīng)用程序的一個(gè)版本號(hào).
usage ?指定使用方法,這里讓我們輸入一直值.
option 指定參數(shù),格式,及參數(shù)說(shuō)明.
?
/*****************************************/
?
const program = require('commander') program.usage('<project-name>').parse(process.argv) console.log('process.argv: ',process.argv) console.log('program.args: ',program.args) // 根據(jù)輸入,獲取項(xiàng)目名稱 let projectName = program.args[0] if (!projectName) { // project-name 必填 // 相當(dāng)于執(zhí)行命令的--help選項(xiàng),顯示help信息,這是commander內(nèi)置的一個(gè)命令選項(xiàng) program.help() return } go() function go () { // 預(yù)留,處理子命令 }bosszhang@bogon test-cli $ node ./bin/macaw-init.js aaa
process.argv: [ '/usr/local/bin/node',
'/Users/bosszhang/Documents/workspace_temp/test-cli/bin/macaw-init.js',
'aaa' ]
program.args: [ 'aaa' ]
說(shuō)明:
program.usage('<project-name>').parse(process.argv) 會(huì)獲取process.argv[2]參數(shù)。
?
macaw.js 和 macaw-init.js 和 macaw-hello.js是有關(guān)聯(lián)的
const program = require('commander') // npm i commander -D program.version('1.0.0') .usage('<command> [項(xiàng)目名稱]') .command('zzz', 'description') // 什么嗎?必須是hello,第二個(gè)參數(shù)是描述? .parse(process.argv) 當(dāng)輸入上面的命令的時(shí)候bosszhang@bogon test-cli $ node ./bin/macaw.js zzz
macaw-zzz(1) does not exist, try --help
提示說(shuō)不存在,說(shuō)明
輸入什么就會(huì)執(zhí)行相應(yīng)的文件
node ./bin/macaw.js init就執(zhí)行macaw-init.js
node ./bin/macaw.js hello就執(zhí)行macaw-hello.js
說(shuō)明:
.command('hello', 'description') 這是一條神奇的命令 它會(huì)執(zhí)行當(dāng)前文件夾-process.argv[2].js的文件?
這條命令
program.usage('<project-name>').parse(process.argv) 可以接受參數(shù) 其中:process.argv: [ '/usr/local/bin/node',
'/Users/bosszhang/Documents/workspace_temp/test-cli/bin/macaw-init.js',
'aa' ]
使用完
program.usage('<project-name>').parse(process.argv)再使用
let projectName = program.args[0] 會(huì)得到program.args: [ 'aa' ]
如果在那條命令之前,則:
program.args: undefined
?
轉(zhuǎn)載于:https://www.cnblogs.com/zhangzs000/p/9301373.html
總結(jié)
以上是生活随笔為你收集整理的commander.js的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: i3多少钱啊?
- 下一篇: xming Linux图形界面至Win