Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.5k views
in Technique[技术] by (71.8m points)

node.js - In yargs, what is the usage difference between (yargs) and (args) in the .command call?

I'm testing out some yargs commands, but I am a bit confused on its usages. My code is:

    const argv1 = require('yargs')
        .command('testFunc', 'testing yargs stuff', (yargs) => {
        yargs.option('age', {
            description: 'number for age',
            type: 'number'
        })
        console.log(` yargs section: ${yargs.name} | ${yargs.age}`);
    }, (argv) => {
        console.log(` argv  section: ${argv.name} | ${argv.age}`);
    }).argv;

Right now, if I run the command: node reporting-job-influx.js testFunc --name=Bob --age=1a, I get the following output:

yargs section: undefined | undefined
argv  section: Bob | NaN

So other than the yargs section unable to get the parameter data, what are the different purposes of having both a (yargs) => {}and an (args) => {}? And why can I still use a --name=Bob value despite not declaring it in the options?

Also, what would be the difference with having the (yargs) => {...} from above and moving the option up one level like this:

    .command('testFunc2', 'testing yargs stuff', (yargs) => {}, (argv) => {
        console.log(` argv  section: ${argv.name} | ${argv.age}`);
    })
    .option('age', {
        description: 'number for age',
        type: 'number'
    })

The output still verifies if --age is a number so it looks to function the same way.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...