使用命令的数组

Arrays using commander

大家好!

我需要有关 commander node.js 库的帮助。我需要创建这个接受 3 个标志的 CLI,--input、--output 和--pattern,比如:

commander
  .version('3.0.0')
  .usage('[options] <file ...>')
  .option('-i, --input', 'Array of files to be extracted')
  .option('-o, --output', 'Output file name')
  .option('-p, --pattern', 'Pattern name to be used in the extraction')
  .parse(process.argv);

我的问题是输入标志。我需要发送几个文件,为此我需要一个数组数据类型。

问题是:我就是想不通怎么做:

node ./bin/extract -i ../files/*.PDF

成为一个数组,其中包含我的文件目录中的所有文件。我已经尝试 运行 文档中的每个示例,但我没有找到解决问题的方法。另外,我搜索了这些问题,也没有找到...奇怪的是,也许我做错了什么,你们可以帮忙吗??

谢谢!

可以使用Coercion来实现:

function scanDir(val) {
    files = fs.readdirSync(val);
    return files;
}

program
    .version('0.0.1')
    .option('-s, --scan [value]', '', scanDir)
    .parse(process.argv);

console.log(' scan: %j', program.scan);

并这样称呼它:

node app.js -s /foo