使用指令器的数组

Arrays using commander

本文关键字:数组 指令      更新时间:2023-09-26

嘿伙计们!

我需要有关指挥官节点.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

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

谢谢!

您可以使用强制来实现它:

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