节点指挥官选项未定义
node commander options are undefined
我在使用指挥官时遇到问题:
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}`);
});
我不知道为什么 program.month
是未定义的,即使我 运行 和 --month 12
。
提前致谢。
尝试使用 program.commands[0].month
而不是 program.month
奇怪的是你应该像这样访问变量。
也许你可以通过 .action
个参数得到 month
?自己对指挥官不是很熟悉
您示例中的 month
选项已添加到 school
(子)命令,而不是程序。向动作处理程序传递一个额外参数,以允许您方便地访问其选项(正如@GiveMeAllYourCats 推测的那样)。
program
.command('school')
.arguments("<year>")
.option("--month <month>", "specify month")
.action(function (year, options) {
console.log(`the year is ${year} and the month is ${options.month}`);
});
program.parse(process.argv);
$ node index.js school --month=3 2020
the year is 2020 and the month is 3
我在使用指挥官时遇到问题: 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}`);
});
我不知道为什么 program.month
是未定义的,即使我 运行 和 --month 12
。
提前致谢。
尝试使用 program.commands[0].month
而不是 program.month
奇怪的是你应该像这样访问变量。
也许你可以通过 .action
个参数得到 month
?自己对指挥官不是很熟悉
您示例中的 month
选项已添加到 school
(子)命令,而不是程序。向动作处理程序传递一个额外参数,以允许您方便地访问其选项(正如@GiveMeAllYourCats 推测的那样)。
program
.command('school')
.arguments("<year>")
.option("--month <month>", "specify month")
.action(function (year, options) {
console.log(`the year is ${year} and the month is ${options.month}`);
});
program.parse(process.argv);
$ node index.js school --month=3 2020
the year is 2020 and the month is 3