mirror of
https://github.com/markedjs/marked.git
synced 2026-11-30 16:48:05 +01:00
fix: reject CLI options missing their values (#4261)
This commit is contained in:
+11
-4
@@ -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':
|
||||
|
||||
@@ -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>',
|
||||
|
||||
Reference in New Issue
Block a user