如果传递无效标志,我如何让 Yargs 退出?

问题描述

我使用 Yargs JavaScript library 来解析命令行参数。

下面是一个HelloWorld风格的Yargs程序。认情况下,它包括“--help”和“--version”,我添加了第三个选项,“--copy”:

const argv = yargs(process.argv.slice(2))
  .alias("h","help") // By default,only "--help" is enabled
  .alias("v","version") // By default,only "--version" is enabled

  .boolean("copy")
  .alias("c","copy")
  .describe("c","run the copy function")

  .argv;

但是,使用虚假标志(例如“--foo”)运行程序仍然有效。如果传递了无效标志,我希望程序抛出错误退出。我该怎么做?

解决方法

显然这是使用严格模式执行的:

https://github.com/yargs/yargs/issues/1890