代码之家  ›  专栏  ›  技术社区  ›  dommer

我如何配置yargs以获取单个输入文件和可选输出文件?

  •  1
  • dommer  · 技术社区  · 5 年前

    我在玩 yargs

    Usage: myapp <source file> [destination file]
    

    没有选项,没有命令等等。只需要一个文件路径来读取和一个可选的路径来写入。是什么让我这么做的 给我所有的好文件?

    我也不知道怎么用指挥官。试图从 process.argv

    0 回复  |  直到 5 年前
        1
  •  1
  •   Aminadav Glickshtein    5 年前

    下面是如何获取第一个和第二个参数的示例:

    var argv=require('yargs').argv
    var source=argv._[0]
    var dest=argv._[1]
    

    或者更好的方法:

    const argv = require("yargs").command(
      "$0 <source file> [destination file]",
      "the default command",
      () => {},
      function({ sourcefile, destinationfile }) {
        console.log({ sourcefile, destinationfile })
      },
    ).argv
    

    $0 是默认命令。

    myapp.js <source file> [destination file]
    
    the default command
    
    Options:
      --help     Show help                                                 [boolean]
      --version  Show version number                                       [boolean]
    
    Not enough non-option arguments: got 0, need at least 1
    

    了解更多: