【问题标题】:Where can i find documentation for builder in yargs npm?我在哪里可以找到 yargs npm 中的 builder 文档?
【发布时间】:2020-09-16 04:06:56
【问题描述】:

有人非常了解 npm yargs 吗?我正在将此包用于命令行参数的事情。但我找不到“builder”的解释。我在哪里可以买到这个?

yargs.command({
    command: 'test',
    describe: 'testing a note',
    builder:{
        sample:{
            describe: 'Note content',
            demandOption: true,
            type: 'string'
        },
    },
    handler(argv){
        notes.addNote(argv.title, argv.content);
    }
})

【问题讨论】:

  • 根据source,它可以是函数或对象。如果是对象,它会合并到命令选项source

标签: yargs


【解决方案1】:

构建器模式可以帮助通过文档帮助消息提供更多上下文,以使用可以使用的多个命令。

我们通常会使用 lambda 函数版本而不是对象版本。

例如,一个应用程序可以支持多个“命令”。这些命令将具有与同一应用程序相关联的不同功能。

下面是一个简单的get/post用户可以做的例子:

$ node src/index.js get
$ node src/index.js post "" 5001

index.js 之后的getpostyargs 认为的命令。

yargs 中实现此功能的一种方法是:

require('yargs') // eslint-disable-line
  .command('post data [port]', 'post some data', yargs => {
    yargs.positional('data', {
      describe: 'post string',
      require: true
    })
    .positional('port', {
      require: false,
      describe: 'port to bind on',
      default: 8080
    })
  })
  .command('get [port]', 'get some data', yargs => {
    yargs.positional('port', {
      require: false,
      describe: 'port to bind on',
      default: 8080
    })
  })
  .option('verbose', {
    alias: 'v',
    type: 'boolean',
    description: 'Run with verbose logging'
  })
  .argv

因此,在请求帮助信息时,您可以执行以下操作:

$ node src/index.js --help
index.js [command]

Commands:
  index.js serve [port]      start the server
  index.js post data [port]  post some data

Options:
      --help     Show help                                             [boolean]
      --version  Show version number                                   [boolean]
  -v, --verbose  Run with verbose logging                              [boolean]

然后您可以进一步深入了解帮助信息:

$ node src/index.js post --help
index.js post data [port]

post some data

Positionals:
  data  post string                                                   [required]
  port  port to bind on                                          [default: 8080]

这显示了post 命令的具体帮助。

因此,命令构建器模式允许我们将任意数量的命令传递给应用程序并为其提供参数文档。

当您必须支持许多需要自己的参数的不同命令时,它非常有用。

所以要回答从哪里获得更多文档的问题还有很长的路要走,如果你使用 lambda 版本,yargs 主对象会传递给你,你可以使用所有常用的yargs 参数来描述每个命令的参数。

因此,在上面的例子中,yargs 的用法就像:

yargs.positional('port', {
      require: false,
      describe: 'port to bind on',
      default: 8080
    })

记录在: https://github.com/yargs/yargs/blob/master/docs/api.md

【讨论】:

  • 这真的很有帮助!谢谢
【解决方案2】:

我发现上面的答案有点复杂,为了帮助将来可能登陆此页面的任何人,我建议阅读这篇 GeeksForGeeks 文章:

https://www.geeksforgeeks.org/node-js-yargs-module/

.command(cmd, desc, [builder], [handler]) 的例子是这样的:

//Add note
yargs
  .command({
    command: "add",
    describe: "Add a note",
    builder: {
      title: {
        describe: "Title of the note",
        demandOption: true,
        type: "string",
      },
    },
    handler: (argv) => {
      console.log("Added note with title:", argv.title);
    },
  })
  .parse();

在终端中,您必须输入:

node app.js add --title="The Complete Node.js Developer Course (3rd Edition)"  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    • 2010-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    相关资源
    最近更新 更多