Skip to content

Commit

Permalink
feature: add configuration option "ignore-unknown-options"
Browse files Browse the repository at this point in the history
  • Loading branch information
henderea committed Jun 7, 2019
1 parent 47ccb0b commit 0cce777
Show file tree
Hide file tree
Showing 2 changed files with 227 additions and 17 deletions.
89 changes: 72 additions & 17 deletions index.js
Expand Up @@ -26,7 +26,8 @@ function parse (args, opts) {
'set-placeholder-key': false,
'halt-at-non-option': false,
'strip-aliased': false,
'strip-dashed': false
'strip-dashed': false,
'ignore-unknown-options': false
}, opts.configuration)
var defaults = opts.default || {}
var configObjects = opts.configObjects || []
Expand Down Expand Up @@ -168,12 +169,18 @@ function parse (args, opts) {
} else if (checkAllAliases(m[1], flags.arrays) && args.length > i + 1) {
args.splice(i + 1, 0, m[2])
i = eatArray(i, m[1], args)
} else {
} else if (!configuration['ignore-unknown-options'] || hasAnyFlag(m[1])) {
setArg(m[1], m[2])
} else {
argv._.push(maybeCoerceNumber('_', arg))
}
} else if (arg.match(negatedBoolean) && configuration['boolean-negation']) {
key = arg.match(negatedBoolean)[1]
setArg(key, false)
if (!configuration['ignore-unknown-options'] || hasAnyFlag(key)) {
setArg(key, false)
} else {
argv._.push(maybeCoerceNumber('_', arg))
}

// -- seperated by space.
} else if (arg.match(/^--.+/) || (
Expand All @@ -187,7 +194,7 @@ function parse (args, opts) {
// array format = '--foo a b c'
} else if (checkAllAliases(key, flags.arrays) && args.length > i + 1) {
i = eatArray(i, key, args)
} else {
} else if (!configuration['ignore-unknown-options'] || hasAnyFlag(key)) {
next = flags.nargs[key] === 0 ? undefined : args[i + 1]

if (next !== undefined && (!next.match(/^-/) ||
Expand All @@ -202,29 +209,40 @@ function parse (args, opts) {
} else {
setArg(key, defaultValue(key))
}
} else {
argv._.push(maybeCoerceNumber('_', arg))
}

// dot-notation flag seperated by '='.
} else if (arg.match(/^-.\..+=/)) {
m = arg.match(/^-([^=]+)=([\s\S]*)$/)
setArg(m[1], m[2])
if (!configuration['ignore-unknown-options'] || hasAnyFlag(m[1])) {
setArg(m[1], m[2])
} else {
argv._.push(maybeCoerceNumber('_', arg))
}

// dot-notation flag seperated by space.
} else if (arg.match(/^-.\..+/)) {
next = args[i + 1]
key = arg.match(/^-(.\..+)/)[1]

if (next !== undefined && !next.match(/^-/) &&
!checkAllAliases(key, flags.bools) &&
!checkAllAliases(key, flags.counts)) {
setArg(key, next)
i++
if(!configuration['ignore-unknown-options'] || hasAnyFlag(key)) {
if (next !== undefined && !next.match(/^-/) &&
!checkAllAliases(key, flags.bools) &&
!checkAllAliases(key, flags.counts)) {
setArg(key, next)
i++
} else {
setArg(key, defaultValue(key))
}
} else {
setArg(key, defaultValue(key))
argv._.push(maybeCoerceNumber('_', arg))
}
} else if (arg.match(/^-[^-]+/) && !arg.match(negative)) {
letters = arg.slice(1, -1).split('')
broken = false
unmatched = []

for (var j = 0; j < letters.length; j++) {
next = arg.slice(j + 2)
Expand All @@ -241,33 +259,54 @@ function parse (args, opts) {
} else if (checkAllAliases(key, flags.arrays) && args.length > i + 1) {
args.splice(i + 1, 0, value)
i = eatArray(i, key, args)
} else {
} else if (!configuration['ignore-unknown-options'] || hasAnyFlag(key)) {
setArg(key, value)
} else {
unmatched.push(key);
unmatched.push('=');
unmatched.push(value);
}

broken = true
break
}

if (next === '-') {
setArg(letters[j], next)
if (!configuration['ignore-unknown-options'] || hasAnyFlag(letters[j])) {
setArg(letters[j], next)
} else {
unmatched.push(letters[j]);
unmatched.push(next);
}
continue
}

// current letter is an alphabetic character and next value is a number
if (/[A-Za-z]/.test(letters[j]) &&
/^-?\d+(\.\d*)?(e-?\d+)?$/.test(next)) {
setArg(letters[j], next)
if (!configuration['ignore-unknown-options'] || hasAnyFlag(letters[j])) {
setArg(letters[j], next)
} else {
unmatched.push(letters[j]);
unmatched.push(next);
}
broken = true
break
}

if (letters[j + 1] && letters[j + 1].match(/\W/)) {
setArg(letters[j], next)
if (!configuration['ignore-unknown-options'] || hasAnyFlag(letters[j])) {
setArg(letters[j], next)
} else {
unmatched.push(letters[j]);
unmatched.push(next);
}
broken = true
break
} else {
} else if (!configuration['ignore-unknown-options'] || hasAnyFlag(letters[j])) {
setArg(letters[j], defaultValue(letters[j]))
} else {
unmatched.push(letters[j])
}
}

Expand All @@ -280,7 +319,7 @@ function parse (args, opts) {
// array format = '-f a b c'
} else if (checkAllAliases(key, flags.arrays) && args.length > i + 1) {
i = eatArray(i, key, args)
} else {
} else if (!configuration['ignore-unknown-options'] || hasAnyFlag(key)) {
next = args[i + 1]

if (next !== undefined && (!/^(-|--)[^-]/.test(next) ||
Expand All @@ -295,8 +334,13 @@ function parse (args, opts) {
} else {
setArg(key, defaultValue(key))
}
} else {
unmatched.push(key);
}
}
if (unmatched.length > 0) {
argv._.push(maybeCoerceNumber('_', ['-', ...unmatched].join('')))
}
} else if (arg === '--') {
notFlags = args.slice(i + 1)
break
Expand Down Expand Up @@ -761,6 +805,17 @@ function parse (args, opts) {

return isSet
}

function hasAnyFlag (key) {
var isSet = false
var toCheck = [].concat(Object.values(flags))

toCheck.forEach(function (flag) {
if (flag[key]) isSet = flag[key]
})

return isSet
}

function setDefaulted (key) {
[].concat(flags.aliases[key] || [], key).forEach(function (k) {
Expand Down
155 changes: 155 additions & 0 deletions test/yargs-parser.js
Expand Up @@ -2963,4 +2963,159 @@ describe('yargs-parser', function () {
})
})
})
describe('ignore-unknown-options', function () {
it('should ignore unknown options in long format separated by =', function () {
const argv = parser('--known-arg=1 --unknown-arg=2', {
number: ['known-arg'],
configuration: {
'ignore-unknown-options': true
}
})
argv.should.deep.equal({
_: ['--unknown-arg=2'],
'known-arg': 1,
'knownArg': 1
})
})
it('should ignore unknown options in boolean negations', function () {
const argv = parser('--no-known-arg --no-unknown-arg', {
boolean: ['known-arg'],
configuration: {
'ignore-unknown-options': true
}
})
argv.should.deep.equal({
_: ['--no-unknown-arg'],
'known-arg': false,
'knownArg': false
})
})
it('should ignore unknown options in long format separated by space', function () {
const argv = parser('--known-arg 1 --unknown-arg 2', {
number: ['known-arg'],
configuration: {
'ignore-unknown-options': true
}
})
argv.should.deep.equal({
_: ['--unknown-arg', 2],
'known-arg': 1,
'knownArg': 1
})
})
it('should ignore unknown options in short dot format separated by equals', function () {
const argv = parser('-k.arg=1 -u.arg=2', {
number: ['k.arg'],
configuration: {
'ignore-unknown-options': true
}
})
argv.should.deep.equal({
_: ['-u.arg=2'],
'k': {
'arg': 1
}
})
})
it('should ignore unknown options in short dot format separated by space', function () {
const argv = parser('-k.arg 1 -u.arg 2', {
number: ['k.arg'],
configuration: {
'ignore-unknown-options': true
}
})
argv.should.deep.equal({
_: ['-u.arg', 2],
'k': {
'arg': 1
}
})
})
it('should ignore unknown options in short format separated by equals', function () {
const argv = parser('-k=1 -u=2', {
number: ['k'],
configuration: {
'ignore-unknown-options': true
}
})
argv.should.deep.equal({
_: ['-u=2'],
'k': 1
})
})
it('should ignore unknown options in short format followed by hyphen', function () {
const argv = parser('-k- -u-', {
string: ['k'],
configuration: {
'ignore-unknown-options': true
}
})
argv.should.deep.equal({
_: ['-u-'],
'k': '-'
})
})
it('should ignore unknown options in short format separated by space', function () {
const argv = parser('-k 1 -u 2', {
number: ['k'],
configuration: {
'ignore-unknown-options': true
}
})
argv.should.deep.equal({
_: ['-u', 2],
'k': 1
})
})
it('should ignore unknown options in short format followed by a number', function () {
const argv = parser('-k1 -u2', {
number: ['k'],
configuration: {
'ignore-unknown-options': true
}
})
argv.should.deep.equal({
_: ['-u2'],
'k': 1
})
})
it('should ignore unknown options in short format followed by a non-word character', function () {
const argv = parser('-k/1/ -u/2/', {
string: ['k'],
configuration: {
'ignore-unknown-options': true
}
})
argv.should.deep.equal({
_: ['-u/2/'],
'k': '/1/'
})
})
})
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'],
configuration: {
'ignore-unknown-options': true
}
})
argv.should.deep.equal({
_: ['-u'],
'k': true,
'v': true
})
it('should ignore unknown options in short format with multiple flags in one argument where an unknown flag is at the end', function () {
const argv = parser('-kvu', {
boolean: ['k', 'v'],
configuration: {
'ignore-unknown-options': true
}
})
argv.should.deep.equal({
_: ['-u'],
'k': true,
'v': true
})
})
})
})

0 comments on commit 0cce777

Please sign in to comment.