1

コマンダーの使用に問題があります: 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です。

前もって感謝します。

4

2 に答える 2

1

あなたの例の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
于 2020-03-13T22:11:40.093 に答える