From 6a2977867bd58dbd8bb550f7b0b4c4c298835597 Mon Sep 17 00:00:00 2001 From: Landon Yarrington <33426811+jly36963@users.noreply.github.com> Date: Tue, 28 Dec 2021 16:55:55 -0700 Subject: [PATCH] fix: wrap unknown args in quotes (#2092) --- lib/validation.ts | 2 +- test/usage.cjs | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/validation.ts b/lib/validation.ts index 0a5b334a3..363fdf042 100644 --- a/lib/validation.ts +++ b/lib/validation.ts @@ -205,7 +205,7 @@ export function validation( 'Unknown argument: %s', 'Unknown arguments: %s', unknown.length, - unknown.join(', ') + unknown.map(s => (s.trim() ? s : `"${s}"`)).join(', ') ) ); } diff --git a/test/usage.cjs b/test/usage.cjs index eef18facc..62b0d536d 100644 --- a/test/usage.cjs +++ b/test/usage.cjs @@ -1110,6 +1110,27 @@ describe('usage tests', () => { r.should.have.property('exit').and.equal(true); }); + // Addresses: https://github.com/yargs/yargs/issues/2033 + it('should wrap whitespace in quotes if provided as an unknown argument', () => { + const r = checkUsage(() => { + return yargs(['--opt1=hello', ' ', '--opt2=world']) + .command({ + command: '$0', + desc: 'default description', + builder: yargs => + yargs + .option('opt1', {type: 'string'}) + .option('opt2', {type: 'string'}), + handler: noop, + }) + .strict() + .wrap(null) + .parse(); + }); + + r.errors.should.match(/Unknown argument: " "/); + }); + it('should fail given multiple option arguments without corresponding descriptions', () => { const r = checkUsage(() => { const opts = {