Skip to content

Commit 804d180

Browse files
committed
src: add validation for CLI arguments that don't take a value
1 parent 7178e91 commit 804d180

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

src/node_options-inl.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ inline std::string RequiresArgumentErr(const std::string& arg) {
268268
return arg + " requires an argument";
269269
}
270270

271+
inline std::string UnexpectedArgumentErr(const std::string& arg) {
272+
return arg + " does not take an argument";
273+
}
274+
271275
inline std::string NegationImpliesBooleanError(const std::string& arg) {
272276
return arg + " is an invalid negation because it is not a boolean option";
273277
}
@@ -475,6 +479,10 @@ void OptionsParser<Options>::Parse(
475479
value = value.substr(1); // Treat \- as escaping an -.
476480
}
477481
}
482+
} else if (info.type == kBoolean && equals_index != std::string::npos) {
483+
// Boolean options don't accept arguments
484+
errors->push_back(UnexpectedArgumentErr(name));
485+
break;
478486
}
479487

480488
switch (info.type) {

test/parallel/test-cli-bad-options.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ requiresArgument('--eval');
1717
missingOption('--allow-fs-read=*', '--permission');
1818
missingOption('--allow-fs-write=*', '--permission');
1919

20+
forbidsArgument('--check');
21+
2022
function missingOption(option, requiredOption) {
2123
const r = spawnSync(process.execPath, [option], { encoding: 'utf8' });
2224
assert.strictEqual(r.status, 1);
@@ -36,3 +38,15 @@ function requiresArgument(option) {
3638
`${process.execPath}: ${option} requires an argument`
3739
);
3840
}
41+
42+
function forbidsArgument(option) {
43+
const r = spawnSync(process.execPath, [`${option}=invalid`], { encoding: 'utf8' });
44+
45+
assert.strictEqual(r.status, 9);
46+
47+
const msg = r.stderr.split(/\r?\n/)[0];
48+
assert.strictEqual(
49+
msg,
50+
`${process.execPath}: ${option} does not take an argument`
51+
);
52+
}

0 commit comments

Comments
 (0)