未为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((的行很好。我觉得我只是做错了。有人有工作的例子吗?

我使用的是码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);});

最新更新