【问题标题】:Why are commands missing from this yargs script help page?为什么此 yargs 脚本帮助页面中缺少命令?
【发布时间】:2021-06-19 23:59:33
【问题描述】:

有人可以解释为什么下面的脚本没有显示所有命令的帮助吗?

test.js [command]

Commands:
  test.js list-spaces  List spaces

Options:
  --help     Show help                                                 [boolean]
  --version  Show version number                                       [boolean]

请注意,list-commands 的帮助由于某种原因丢失了。

#!/usr/bin/env node

'use strict'
const yargs = require("yargs");

yargs.command({
  command: 'list-spaces',
  describe: 'List spaces',
  type: 'boolean',
  handler: function(argv) {
    console.log('aaa');
  }
}).argv;

yargs.command({
  command: 'list-connectors',
  describe: 'List connectors',
  builder: {
    space: {
      describe: 'space',
      demandOption: true,
      type: 'string'
    },
  },
  handler: function(argv) {
    console.log(argv.space);
  }
}).argv;

【问题讨论】:

  • 你的意思是list-connectors,而不是list-commands
  • 你可以像这样链接命令:yargs.command().command().command().help().argv

标签: javascript node.js ecmascript-6 yargs


【解决方案1】:

访问.argv 是触发解析(process.args)和输出生成的原因。来自the docs

不带参数调用.parse()等同于调用.argv

[…]

下面的其余方法仅在终止 .argv 或终止.parse() 之前出现。

您出于某种原因访问了两次.argv。第一次,第二个命令尚未注册。第二条语句甚至不再运行。

【讨论】:

    猜你喜欢
    • 2021-02-16
    • 2017-04-03
    • 2012-11-27
    • 1970-01-01
    • 2012-06-18
    • 2019-09-05
    • 1970-01-01
    • 2023-02-20
    • 1970-01-01
    相关资源
    最近更新 更多