かなり古いバージョンのコマンダー (v2.20.3) からパッケージを移行しています
program.command(`install [pkg]`)
.alias(`i`)
.action(installPackageOrLocal)
.option(`-S, --save`, `Save to dependencies`)
.option(`-D, --save-dev`, `Save to devDependencies`)
.option(`--production`, `Will not install modules listed in devDependencies`)
.option(`--test`, `Exit with code 1 if package limits like maxPackagesNumber or maxSizeBites exceeded`);
デフォルト (引数をまったく指定せずに CLI を呼び出す場合) をヘルプの表示のままにし、エラーが発生しないようにしたいと思いますが、現在は次のようにエラーが発生します。
.../npm-reflect/node_modules/.pnpm/commander@8.3.0/node_modules/commander/lib/command.js:142 const [, name, args] = nameAndArgs.match(/([^ ]+) ( . )/);
以下を追加することで、最も望ましい動作を得ることができました。
program.command('help', {isDefault: true})
.action(() => {
program.help();
})
.command(`install [pkg]`)
// ...
...しかし、これは新しい「ヘルプ」コマンドをリストすることで、ヘルプの内容を汚染しているようです。新しいコマンドを追加せずに、引数がまだ存在しないときにパーサーが不平を言うのを避けるにはどうすればよいですか?