【问题标题】:Arrays using commander使用指挥官的数组
【发布时间】:2016-05-10 03:25:51
【问题描述】:

大家好!

我需要有关 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

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

谢谢!

【问题讨论】:

    标签: javascript arrays node.js command-line-interface


    【解决方案1】:

    你可以使用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
    

    【讨论】:

    • 你能解释一下在这种情况下强制是什么意思吗?
    • 这是一种强制修改选项的方法
    猜你喜欢
    • 1970-01-01
    • 2010-11-24
    • 1970-01-01
    • 2017-10-03
    • 2014-12-06
    • 1970-01-01
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    相关资源
    最近更新 更多