Skip to content

Commit cea8972

Browse files
fix: remove inaccurate and unnecessary regexp used by unknown-options-as-args (#508)
--------- Co-authored-by: Xunnamius (Romulus) <[email protected]>
1 parent 553c808 commit cea8972

File tree

2 files changed

+111
-7
lines changed

2 files changed

+111
-7
lines changed

lib/yargs-parser.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -980,14 +980,8 @@ export class YargsParser {
980980
const flagWithEquals = /^-+([^=]+?)=[\s\S]*$/
981981
// e.g. '-a' or '--arg'
982982
const normalFlag = /^-+([^=]+?)$/
983-
// e.g. '-a-'
984-
const flagEndingInHyphen = /^-+([^=]+?)-$/
985-
// e.g. '-abc123'
986-
const flagEndingInDigits = /^-+([^=]+?\d+)$/
987-
// e.g. '-a/usr/local'
988-
const flagEndingInNonWordCharacters = /^-+([^=]+?)\W+.*$/
989983
// check the different types of flag styles, including negatedBoolean, a pattern defined near the start of the parse method
990-
return !hasFlagsMatching(arg, flagWithEquals, negatedBoolean, normalFlag, flagEndingInHyphen, flagEndingInDigits, flagEndingInNonWordCharacters)
984+
return !hasFlagsMatching(arg, flagWithEquals, negatedBoolean, normalFlag)
991985
}
992986

993987
// make a best effort to pick a default value

test/yargs-parser.mjs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3182,6 +3182,19 @@ describe('yargs-parser', function () {
31823182
k: 1
31833183
})
31843184
})
3185+
it('should ignore unknown options in short format followed by a dash character', function () {
3186+
// Somewhat redundant test since '-' is a non-word character (see next test) but '-' has own code in parser.
3187+
const argv = parser('-k- -u-', {
3188+
string: ['k'],
3189+
configuration: {
3190+
'unknown-options-as-args': true
3191+
}
3192+
})
3193+
argv.should.deep.equal({
3194+
_: ['-u-'],
3195+
k: '-'
3196+
})
3197+
})
31853198
it('should ignore unknown options in short format followed by a non-word character', function () {
31863199
const argv = parser('-k/1/ -u/2/', {
31873200
string: ['k'],
@@ -3217,6 +3230,103 @@ describe('yargs-parser', function () {
32173230
k: true,
32183231
v: true
32193232
})
3233+
})
3234+
// Fixes: https://github.com/yargs/yargs-parser/issues/501
3235+
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 () {
3236+
{
3237+
const argv = parser('--known-arg /1/ -k --known-arg-unknown', {
3238+
string: ['k', 'known-arg'],
3239+
narg: { k: 1, 'known-arg': 1 },
3240+
configuration: {
3241+
'unknown-options-as-args': true
3242+
}
3243+
})
3244+
argv.should.deep.equal({
3245+
_: [],
3246+
k: '--known-arg-unknown',
3247+
'knownArg': '/1/',
3248+
'known-arg': '/1/',
3249+
})
3250+
}
3251+
3252+
{
3253+
const argv = parser('-k --u-u', {
3254+
string: ['k', 'u'],
3255+
narg: { k: 1, u: 1 },
3256+
configuration: {
3257+
'unknown-options-as-args': true
3258+
}
3259+
})
3260+
argv.should.deep.equal({
3261+
_: [],
3262+
k: '--u-u'
3263+
})
3264+
}
3265+
})
3266+
// Fixes: https://github.com/yargs/yargs-parser/issues/501
3267+
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 () {
3268+
{
3269+
const argv = parser('-k /1/ --known-arg --k-u', {
3270+
string: ['k', 'known-arg'],
3271+
narg: { k: 1, 'known-arg': 1 },
3272+
configuration: {
3273+
'unknown-options-as-args': true
3274+
}
3275+
})
3276+
argv.should.deep.equal({
3277+
_: [],
3278+
k: '/1/',
3279+
'knownArg': '--k-u',
3280+
'known-arg': '--k-u',
3281+
})
3282+
}
3283+
3284+
{
3285+
const argv = parser('--known-arg --known-unknown', {
3286+
string: ['known-arg', 'known'],
3287+
narg: { 'known-arg': 1, 'known': 1 },
3288+
configuration: {
3289+
'unknown-options-as-args': true
3290+
}
3291+
})
3292+
argv.should.deep.equal({
3293+
_: [],
3294+
'knownArg': '--known-unknown',
3295+
'known-arg': '--known-unknown',
3296+
})
3297+
}
3298+
})
3299+
// Fixes: https://github.com/yargs/yargs-parser/issues/501
3300+
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 () {
3301+
{
3302+
const argv = parser('--known-arg --known-unknown --known-not-known', {
3303+
array: ['known-arg', 'known'],
3304+
configuration: {
3305+
'unknown-options-as-args': true
3306+
}
3307+
})
3308+
argv.should.deep.equal({
3309+
_: [],
3310+
knownArg: ['--known-unknown', '--known-not-known'],
3311+
'known-arg': ['--known-unknown', '--known-not-known']
3312+
})
3313+
}
3314+
3315+
{
3316+
const argv = parser('--known-arg --k-unknown --k-not-known', {
3317+
array: ['known-arg'],
3318+
alias: { 'known-arg': ['k'] },
3319+
configuration: {
3320+
'unknown-options-as-args': true
3321+
}
3322+
})
3323+
argv.should.deep.equal({
3324+
_: [],
3325+
knownArg: ['--k-unknown', '--k-not-known'],
3326+
'known-arg': ['--k-unknown', '--k-not-known'],
3327+
k: ['--k-unknown', '--k-not-known']
3328+
})
3329+
}
32203330
})
32213331
it('should parse negative numbers', function () {
32223332
const argv = parser('-k -33', {

0 commit comments

Comments
 (0)