Commander.js
基本使用
const person = require('commander')
function list (val) {
return val.split(',')
}
person
.version('0.0.1')
.option('-m, --man', 'Add the sex')
.option('-w, --no-woman', 'Add the sex')
.option('-n, --name <string>', 'A string name')
.option('-p, --province [string]', 'Your province')
.option('-a, --age <number>', 'A int age', parseInt)
.option('-f, --favorite <items>', 'A favorite list', list)
.option('-c, --country <string>', 'Your country', 'USA')
person.on('--help', function(){
console.log('')
console.log('Examples:');
console.log(' -a <number> 指定年龄 ');
console.log(' -c <string> 指定国家(默认USA)');
});
person.parse(process.argv)
console.log(person.man, person.woman, person.name, person.age, person.favorite, person.country, person.province, person.argv)
$ node person.js -V => 0.0.1
$ node person.js -h => output help
$ node person.js -m -w -n Curry -a 30 -f basketball,spoof,golf -p XiaLuoTe
$ --------------true false Curry 30 [basketball, spoof, golf] USA XiaLuoTe
其他
- 正则表达
- command 命令
- 参数 syntax