Skip to content

Commit 837f5dd

Browse files
committed
fix: correctly parse unknown args with dashes when they resemble known args
Fixes #501
1 parent 553c808 commit 837f5dd

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed

lib/yargs-parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ export class YargsParser {
985985
// e.g. '-abc123'
986986
const flagEndingInDigits = /^-+([^=]+?\d+)$/
987987
// e.g. '-a/usr/local'
988-
const flagEndingInNonWordCharacters = /^-+([^=]+?)\W+.*$/
988+
const flagEndingInNonWordCharacters = /^-+([^=]+?)[^\w-]+.*$/
989989
// check the different types of flag styles, including negatedBoolean, a pattern defined near the start of the parse method
990990
return !hasFlagsMatching(arg, flagWithEquals, negatedBoolean, normalFlag, flagEndingInHyphen, flagEndingInDigits, flagEndingInNonWordCharacters)
991991
}

test/yargs-parser.mjs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3194,6 +3194,103 @@ describe('yargs-parser', function () {
31943194
k: '/1/'
31953195
})
31963196
})
3197+
// Fixes: https://github.com/yargs/yargs-parser/issues/501
3198+
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 () {
3199+
{
3200+
const argv = parser('--known-arg /1/ -k --known-arg-unknown', {
3201+
string: ['k', 'known-arg'],
3202+
narg: { k: 1, 'known-arg': 1 },
3203+
configuration: {
3204+
'unknown-options-as-args': true
3205+
}
3206+
})
3207+
argv.should.deep.equal({
3208+
_: [],
3209+
k: '--known-arg-unknown',
3210+
'knownArg': '/1/',
3211+
'known-arg': '/1/',
3212+
})
3213+
}
3214+
3215+
{
3216+
const argv = parser('-k --u-u', {
3217+
string: ['k', 'u'],
3218+
narg: { k: 1, u: 1 },
3219+
configuration: {
3220+
'unknown-options-as-args': true
3221+
}
3222+
})
3223+
argv.should.deep.equal({
3224+
_: [],
3225+
k: '--u-u'
3226+
})
3227+
}
3228+
})
3229+
// Fixes: https://github.com/yargs/yargs-parser/issues/501
3230+
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 () {
3231+
{
3232+
const argv = parser('-k /1/ --known-arg --k-u', {
3233+
string: ['k', 'known-arg'],
3234+
narg: { k: 1, 'known-arg': 1 },
3235+
configuration: {
3236+
'unknown-options-as-args': true
3237+
}
3238+
})
3239+
argv.should.deep.equal({
3240+
_: [],
3241+
k: '/1/',
3242+
'knownArg': '--k-u',
3243+
'known-arg': '--k-u',
3244+
})
3245+
}
3246+
3247+
{
3248+
const argv = parser('--known-arg --known-unknown', {
3249+
string: ['known-arg', 'known'],
3250+
narg: { 'known-arg': 1, 'known': 1 },
3251+
configuration: {
3252+
'unknown-options-as-args': true
3253+
}
3254+
})
3255+
argv.should.deep.equal({
3256+
_: [],
3257+
'knownArg': '--known-unknown',
3258+
'known-arg': '--known-unknown',
3259+
})
3260+
}
3261+
})
3262+
// Fixes: https://github.com/yargs/yargs-parser/issues/501
3263+
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 () {
3264+
{
3265+
const argv = parser('--known-arg --known-unknown --known-not-known', {
3266+
array: ['known-arg', 'known'],
3267+
configuration: {
3268+
'unknown-options-as-args': true
3269+
}
3270+
})
3271+
argv.should.deep.equal({
3272+
_: [],
3273+
knownArg: ['--known-unknown', '--known-not-known'],
3274+
'known-arg': ['--known-unknown', '--known-not-known']
3275+
})
3276+
}
3277+
3278+
{
3279+
const argv = parser('--known-arg --k-unknown --k-not-known', {
3280+
array: ['known-arg'],
3281+
alias: { 'known-arg': ['k'] },
3282+
configuration: {
3283+
'unknown-options-as-args': true
3284+
}
3285+
})
3286+
argv.should.deep.equal({
3287+
_: [],
3288+
knownArg: ['--k-unknown', '--k-not-known'],
3289+
'known-arg': ['--k-unknown', '--k-not-known'],
3290+
k: ['--k-unknown', '--k-not-known']
3291+
})
3292+
}
3293+
})
31973294
it('should ignore unknown options in short format with multiple flags in one argument where an unknown flag is before the end', function () {
31983295
const argv = parser('-kuv', {
31993296
boolean: ['k', 'v'],

0 commit comments

Comments
 (0)