From 4f1060b50759fadbac3315c5117b0c3d65b0a7d8 Mon Sep 17 00:00:00 2001 From: Helio Machado <0x2b3bfa0+git@googlemail.com> Date: Tue, 5 Jul 2022 17:16:58 +0100 Subject: [PATCH] fix: parse options ending with 3+ hyphens (#434) Before this commit, options ending with three or more hyphens were being parsed as positional arguments, because a regular expression didn't have a start anchor. Co-authored-by: Benjamin E. Coe --- lib/yargs-parser.ts | 2 +- test/yargs-parser.cjs | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/yargs-parser.ts b/lib/yargs-parser.ts index 571a0cbc..5f776705 100644 --- a/lib/yargs-parser.ts +++ b/lib/yargs-parser.ts @@ -225,7 +225,7 @@ export class YargsParser { if (arg !== '--' && isUnknownOptionAsArg(arg)) { pushPositional(arg) // ---, ---=, ----, etc, - } else if (truncatedArg.match(/---+(=|$)/)) { + } else if (truncatedArg.match(/^---+(=|$)/)) { // options without key name are invalid. pushPositional(arg) continue diff --git a/test/yargs-parser.cjs b/test/yargs-parser.cjs index aa6bf6bf..97add48c 100644 --- a/test/yargs-parser.cjs +++ b/test/yargs-parser.cjs @@ -3588,6 +3588,13 @@ describe('yargs-parser', function () { args.bar.should.equal('--goodnight moon') }) + // see: https://github.com/yargs/yargs-parser/issues/433 + it('handles strings with three or more trailing dashes', function () { + const args = parser('--foo "hello---" --bar="world---"') + args.foo.should.equal('hello---') + args.bar.should.equal('world---') + }) + it('respects inner quotes (string)', function () { const args = parser('cmd --foo ""Hello"" --bar ""World"" --baz="":)""') args.foo.should.equal('"Hello"')