From dd9fbbe8e4e0b839f3bf0b534660fbfafe3bc320 Mon Sep 17 00:00:00 2001 From: Ragib MRB Date: Fri, 27 Nov 2026 21:44:27 +0530 Subject: [PATCH] fix: keep --option=value values attached to their option and check own defaults in CLI (#4265) --- bin/main.js | 23 +++++++++++++++-------- test/unit/bin.test.js | 17 +++++++++++++++++ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/bin/main.js b/bin/main.js index 8d403feb..cc26ded4 100644 --- a/bin/main.js +++ b/bin/main.js @@ -68,18 +68,20 @@ export async function main(nodeProcess) { let config; let opt; let noclobber; + let argValue; function getArg() { let arg = argv.shift(); + argValue = undefined; if (arg.indexOf('--') === 0) { // e.g. --opt - arg = arg.split('='); - if (arg.length > 1) { + const eqIndex = arg.indexOf('='); + if (eqIndex !== -1) { // e.g. --opt=val - argv.unshift(arg.slice(1).join('=')); + argValue = arg.slice(eqIndex + 1); + arg = arg.slice(0, eqIndex); } - arg = arg[0]; } else if (arg[0] === '-') { if (arg.length > 2) { // e.g. -abc @@ -98,6 +100,9 @@ export async function main(nodeProcess) { } function getOptionValue(option) { + if (argValue !== undefined) { + return argValue; + } if (argv.length === 0) { throw new Error(`marked: option '${option}' requires an argument`); } @@ -144,17 +149,19 @@ export async function main(nodeProcess) { default: if (arg.indexOf('--') === 0) { opt = camelize(arg.replace(/^--(no-)?/, '')); - if (!(opt in marked.defaults)) { + if (!Object.hasOwn(marked.defaults, opt)) { continue; } if (arg.indexOf('--no-') === 0) { cliOptions[opt] = typeof marked.defaults[opt] !== 'boolean' ? null : false; + } else if (typeof marked.defaults[opt] === 'boolean') { + cliOptions[opt] = argValue === undefined + ? true + : argValue !== 'false'; } else { - cliOptions[opt] = typeof marked.defaults[opt] !== 'boolean' - ? argv.shift() - : true; + cliOptions[opt] = getOptionValue(arg); } } else { files.push(arg); diff --git a/test/unit/bin.test.js b/test/unit/bin.test.js index b04ec40a..3c17e557 100644 --- a/test/unit/bin.test.js +++ b/test/unit/bin.test.js @@ -350,6 +350,23 @@ describe('bin/marked', () => { stdout: '

test

', })); + it('does not treat the value of an unknown --option=value as an input file', testInput({ + args: ['--unknown=ignored'], + stdin: '# test\n', + stdout: '

test

', + })); + + it('supports --option=false for boolean options without treating false as a file', testInput({ + args: ['--gfm=false'], + stdin: '~~text~~\n', + stdout: '

~~text~~

', + })); + + it('ignores prototype property names like --to-string without consuming the next argument', testInput({ + args: ['--to-string', fixturePath('bin-input.md')], + stdout: '

file

', + })); + for (const filename of ['-s.md', '--version']) { it(`treats ${filename} as a filename after --`, async() => { const dir = await mkdtemp(resolve(tmpdir(), 'marked-end-options-'));