在 yargs 中,.command 调用中 (yargs) 和 (args) 之间的用法区别是什么?

问题描述

我正在测试一些 yargs 命令,但我对其用法有些困惑。我的代码是:

    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;

现在,如果我运行命令: node reporting-job-influx.js testFunc --name=Bob --age=1a,我会得到以下输出

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

那么除了 yargs 部分无法获取参数数据之外,同时拥有 (yargs) => {}(args) => {} 的不同目的是什么?为什么尽管没有在选项中声明它,我仍然可以使用 --name=Bob 值?

此外,将 (yargs) => {...} 从上方放置并将选项向上移动一级,如下所示有什么区别:

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

输出仍会验证 --age 是否为数字,因此它看起来以相同的方式运行。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)