fix: reject CLI options missing their values (#4261)

This commit is contained in:
Ragib MRB
2026-11-20 00:44:08 -07:00
committed by GitHub
parent 1e1190241b
commit 502e658115
2 changed files with 28 additions and 4 deletions
+11 -4
View File
@@ -97,6 +97,13 @@ export async function main(nodeProcess) {
return arg;
}
function getOptionValue(option) {
if (argv.length === 0) {
throw new Error(`marked: option '${option}' requires an argument`);
}
return argv.shift();
}
while (argv.length) {
if (argv[0] === '--') {
files.push(...argv.slice(1));
@@ -106,15 +113,15 @@ export async function main(nodeProcess) {
switch (arg) {
case '-o':
case '--output':
output = argv.shift();
output = getOptionValue(arg);
break;
case '-i':
case '--input':
input = argv.shift();
input = getOptionValue(arg);
break;
case '-s':
case '--string':
string = argv.shift();
string = getOptionValue(arg);
break;
case '-t':
case '--tokens':
@@ -122,7 +129,7 @@ export async function main(nodeProcess) {
break;
case '-c':
case '--config':
config = argv.shift();
config = getOptionValue(arg);
break;
case '-n':
case '--no-clobber':
+17
View File
@@ -103,6 +103,17 @@ function execMarked({ args = '', stdin = '', stdout = '', stderr = '' }) {
}
describe('bin/marked', () => {
describe('required option values', () => {
for (const option of ['-o', '--output', '-i', '--input', '-s', '--string', '-c', '--config']) {
it(`rejects ${option} without a value`, testInput({
args: [option],
stdin: '# ignored',
stderr: `marked: option '${option}' requires an argument`,
code: 1,
}));
}
});
describe('output', () => {
it('writes all output before exiting when stdout is piped', async() => {
const dir = await mkdtemp(resolve(tmpdir(), 'marked-stdout-'));
@@ -182,6 +193,12 @@ describe('bin/marked', () => {
});
describe('string', () => {
it('--string= accepts empty input instead of reading stdin', testInput({
args: ['--string='],
stdin: '# ignored',
stdout: '\n',
}));
it('-s', testInput({
args: ['-s', '# test'],
stdout: '<h1>test</h1>',