commander.js基本用法

准备工作

  • 安装nodejs
  • 安装commander.js,执行npm install commander --save



version方法



作用:定义命令程序的版本号



参数说明:

  • 版本号<必须>
  • 自定义flag<可省略>,默认为 -V--version



使用:

(1)只传入版本信息

 
  1. // index.js

  2. const program = require('commander');

  3. program

  4. .version('1.0.0')

  5. .parse(process.argv);

  6. 复制代码

  • 执行node index.js -V 或者 node index.js --version得到版本号。

(2)自定义flag

 
  1. // index.js

  2. const program = require('commander');

  3. program

  4. .version('1.0.0', '-v, --version')

  5. .parse(process.argv);

  6. 复制代码

  • 当自定义flag时,--version不能被省略
  • 执行node index.js -v或者 node index.js --version得到版本号。



option方法



作用:定义命令的选项



参数说明:

  • 自定义flag<必须>
    • 一长一短的flag,中间可以逗号、竖线或空格隔开
    • flag后面可以跟参数,<>定义必需参数,[]定义可选参数
  • 选项的描述<可省略>:在使用-h或者--help时会显示
  • 选项的默认值<可省略>



使用

(1)定义多个选项

 
  1. // index.js

  2. const program = require('commander');

  3. program

  4. .version('1.0.0')

  5. .option('-a, --add', 'add Something')

  6. .option('-u, --update', 'update Something')

  7. .option('-r, --remove', 'remove Something')

  8. .parse(process.argv);

  9. console.log('You choose: ');

  10. if (program.add) console.log(' add Something');

  11. if (program.update) console.log(' update Something');

  12. if (program.remove) console.log(' remove Something');

  13. 复制代码

  • 短flag使用-,长flag使用--
  • program.XXX可以得到输入的选项
  • 执行node index.js -a或者node index.js --add会打印You choose: add Something

(2)多单词形式

 
  1. // index.js

  2. const program = require('commander');

  3. program

  4. .version('1.0.0')

  5. .option('--add-file', 'add a file')

  6. .parse(process.argv);

  7. if (program.addFile) console.log('add a file')

  8. 复制代码

  • 当选项为多单词形式时,使用驼峰形式得到输入项
  • 执行node index.js --add-file会打印add a file

(3)以--no形式开头的选项,代表后面紧跟单词的相反面

 
  1. // index.js

  2. const program = require('commander');

  3. program

  4. .version('1.0.0')

  5. .option('--no-add', 'not add a file')

  6. .parse(process.argv);

  7. if (program.add) console.log('add a file')

  8. else console.log('not add a file')

  9. 复制代码

  • 执行node index.js --no-add会打印not add a file

(4)选项后面使用<>或[]

 
  1. // index.js

  2. const program = require('commander');

  3. program

  4. .version('1.0.0')

  5. .option('-a, --add <fileName>', 'add a file')

  6. .parse(process.argv);

  7. console.log('add a file named: ' + program.add)

  8. 复制代码

  • 执行node index.js -a demo.js会打印add a file named: demo.js



command方法



作用:自定义命令



参数说明:

  • 自定义命令名称
    • 名称<必须>
    • 命令参数<可选>:
      • <>[]定义参数
      • 命令的最后一个参数可以是可变的,需要在数组后面加入 ... 标志;在命令后面传入的参数会
  • 命令描述<可省略>
  • 配置选项<可省略>:可配置noHelp、isDefault等



使用

 
  1. const program = require('commander');

  2. program

  3. .version('1.0.0')

  4. .command('my-cli <path>')

  5. .option('-a, --add <fileName>', 'add a file')

  6. .option('-u, --update <fileName>', 'update a file')

  7. .option('-r, --remove <fileName>', 'remove a file')

  8. .action(function(path, cmd) {

  9. console.log(path)

  10. console.log(cmd.add)

  11. })

  12. program.parse(process.argv);

  13. 复制代码

  • 使用action后会开启输入的选项校验,若输入了未定义的选项,则抛出错误
  • 执行node index.js my-cli C -a demo.js会打印C demo.js



description方法



作用:命令的描述性语句



参数说明

  • 命令的描述



使用

 
  1. // index.js

  2. const program = require('commander');

  3. program

  4. .version('1.0.0')

  5. .description('It is my cli')

  6. .parse(process.argv);

  7. 复制代码



action方法



作用:定义命令的回调函数



参数说明:

  • 回调函数



parse方法



作用:用于解析process.argv,设置options以及触发commands



参数说明:

  • process.argv

转自commander.js基本用法_weixin_33905756的博客-CSDN博客 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值