node.js - Node 命令选项未定义

标签 node.js node-commander

我在使用指挥官时遇到问题: https://github.com/tj/commander.js/

program
    .command('school')
    .arguments("<year>")
    .option("--month <month>", "specify month")
    .parse(process.argv)
    .action(function (year) {
        console.log(`the year is ${year} and the month is ${program.month}`);
    });

我不知道为什么,但即使我使用 --month 12 运行,program.month 也未定义。

提前致谢。

最佳答案

尝试使用program.commands[0].month而不是program.month 不过,你应该像这样访问变量,这很奇怪。

也许你可以通过 .action 参数获取month?我自己对指挥官不太熟悉。

关于node.js - Node 命令选项未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37005123/

相关文章:

javascript - 了解 Node.js 应用程序中的控制流

node.js - Superagent res.text 未定义

node.js - 事件.js :85//Unhandled error event

node.js - 创建 nodejs cli 选择/选项菜单

javascript - 如何使用 Commander.js 运行 bash?

javascript - 在 Node.JS 中一次读取一个大文件 N 行

javascript - 如何保存应用程序的登录次数?(nodejs,JS,mongodb)

javascript - Commander.js 收集多个选项总是包括默认

javascript - Node js指挥官args返回true而不是值