微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决在 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 举报,一经查实,本站将立刻删除。