yargs使用总结

yargs是nodejs环境下的命令行参数解析工具,
其github地址: https://github.com/yargs/yargs

我用的版本是: ^10.0.3

看看在gulp中的使用:

var argv = require('yargs').argv;
console.log('编译参数: \r\n', argv, '\r\n');
$ gulp build --module=m --mobile --rootFontSize=25

编译参数:
 { _: [ 'build' ],
  help: false,
  version: false,
  module: 'm',
  mobile: true,
  rootFontSize: 25,
  '$0': 'C:\\Users\\www\\AppData\\Roaming\\npm\\node_modules\\gulp\\bin\\gulp.js' 
 }

当然我用的是长参数, 个人觉得这样比较清晰, 短参数也是可以的:

$ gulp build -m a  -d=3 -s --prefix=/usr/local --color
编译参数:
{ _: [ 'build' ],
  help: false,
  version: false,
  m: 'a',
  d: 3,
  s: true,
  prefix: 'D:/soft/git/usr/local',
  color: true,
  '$0': 'C:\\Users\\www\\AppData\\Roaming\\npm\\node_modules\\gulp\\bin\\gulp.js' 
}

上述的prefix输出不一致, 是因为我的git安装在D:/soft/git, 所以会是这样.

可以看到, 不管长短参数, 没有参数值的就设置为true, 有参数值的就取相应的值, 很好用!
可以说, 这些使用方式已经足够满足我们的需要!

如果你需求比较简单, 可以利用process.argv来手动解析.

欢迎补充指正!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值