From b1ff286a3c4c2dcc93b07b7050271bd5d8408666 Mon Sep 17 00:00:00 2001 From: Landon Yarrington Date: Tue, 20 Jul 2021 13:18:28 -0600 Subject: [PATCH 1/5] fix: parser should preserve inner quotes --- test/yargs-parser.cjs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/yargs-parser.cjs b/test/yargs-parser.cjs index 95bee24a..b7c22691 100644 --- a/test/yargs-parser.cjs +++ b/test/yargs-parser.cjs @@ -3587,6 +3587,22 @@ describe('yargs-parser', function () { args.foo.should.equal('-hello world') args.bar.should.equal('--goodnight moon') }) + + it.only('respects inner quotes (string)', function () { + const args = parser('cmd --foo ""Hello"" --bar ""World"" --baz "":)""') + console.log('string', {args}) + args.foo.should.equal('"Hello"') + args.bar.should.equal('"World"') + args.baz.should.equal('":)"') + }) + + it.only('respects inner quotes (array)', function () { + const args = parser(['cmd', '--foo', '"Hello"', '--bar', '"World"', '--baz', '":)"']) + console.log('array', {args}) + args.foo.should.equal('"Hello"') + args.bar.should.equal('"World"') + args.baz.should.equal('":)"') + }) }) // see: https://github.com/yargs/yargs-parser/issues/144 From 95a1f4d0a5b66e9daa4dcfe835d4912d1ae726a4 Mon Sep 17 00:00:00 2001 From: Landon Yarrington Date: Tue, 20 Jul 2021 13:39:46 -0600 Subject: [PATCH 2/5] remove .only --- test/yargs-parser.cjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/yargs-parser.cjs b/test/yargs-parser.cjs index b7c22691..7e22b23a 100644 --- a/test/yargs-parser.cjs +++ b/test/yargs-parser.cjs @@ -3588,7 +3588,7 @@ describe('yargs-parser', function () { args.bar.should.equal('--goodnight moon') }) - it.only('respects inner quotes (string)', function () { + it('respects inner quotes (string)', function () { const args = parser('cmd --foo ""Hello"" --bar ""World"" --baz "":)""') console.log('string', {args}) args.foo.should.equal('"Hello"') @@ -3596,7 +3596,7 @@ describe('yargs-parser', function () { args.baz.should.equal('":)"') }) - it.only('respects inner quotes (array)', function () { + it('respects inner quotes (array)', function () { const args = parser(['cmd', '--foo', '"Hello"', '--bar', '"World"', '--baz', '":)"']) console.log('array', {args}) args.foo.should.equal('"Hello"') From 10041ab67624d32eab6d722ac7859ad4ceb96d36 Mon Sep 17 00:00:00 2001 From: Landon Yarrington Date: Wed, 21 Jul 2021 01:00:27 -0600 Subject: [PATCH 3/5] fixes --- lib/yargs-parser.ts | 37 +++++++++++++++++++++++++++---------- test/yargs-parser.cjs | 8 ++++---- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/lib/yargs-parser.ts b/lib/yargs-parser.ts index 1499fc60..afc320be 100644 --- a/lib/yargs-parser.ts +++ b/lib/yargs-parser.ts @@ -59,6 +59,9 @@ export class YargsParser { // allow a string argument to be passed in rather // than an argv array. const args = tokenizeArgString(argsInput) + // tokenizeArgString adds extra quotes to args if argsInput is a string + // only strip those extra quotes in processValue if argsInput is a string + const shouldStripQuotes = typeof argsInput === 'string' // aliases might have transitive relationships, normalize this. const aliases = combineAliases(Object.assign(Object.create(null), opts.alias)) @@ -243,7 +246,13 @@ export class YargsParser { // nargs format = '--f=monkey washing cat' i = eatNargs(i, m[1], args, m[2]) } else { - setArg(m[1], m[2]) + // only strip quotes if they wouldn't already be removed + if (shouldStripQuotes) { + setArg(m[1], m[2]) + } else { + setArg(m[1], stripQuotes(m[2])) + } + } } } else if (arg.match(negatedBoolean) && configuration['boolean-negation']) { @@ -516,7 +525,7 @@ export class YargsParser { } else { // value in --option=value is eaten as is if (!isUndefined(argAfterEqualSign)) { - argsToSet.push(processValue(key, argAfterEqualSign)) + argsToSet.push(processValue(key, argAfterEqualSign, true)) } for (let ii = i + 1; ii < args.length; ii++) { if ((!configuration['greedy-arrays'] && argsToSet.length > 0) || @@ -524,7 +533,7 @@ export class YargsParser { next = args[ii] if (/^-/.test(next) && !negative.test(next) && !isUnknownOptionAsArg(next)) break i = ii - argsToSet.push(processValue(key, next)) + argsToSet.push(processValue(key, next, shouldStripQuotes)) } } @@ -548,7 +557,8 @@ export class YargsParser { addNewAlias(key, alias) } - const value = processValue(key, val) + console.log({val}) + const value = processValue(key, val, shouldStripQuotes) const splitKey = key.split('.') setKey(argv, splitKey, value) @@ -605,13 +615,10 @@ export class YargsParser { } } - function processValue (key: string, val: any) { + function processValue (key: string, val: any, shouldStripQuotes: boolean) { // strings may be quoted, clean this up as we assign values. - if (typeof val === 'string' && - (val[0] === "'" || val[0] === '"') && - val[val.length - 1] === val[0] - ) { - val = val.substring(1, val.length - 1) + if (shouldStripQuotes) { + val = stripQuotes(val) } // handle parsing boolean arguments --foo=true --bar false. @@ -1116,3 +1123,13 @@ function sanitizeKey (key: string): string { if (key === '__proto__') return '___proto___' return key } + +function stripQuotes(val: string): string { + return ( + typeof val === 'string' && + (val[0] === "'" || val[0] === '"') && + val[val.length - 1] === val[0] + ) + ? val = val.substring(1, val.length - 1) + : val +} diff --git a/test/yargs-parser.cjs b/test/yargs-parser.cjs index 7e22b23a..99740d41 100644 --- a/test/yargs-parser.cjs +++ b/test/yargs-parser.cjs @@ -3569,7 +3569,7 @@ describe('yargs-parser', function () { args.foo.should.equal('hello world') args.bar.should.equal('goodnight\'moon') const args2 = parser(['--foo', '"hello world"', '--bar="goodnight\'moon"']) - args2.foo.should.equal('hello world') + args2.foo.should.equal('"hello world"') args2.bar.should.equal('goodnight\'moon') }) @@ -3578,7 +3578,7 @@ describe('yargs-parser', function () { args.foo.should.equal('hello world') args.bar.should.equal('goodnight"moon') const args2 = parser(['--foo', "'hello world'", "--bar='goodnight\"moon'"]) - args2.foo.should.equal('hello world') + args2.foo.should.equal("'hello world'") args2.bar.should.equal('goodnight"moon') }) @@ -3589,7 +3589,7 @@ describe('yargs-parser', function () { }) it('respects inner quotes (string)', function () { - const args = parser('cmd --foo ""Hello"" --bar ""World"" --baz "":)""') + const args = parser('cmd --foo ""Hello"" --bar ""World"" --baz="":)""') console.log('string', {args}) args.foo.should.equal('"Hello"') args.bar.should.equal('"World"') @@ -3597,7 +3597,7 @@ describe('yargs-parser', function () { }) it('respects inner quotes (array)', function () { - const args = parser(['cmd', '--foo', '"Hello"', '--bar', '"World"', '--baz', '":)"']) + const args = parser(['cmd', '--foo', '"Hello"', '--bar', '"World"', '--baz="":)""']) console.log('array', {args}) args.foo.should.equal('"Hello"') args.bar.should.equal('"World"') From d8400082043450dbfebb80ada896f80acdfe3428 Mon Sep 17 00:00:00 2001 From: Landon Yarrington Date: Mon, 2 Aug 2021 19:08:41 -0600 Subject: [PATCH 4/5] fixes --- lib/yargs-parser.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/yargs-parser.ts b/lib/yargs-parser.ts index afc320be..cfdd111c 100644 --- a/lib/yargs-parser.ts +++ b/lib/yargs-parser.ts @@ -557,7 +557,6 @@ export class YargsParser { addNewAlias(key, alias) } - console.log({val}) const value = processValue(key, val, shouldStripQuotes) const splitKey = key.split('.') setKey(argv, splitKey, value) From 5ed073d480242be7c4959487ca12f39b4401e30a Mon Sep 17 00:00:00 2001 From: Landon Yarrington Date: Mon, 15 Nov 2021 15:05:29 -0700 Subject: [PATCH 5/5] fixes --- lib/yargs-parser.ts | 18 ++++++------------ test/yargs-parser.cjs | 2 -- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/lib/yargs-parser.ts b/lib/yargs-parser.ts index f04e3814..571a0cbc 100644 --- a/lib/yargs-parser.ts +++ b/lib/yargs-parser.ts @@ -61,7 +61,7 @@ export class YargsParser { const args = tokenizeArgString(argsInput) // tokenizeArgString adds extra quotes to args if argsInput is a string // only strip those extra quotes in processValue if argsInput is a string - const shouldStripQuotes = typeof argsInput === 'string' + const inputIsString = typeof argsInput === 'string' // aliases might have transitive relationships, normalize this. const aliases = combineAliases(Object.assign(Object.create(null), opts.alias)) @@ -246,13 +246,7 @@ export class YargsParser { // nargs format = '--f=monkey washing cat' i = eatNargs(i, m[1], args, m[2]) } else { - // only strip quotes if they wouldn't already be removed - if (shouldStripQuotes) { - setArg(m[1], m[2]) - } else { - setArg(m[1], stripQuotes(m[2])) - } - + setArg(m[1], m[2], true) } } } else if (arg.match(negatedBoolean) && configuration['boolean-negation']) { @@ -533,7 +527,7 @@ export class YargsParser { next = args[ii] if (/^-/.test(next) && !negative.test(next) && !isUnknownOptionAsArg(next)) break i = ii - argsToSet.push(processValue(key, next, shouldStripQuotes)) + argsToSet.push(processValue(key, next, inputIsString)) } } @@ -549,7 +543,7 @@ export class YargsParser { return i } - function setArg (key: string, val: any): void { + function setArg (key: string, val: any, shouldStripQuotes: boolean = inputIsString): void { if (/-/.test(key) && configuration['camel-case-expansion']) { const alias = key.split('.').map(function (prop) { return camelCase(prop) @@ -1123,12 +1117,12 @@ function sanitizeKey (key: string): string { return key } -function stripQuotes(val: string): string { +function stripQuotes (val: string): string { return ( typeof val === 'string' && (val[0] === "'" || val[0] === '"') && val[val.length - 1] === val[0] ) - ? val = val.substring(1, val.length - 1) + ? val.substring(1, val.length - 1) : val } diff --git a/test/yargs-parser.cjs b/test/yargs-parser.cjs index 99740d41..aa6bf6bf 100644 --- a/test/yargs-parser.cjs +++ b/test/yargs-parser.cjs @@ -3590,7 +3590,6 @@ describe('yargs-parser', function () { it('respects inner quotes (string)', function () { const args = parser('cmd --foo ""Hello"" --bar ""World"" --baz="":)""') - console.log('string', {args}) args.foo.should.equal('"Hello"') args.bar.should.equal('"World"') args.baz.should.equal('":)"') @@ -3598,7 +3597,6 @@ describe('yargs-parser', function () { it('respects inner quotes (array)', function () { const args = parser(['cmd', '--foo', '"Hello"', '--bar', '"World"', '--baz="":)""']) - console.log('array', {args}) args.foo.should.equal('"Hello"') args.bar.should.equal('"World"') args.baz.should.equal('":)"')