参考链接:
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如果是命令的简写,必须是一个字母,多了也会报错。
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
这几个的效果一样。
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
说明只要加上
.option('-n, --no-bbq-sauce', 'remove bbq sauce')
这条命令不管是
no-bbq-sauce 命令,还是no-bbq-sauce命令,都只会执行
else console.log(' without bbq');
这个语句。
比如说:
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
说明:
不识别的命令执行的也是
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
说明:
同时出现,跟顺序有关,只会执行后出现的那个命令。
--sauce 和 --no-sauce
命令
--sauce 将不起作用。
即使执行也是执行--no-sauce
/*****************************************/
bosszhang@bogon bin $ node macaw.js rm dir
remove dir
bosszhang@bogon bin $ node macaw.js rm dir --recursive
remove dir recursively
说明:
command可以接受输入命令,反正都是process.arg数组的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' ]
当一个Nodejs程序运行时,会有许多存在内存中的全局变量,其中有一个叫做process
,意为进程对象。process
对象中有一个叫做argv
的属性。命令行程序的第一个重头戏就是解析这个process.argv
属性。
看起来process.argv
好像是一个数组,其中第一个元素是node的执行路径,第二个元素是当前执行文件的路径,从第三个元素开始,是执行时带入的参数。
option
用户:.option('-n, --name <name>', 'your name', 'GK')
- 第一个参数是选项定义,分为短定义和长定义。用
|
,,
,连接。
- 参数可以用
<>
或者[]
修饰,前者意为必须参数,后者意为可选参数。
- 参数可以用
- 第二个参数为选项描述
- 第三个参数为选项参数默认值,可选。
commander.version 可以指定当前应用程序的一个版本号.
usage 指定使用方法,这里让我们输入一直值.
option 指定参数,格式,及参数说明.
/*****************************************/
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' ]
说明:
program.usage('<project-name>').parse(process.argv) 会获取process.argv[2]参数。
macaw.js 和 macaw-init.js 和 macaw-hello.js是有关联的
bosszhang@bogon test-cli $ node ./bin/macaw.js zzz
macaw-zzz(1) does not exist, try --help
提示说不存在,说明
输入什么就会执行相应的文件
node ./bin/macaw.js init就执行macaw-init.js
node ./bin/macaw.js hello就执行macaw-hello.js
说明:
这条命令
process.argv: [ '/usr/local/bin/node',
'/Users/bosszhang/Documents/workspace_temp/test-cli/bin/macaw-init.js',
'aa' ]
使用完
再使用
program.args: [ 'aa' ]
如果在那条命令之前,则:
program.args: undefined