From 837f5dd6ab1cab452c26c1e5081e08382fa5b024 Mon Sep 17 00:00:00 2001 From: "Xunnamius (Romulus)" Date: Mon, 17 Mar 2025 16:12:35 -0700 Subject: [PATCH] fix: correctly parse unknown args with dashes when they resemble known args Fixes #501 --- lib/yargs-parser.ts | 2 +- test/yargs-parser.mjs | 97 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 1 deletion(-) diff --git a/lib/yargs-parser.ts b/lib/yargs-parser.ts index 7ca73055..59e44386 100644 --- a/lib/yargs-parser.ts +++ b/lib/yargs-parser.ts @@ -985,7 +985,7 @@ export class YargsParser { // e.g. '-abc123' const flagEndingInDigits = /^-+([^=]+?\d+)$/ // e.g. '-a/usr/local' - const flagEndingInNonWordCharacters = /^-+([^=]+?)\W+.*$/ + const flagEndingInNonWordCharacters = /^-+([^=]+?)[^\w-]+.*$/ // check the different types of flag styles, including negatedBoolean, a pattern defined near the start of the parse method return !hasFlagsMatching(arg, flagWithEquals, negatedBoolean, normalFlag, flagEndingInHyphen, flagEndingInDigits, flagEndingInNonWordCharacters) } diff --git a/test/yargs-parser.mjs b/test/yargs-parser.mjs index f304031e..288f383b 100644 --- a/test/yargs-parser.mjs +++ b/test/yargs-parser.mjs @@ -3194,6 +3194,103 @@ describe('yargs-parser', function () { k: '/1/' }) }) + // Fixes: https://github.com/yargs/yargs-parser/issues/501 + it('should allow an unknown arg that resembles a known arg and contains hyphens to be used as the value of another flag in short form', function () { + { + const argv = parser('--known-arg /1/ -k --known-arg-unknown', { + string: ['k', 'known-arg'], + narg: { k: 1, 'known-arg': 1 }, + configuration: { + 'unknown-options-as-args': true + } + }) + argv.should.deep.equal({ + _: [], + k: '--known-arg-unknown', + 'knownArg': '/1/', + 'known-arg': '/1/', + }) + } + + { + const argv = parser('-k --u-u', { + string: ['k', 'u'], + narg: { k: 1, u: 1 }, + configuration: { + 'unknown-options-as-args': true + } + }) + argv.should.deep.equal({ + _: [], + k: '--u-u' + }) + } + }) + // Fixes: https://github.com/yargs/yargs-parser/issues/501 + it('should allow an unknown arg that resembles a known arg and contains hyphens to be used as the value of another flag in long form', function () { + { + const argv = parser('-k /1/ --known-arg --k-u', { + string: ['k', 'known-arg'], + narg: { k: 1, 'known-arg': 1 }, + configuration: { + 'unknown-options-as-args': true + } + }) + argv.should.deep.equal({ + _: [], + k: '/1/', + 'knownArg': '--k-u', + 'known-arg': '--k-u', + }) + } + + { + const argv = parser('--known-arg --known-unknown', { + string: ['known-arg', 'known'], + narg: { 'known-arg': 1, 'known': 1 }, + configuration: { + 'unknown-options-as-args': true + } + }) + argv.should.deep.equal({ + _: [], + 'knownArg': '--known-unknown', + 'known-arg': '--known-unknown', + }) + } + }) + // Fixes: https://github.com/yargs/yargs-parser/issues/501 + it('should allow an unknown arg that resembles a known arg and contains hyphens to be used as the value of another flag in array form', function () { + { + const argv = parser('--known-arg --known-unknown --known-not-known', { + array: ['known-arg', 'known'], + configuration: { + 'unknown-options-as-args': true + } + }) + argv.should.deep.equal({ + _: [], + knownArg: ['--known-unknown', '--known-not-known'], + 'known-arg': ['--known-unknown', '--known-not-known'] + }) + } + + { + const argv = parser('--known-arg --k-unknown --k-not-known', { + array: ['known-arg'], + alias: { 'known-arg': ['k'] }, + configuration: { + 'unknown-options-as-args': true + } + }) + argv.should.deep.equal({ + _: [], + knownArg: ['--k-unknown', '--k-not-known'], + 'known-arg': ['--k-unknown', '--k-not-known'], + k: ['--k-unknown', '--k-not-known'] + }) + } + }) it('should ignore unknown options in short format with multiple flags in one argument where an unknown flag is before the end', function () { const argv = parser('-kuv', { boolean: ['k', 'v'],