Skip to content

fix: correctly parse unknown args with dashes when they resemble known args #502

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/yargs-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,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)
}
Expand Down
97 changes: 97 additions & 0 deletions test/yargs-parser.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3188,6 +3188,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'],
Expand Down