【问题标题】:Yargs - can I get "rest" arguments?Yargs - 我可以得到“休息”的论点吗?
【发布时间】:2021-09-29 10:41:01
【问题描述】:

我需要处理特定选项,并将所有其他参数作为数组进一步传递。

例如:

我这样调用脚本: node script.js --one 1 --two 2 --three 3

const args = yargs(argv).demandOption("one").string("one").argv;
console.log(args.one);

const rest = ???
console.log(rest);

应该写:

1
["--two", 2, "--three", 3]

这对 yargs 可行吗?类似于_ 的值,但_ 只包含非选项值,所以我不能使用它。

【问题讨论】:

    标签: javascript yargs


    【解决方案1】:

    感谢惊人的 yargs 测试套件找到了答案: https://github.com/yargs/yargs/blob/main/test/yargs.cjs#L1841

    我只需要配置 yargs 以将未知选项也传递给 _

    const args = yargs(argv)
        .parserConfiguration({'unknown-options-as-args': true})
        .demandOption("one")
        .string("one")
        .argv;
    

    【讨论】:

      猜你喜欢
      • 2014-10-15
      • 1970-01-01
      • 1970-01-01
      • 2011-01-31
      • 1970-01-01
      • 1970-01-01
      • 2011-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多