diff --git a/src/node_options-inl.h b/src/node_options-inl.h index 877e8ce4ded92b..4419969a3bb8d3 100644 --- a/src/node_options-inl.h +++ b/src/node_options-inl.h @@ -268,6 +268,10 @@ inline std::string RequiresArgumentErr(const std::string& arg) { return arg + " requires an argument"; } +inline std::string UnexpectedArgumentErr(const std::string& arg) { + return arg + " does not take an argument"; +} + inline std::string NegationImpliesBooleanError(const std::string& arg) { return arg + " is an invalid negation because it is not a boolean option"; } @@ -475,6 +479,10 @@ void OptionsParser::Parse( value = value.substr(1); // Treat \- as escaping an -. } } + } else if (info.type == kBoolean && equals_index != std::string::npos) { + // Boolean options don't accept arguments + errors->push_back(UnexpectedArgumentErr(name)); + break; } switch (info.type) { diff --git a/test/parallel/test-cli-bad-options.js b/test/parallel/test-cli-bad-options.js index 6868541325302d..035d4d888d68e2 100644 --- a/test/parallel/test-cli-bad-options.js +++ b/test/parallel/test-cli-bad-options.js @@ -17,6 +17,8 @@ requiresArgument('--eval'); missingOption('--allow-fs-read=*', '--permission'); missingOption('--allow-fs-write=*', '--permission'); +forbidsArgument('--check'); + function missingOption(option, requiredOption) { const r = spawnSync(process.execPath, [option], { encoding: 'utf8' }); assert.strictEqual(r.status, 1); @@ -36,3 +38,15 @@ function requiresArgument(option) { `${process.execPath}: ${option} requires an argument` ); } + +function forbidsArgument(option) { + const r = spawnSync(process.execPath, [`${option}=invalid`], { encoding: 'utf8' }); + + assert.strictEqual(r.status, 9); + + const msg = r.stderr.split(/\r?\n/)[0]; + assert.strictEqual( + msg, + `${process.execPath}: ${option} does not take an argument` + ); +}