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

How would I configure yargs to take a single input file and optional output file?

我正在玩 yargs 想配置以下命令行选项:

Usage: myapp <source file> [destination file]

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

看来我需要使用选项或命令。我不能单独传入文件参数吗?

也看不出如何用指挥官来做到这一点。试图从 process.argv 向上交易,但卡在了第一个障碍。

这里是一个如何获取第一个和第二个参数的例子:

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

或者更好的方法:

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

[=13=] 是默认命令。 输出应该是:

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

了解更多: