Skip to content

Commit 120db9a

Browse files
authored
Merge branch 'main' into normalise-negative-numbers-without-short-option-groups
2 parents f9514ad + cea8972 commit 120db9a

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
@@ -3200,6 +3200,19 @@ describe('yargs-parser', function () {
32003200
k: 1
32013201
})
32023202
})
3203+
it('should ignore unknown options in short format followed by a dash character', function () {
3204+
// Somewhat redundant test since '-' is a non-word character (see next test) but '-' has own code in parser.
3205+
const argv = parser('-k- -u-', {
3206+
string: ['k'],
3207+
configuration: {
3208+
'unknown-options-as-args': true
3209+
}
3210+
})
3211+
argv.should.deep.equal({
3212+
_: ['-u-'],
3213+
k: '-'
3214+
})
3215+
})
32033216
it('should ignore unknown options in short format followed by a non-word character', function () {
32043217
const argv = parser('-k/1/ -u/2/', {
32053218
string: ['k'],
@@ -3235,6 +3248,103 @@ describe('yargs-parser', function () {
32353248
k: true,
32363249
v: true
32373250
})
3251+
})
3252+
// Fixes: https://github.com/yargs/yargs-parser/issues/501
3253+
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 () {
3254+
{
3255+
const argv = parser('--known-arg /1/ -k --known-arg-unknown', {
3256+
string: ['k', 'known-arg'],
3257+
narg: { k: 1, 'known-arg': 1 },
3258+
configuration: {
3259+
'unknown-options-as-args': true
3260+
}
3261+
})
3262+
argv.should.deep.equal({
3263+
_: [],
3264+
k: '--known-arg-unknown',
3265+
'knownArg': '/1/',
3266+
'known-arg': '/1/',
3267+
})
3268+
}
3269+
3270+
{
3271+
const argv = parser('-k --u-u', {
3272+
string: ['k', 'u'],
3273+
narg: { k: 1, u: 1 },
3274+
configuration: {
3275+
'unknown-options-as-args': true
3276+
}
3277+
})
3278+
argv.should.deep.equal({
3279+
_: [],
3280+
k: '--u-u'
3281+
})
3282+
}
3283+
})
3284+
// Fixes: https://github.com/yargs/yargs-parser/issues/501
3285+
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 () {
3286+
{
3287+
const argv = parser('-k /1/ --known-arg --k-u', {
3288+
string: ['k', 'known-arg'],
3289+
narg: { k: 1, 'known-arg': 1 },
3290+
configuration: {
3291+
'unknown-options-as-args': true
3292+
}
3293+
})
3294+
argv.should.deep.equal({
3295+
_: [],
3296+
k: '/1/',
3297+
'knownArg': '--k-u',
3298+
'known-arg': '--k-u',
3299+
})
3300+
}
3301+
3302+
{
3303+
const argv = parser('--known-arg --known-unknown', {
3304+
string: ['known-arg', 'known'],
3305+
narg: { 'known-arg': 1, 'known': 1 },
3306+
configuration: {
3307+
'unknown-options-as-args': true
3308+
}
3309+
})
3310+
argv.should.deep.equal({
3311+
_: [],
3312+
'knownArg': '--known-unknown',
3313+
'known-arg': '--known-unknown',
3314+
})
3315+
}
3316+
})
3317+
// Fixes: https://github.com/yargs/yargs-parser/issues/501
3318+
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 () {
3319+
{
3320+
const argv = parser('--known-arg --known-unknown --known-not-known', {
3321+
array: ['known-arg', 'known'],
3322+
configuration: {
3323+
'unknown-options-as-args': true
3324+
}
3325+
})
3326+
argv.should.deep.equal({
3327+
_: [],
3328+
knownArg: ['--known-unknown', '--known-not-known'],
3329+
'known-arg': ['--known-unknown', '--known-not-known']
3330+
})
3331+
}
3332+
3333+
{
3334+
const argv = parser('--known-arg --k-unknown --k-not-known', {
3335+
array: ['known-arg'],
3336+
alias: { 'known-arg': ['k'] },
3337+
configuration: {
3338+
'unknown-options-as-args': true
3339+
}
3340+
})
3341+
argv.should.deep.equal({
3342+
_: [],
3343+
knownArg: ['--k-unknown', '--k-not-known'],
3344+
'known-arg': ['--k-unknown', '--k-not-known'],
3345+
k: ['--k-unknown', '--k-not-known']
3346+
})
3347+
}
32383348
})
32393349
it('should parse negative numbers', function () {
32403350
const argv = parser('-k -33', {

0 commit comments

Comments
 (0)