简体   繁体   English

未为 yargs.getHelp() 定义获取函数

[英]Getting function not defined for yargs.getHelp()

I am looking to get the automatically generated help that results from yargs.getHelp() and instead I am getting an error that the function is not defined.我希望获得由 yargs.getHelp() 产生的自动生成的帮助,但我收到一个错误,指出该函数未定义。 Here is the sample code:这是示例代码:

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);});

Note: This is just an extraction of the larger code base.注意:这只是对较大代码库的提取。 Commenting the line that calls yargs.getHelp() works fine.注释调用 yargs.getHelp() 的行工作正常。 I feel like I am just doing this wrong.我觉得我只是做错了。 Anyone have a working example?任何人都有一个工作示例?

I am using yargs v17.2.1我正在使用 yargs v17.2.1

Update--- I was able to get the help by passing all of the options to yargs() and then calling getHelp() like this:更新---我能够通过将所有选项传递给 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();

Is there a better way to do this without listing all the options twice?有没有更好的方法来做到这一点而不将所有选项列出两次?

I was doing it wrong.我做错了。 All that was necessary was to return the yargs object to a variable first and then use that to separately get the argument list using argv and the help using getHelp().所需要做的就是首先将 yargs 对象返回到一个变量,然后使用它分别使用 argv 和使用 getHelp() 获取参数列表。 The final code should look like this:最终代码应如下所示:

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);});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM