node.js - 获取未为 yargs.getHelp() 定义的函数
问题描述
我希望获得由 yargs.getHelp() 产生的自动生成的帮助,但我收到了一个错误,即该函数未定义。这是示例代码:
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const { parsed, boolean } = require("yargs");
async function parseArgs(){
let parsedArgs = yargs(hideBin(process.argv))
.option("trend-file", {
alias: "t",
description: "The full filename of the trendfile.",
type: "string",
})
.option("start-time", {
alias: "s",
description: "Start time for trend.",
type: "string",
})
.argv;
const test = await yargs.getHelp();
console.log(test);
}
parseArgs()
.catch((e)=>{console.log(e.message);});
注意:这只是对较大代码库的提取。注释调用 yargs.getHelp() 的行可以正常工作。我觉得我只是做错了。有人有一个工作的例子吗?
我正在使用 yargs v17.2.1
更新---我能够通过将所有选项传递给 yargs() 然后像这样调用 getHelp() 来获得帮助:
let test = await yargs()
.option("trend-file", {
alias: "t",
description: "The full filename of the trendfile.",
type: "string",
})
.option("start-time", {
alias: "s",
description: "Start time for trend.",
type: "string",
})
.getHelp();
有没有更好的方法来做到这一点而不列出所有选项两次?
解决方案
我做错了。所需要的只是首先将 yargs 对象返回给一个变量,然后使用它来分别使用 argv 获取参数列表和使用 getHelp() 获取帮助。最终代码应如下所示:
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const { parsed, boolean } = require("yargs");
async function parseArgs(){
let parsedArgs = await yargs(hideBin(process.argv))
.option("trend-file", {
alias: "t",
description: "The full filename of the trendfile.",
type: "string",
})
.option("start-time", {
alias: "s",
description: "Start time for trend.",
type: "string",
});
let args = parsedArgs.argv;
const help = await parsedArgs.getHelp();
console.log(help);
}
parseArgs()
.catch((e)=>{console.log(e.message);});
推荐阅读
- android - 当我恢复我的颤振应用程序时如何解决我得到的错误
- qt - 从同名的后代方法调用 QML 祖先方法
- spring - Spring Boot 控制器捕获所有未配置的路由
- windows - MSI Installer fails with error: Assembly Install [GAC] at RemoveDirectoryAndChildren, line 393
- python - 根据来自另一个数组的值将布尔矩阵的元素更改为 True
- sql - 在 SQL Server 2008 R2 中执行的 SQL 查询出错
- python - 如何在值偏好后以原始索引顺序获取熊猫系列的值计数
- kotlin - 如何在 Kotlin 中创建 2D ArrayList?
- python - 如何使用 JupyterLab 循环更新交互式图形
- python - 如何计算按python中的值分组的数据框中的聚合百分比?