【问题标题】:Yargs - How to provide custom error msg for omitted positional argumentYargs - 如何为省略的位置参数提供自定义错误消息
【发布时间】:2017-11-15 10:00:39
【问题描述】:

我找不到正确配置位置参数的方法。我有这个代码:

#!/usr/bin/env node

const create = (argv) => {
  console.log('create component with name:', argv.name)
}

const createBuilder = (yargs) => {
  yargs.positional('name', {
    desc: 'Name of the new component',
  })
}

/* eslint-disable no-unused-expressions */
require('yargs')
  .command({
    command: 'create <name>',
    desc: 'Create a new component',
    builder: createBuilder,
    handler: create,
  })
  .demandCommand(1, 'A command is required')
  .help()
  .argv

如果用户在创建命令后没有指定名称,我想提供自定义错误消息。

从文档中我不清楚如何做到这一点,在浏览 github 问题时,我遇到了这条评论 (#928):

我建议改为使用 demandCommand 和 demandOption(每个 记录在案)。

这些允许您配置位置参数和标志参数 分别

我尝试过各种搭配

.demandCommand(1, 'You need to provide name for the new component')

.demandOption('name', 'You need to provide name for the new component')

但没有运气。有人知道怎么做吗?

【问题讨论】:

    标签: node.js command-line-interface yargs


    【解决方案1】:

    tl;dr - 尝试通过将字符串 *$0 添加到命令名称的开头来使您的命令成为默认命令。

    我发现只有在default command 中定义了位置参数时,才会尊重位置参数(即:显示在帮助菜单中并在未提供所需位置时抛出错误)。

    这是一个让它与您的代码一起使用的示例(请注意,您不再需要使用 .demandCommand()):

    require('yargs')
      .scriptName('cli-app')
      .command({
        command: '$0 create <name>',
        desc: 'Create a new component',
        builder: yargs => {
          yargs.positional('name', {
            desc: 'Name of the new component'
          });
        },
        handler: function(argv) {
          console.log('this is the handler function!');
        }
      })
      .help().argv;
    
    

    输出(注意最后的“没有足够的非选项参数:得到 1,需要至少 2”行):

    ➞ node cli-app.js create                                                                                                                                       1 ↵
    cli-app create <name>
    
    Create a new component
    
    Positionals:
      name  Name of the new component
    
    Options:
      --version  Show version number                                       [boolean]
      --help     Show help                                                 [boolean]
    
    Not enough non-option arguments: got 1, need at least 2
    

    【讨论】:

      【解决方案2】:

      yargs 的命令选项可以接受两种类型的参数。

      第一个是强制性&lt;varName&gt;。如果由于某种原因,用户在没有输入varName 的情况下键入了命令,那么它将运行帮助页面。

      第二个是可选[varName]。如果用户输入命令,即使缺少varName,命令也会运行。

      额外:如果您想要无限的varName 变量,那么您可以为wanted 选项提供spread operator。成为&lt;...varNames&gt;[...varNames]


      话虽如此,如果您想提供自定义错误消息,有几种方法可以解决。第一个是这个:

      const program = require('yargs')
          .command('create [fileName]', 'your description', () => {}, argv => {
              if(argv.fileName === undefined) {
                  console.error('You need to provide name for the new component')
                  return;
              }
              console.log(`success, a component called ${argv.fileName} got created.`)
          })
      

      Lodash 还提供了一个function _.isUndefined,它也可以工作。


      第二个是这个:

      const program = require('yargs')
          .command('create <fileName>', 'A description', () => {}, argv => {
      
          }).fail((msg, err, yargs) => {
              console.log('Sorry, no component name was given.')
          })
      
      program.argv
      

      更多信息,这里是 yargs api 上的fail documentation

      【讨论】:

      • 谢谢,但我觉得这更像是一种解决方法,而不是使用 yargs api 的解决方案。我看到的一个问题是 -h 会显示:create [fileName] Create a new component 将文件名标记为可选,而我想传达它是必需的。
      • 第二个例子修复了你的问题?
      • 不是真的,它仍然会使用undefined 调用处理程序,所以我必须处理它。另外我不知道还有什么其他错误可能导致fail 被调用,所以我必须添加代码来解决这个问题。加上用户通过console.log/error 反馈的信息与demandCommanddemandOption 返回的消息不一致。我希望像demandPositional 这样的东西......我可能只是在github上打开问题。不过,我感谢您的帮助!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-23
      • 2019-05-21
      • 2013-09-23
      • 1970-01-01
      • 2018-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多