【问题标题】:How would I configure yargs to take a single input file and optional output file?我将如何配置 yargs 以获取单个输入文件和可选的输出文件?
【发布时间】:2019-08-14 10:20:42
【问题描述】:

我正在使用yargs 来配置以下命令行选项:

Usage: myapp <source file> [destination file]

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

似乎我需要使用选项或命令。我不能自己传递文件参数吗?

也不知道如何用指挥官来做到这一点。试图从process.argv 向上交易,但在第一关卡住了。

【问题讨论】:

    标签: javascript command-line-interface yargs


    【解决方案1】:

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

    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
    

    了解详情:

    【讨论】:

    • 谢谢。可以使用 yargs 来记录这些参数应该是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-14
    • 1970-01-01
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多