const {program} = require('commander') program .option('-d, --debug', 'output extra debugging') //格式,命令标识,在opts 里的描述 .option('-s, --small', 'small pizza size') .option('-p, --pizza-type <type>[no-required]', 'flavour of pizza'); //可接参数,<必选> [可选] .option('-c,--color <type>','show color','bule') //给选项赋予默认值 .option('-f','resolve with fuction',(new,old)=>{},old) //用一个函数处理 应当要返回的值 .option('-l,--list <numbers...>',) //变长参数 program.parse(process.argv);
if (program.debug) console.log(program.opts()); console.log('pizza details:'); if (program.small) console.log('- small pizza size'); if (program.pizzaType) console.log(`- ${program.pizzaType}`);
取反选项,no-xxx
几个函数
program.args 返回未被处理的参数列表
program.opts()返回命令相关信息
program.parse 处理参数
版本选项
program.version(‘0.0.1’, ‘-v, –vers’, ‘output the current version’);