0

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. 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. I feel like I am just doing this wrong. Anyone have a working example?

I am using 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:

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?

habadeer
  • 91
  • 6

1 Answers1

0

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

habadeer
  • 91
  • 6