Skip to content

Commit

Permalink
don't coerce number, change to "parse-unknown-options: false", fix no…
Browse files Browse the repository at this point in the history
…de 6 support
  • Loading branch information
henderea committed Jun 14, 2019
1 parent df705f8 commit c872e66
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
39 changes: 20 additions & 19 deletions index.js
Expand Up @@ -27,7 +27,7 @@ function parse (args, opts) {
'halt-at-non-option': false,
'strip-aliased': false,
'strip-dashed': false,
'ignore-unknown-options': false
'parse-unknown-options': true
}, opts.configuration)
var defaults = opts.default || {}
var configObjects = opts.configObjects || []
Expand Down Expand Up @@ -169,17 +169,17 @@ 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 if (!configuration['ignore-unknown-options'] || hasAnyFlag(m[1])) {
} else if (configuration['parse-unknown-options'] || hasAnyFlag(m[1])) {
setArg(m[1], m[2])
} else {
argv._.push(maybeCoerceNumber('_', arg))
argv._.push(arg)
}
} else if (arg.match(negatedBoolean) && configuration['boolean-negation']) {
key = arg.match(negatedBoolean)[1]
if (!configuration['ignore-unknown-options'] || hasAnyFlag(key)) {
if (configuration['parse-unknown-options'] || hasAnyFlag(key)) {
setArg(key, false)
} else {
argv._.push(maybeCoerceNumber('_', arg))
argv._.push(arg)
}

// -- seperated by space.
Expand All @@ -194,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 if (!configuration['ignore-unknown-options'] || hasAnyFlag(key)) {
} else if (configuration['parse-unknown-options'] || hasAnyFlag(key)) {
next = flags.nargs[key] === 0 ? undefined : args[i + 1]

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

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

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

if (!configuration['ignore-unknown-options'] || hasAnyFlag(key)) {
if (configuration['parse-unknown-options'] || hasAnyFlag(key)) {
if (next !== undefined && !next.match(/^-/) &&
!checkAllAliases(key, flags.bools) &&
!checkAllAliases(key, flags.counts)) {
Expand All @@ -237,7 +237,7 @@ function parse (args, opts) {
setArg(key, defaultValue(key))
}
} else {
argv._.push(maybeCoerceNumber('_', arg))
argv._.push(arg)
}
} else if (arg.match(/^-[^-]+/) && !arg.match(negative)) {
letters = arg.slice(1, -1).split('')
Expand All @@ -259,7 +259,7 @@ 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 if (!configuration['ignore-unknown-options'] || hasAnyFlag(key)) {
} else if (configuration['parse-unknown-options'] || hasAnyFlag(key)) {
setArg(key, value)
} else {
unmatched.push(key)
Expand All @@ -272,7 +272,7 @@ function parse (args, opts) {
}

if (next === '-') {
if (!configuration['ignore-unknown-options'] || hasAnyFlag(letters[j])) {
if (configuration['parse-unknown-options'] || hasAnyFlag(letters[j])) {
setArg(letters[j], next)
} else {
unmatched.push(letters[j])
Expand All @@ -284,7 +284,7 @@ function parse (args, opts) {
// 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)) {
if (!configuration['ignore-unknown-options'] || hasAnyFlag(letters[j])) {
if (configuration['parse-unknown-options'] || hasAnyFlag(letters[j])) {
setArg(letters[j], next)
} else {
unmatched.push(letters[j])
Expand All @@ -295,15 +295,15 @@ function parse (args, opts) {
}

if (letters[j + 1] && letters[j + 1].match(/\W/)) {
if (!configuration['ignore-unknown-options'] || hasAnyFlag(letters[j])) {
if (configuration['parse-unknown-options'] || hasAnyFlag(letters[j])) {
setArg(letters[j], next)
} else {
unmatched.push(letters[j])
unmatched.push(next)
}
broken = true
break
} else if (!configuration['ignore-unknown-options'] || hasAnyFlag(letters[j])) {
} else if (configuration['parse-unknown-options'] || hasAnyFlag(letters[j])) {
setArg(letters[j], defaultValue(letters[j]))
} else {
unmatched.push(letters[j])
Expand All @@ -319,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 if (!configuration['ignore-unknown-options'] || hasAnyFlag(key)) {
} else if (configuration['parse-unknown-options'] || hasAnyFlag(key)) {
next = args[i + 1]

if (next !== undefined && (!/^(-|--)[^-]/.test(next) ||
Expand All @@ -339,7 +339,7 @@ function parse (args, opts) {
}
}
if (unmatched.length > 0) {
argv._.push(maybeCoerceNumber('_', ['-', ...unmatched].join('')))
argv._.push(['-', ...unmatched].join(''))
}
} else if (arg === '--') {
notFlags = args.slice(i + 1)
Expand Down Expand Up @@ -808,7 +808,8 @@ function parse (args, opts) {

function hasAnyFlag (key) {
var isSet = false
var toCheck = [].concat(Object.values(flags))
// XXX Switch to [].concat(...Object.values(flags)) once node.js 6 is dropped
var toCheck = [].concat(...Object.keys(flags).map(k => flags[k]))

toCheck.forEach(function (flag) {
if (flag[key]) isSet = flag[key]
Expand Down
26 changes: 13 additions & 13 deletions test/yargs-parser.js
Expand Up @@ -2963,12 +2963,12 @@ describe('yargs-parser', function () {
})
})
})
describe('ignore-unknown-options', function () {
describe('parse-unknown-options = false', 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
'parse-unknown-options': false
}
})
argv.should.deep.equal({
Expand All @@ -2981,7 +2981,7 @@ describe('yargs-parser', function () {
const argv = parser('--no-known-arg --no-unknown-arg', {
boolean: ['known-arg'],
configuration: {
'ignore-unknown-options': true
'parse-unknown-options': false
}
})
argv.should.deep.equal({
Expand All @@ -2994,7 +2994,7 @@ describe('yargs-parser', function () {
const argv = parser('--known-arg 1 --unknown-arg 2', {
number: ['known-arg'],
configuration: {
'ignore-unknown-options': true
'parse-unknown-options': false
}
})
argv.should.deep.equal({
Expand All @@ -3007,7 +3007,7 @@ describe('yargs-parser', function () {
const argv = parser('-k.arg=1 -u.arg=2', {
number: ['k.arg'],
configuration: {
'ignore-unknown-options': true
'parse-unknown-options': false
}
})
argv.should.deep.equal({
Expand All @@ -3021,7 +3021,7 @@ describe('yargs-parser', function () {
const argv = parser('-k.arg 1 -u.arg 2', {
number: ['k.arg'],
configuration: {
'ignore-unknown-options': true
'parse-unknown-options': false
}
})
argv.should.deep.equal({
Expand All @@ -3035,7 +3035,7 @@ describe('yargs-parser', function () {
const argv = parser('-k=1 -u=2', {
number: ['k'],
configuration: {
'ignore-unknown-options': true
'parse-unknown-options': false
}
})
argv.should.deep.equal({
Expand All @@ -3047,7 +3047,7 @@ describe('yargs-parser', function () {
const argv = parser('-k- -u-', {
string: ['k'],
configuration: {
'ignore-unknown-options': true
'parse-unknown-options': false
}
})
argv.should.deep.equal({
Expand All @@ -3059,7 +3059,7 @@ describe('yargs-parser', function () {
const argv = parser('-k 1 -u 2', {
number: ['k'],
configuration: {
'ignore-unknown-options': true
'parse-unknown-options': false
}
})
argv.should.deep.equal({
Expand All @@ -3071,7 +3071,7 @@ describe('yargs-parser', function () {
const argv = parser('-k1 -u2', {
number: ['k'],
configuration: {
'ignore-unknown-options': true
'parse-unknown-options': false
}
})
argv.should.deep.equal({
Expand All @@ -3083,7 +3083,7 @@ describe('yargs-parser', function () {
const argv = parser('-k/1/ -u/2/', {
string: ['k'],
configuration: {
'ignore-unknown-options': true
'parse-unknown-options': false
}
})
argv.should.deep.equal({
Expand All @@ -3096,7 +3096,7 @@ describe('yargs-parser', function () {
const argv = parser('-kuv', {
boolean: ['k', 'v'],
configuration: {
'ignore-unknown-options': true
'parse-unknown-options': false
}
})
argv.should.deep.equal({
Expand All @@ -3108,7 +3108,7 @@ describe('yargs-parser', function () {
const argv = parser('-kvu', {
boolean: ['k', 'v'],
configuration: {
'ignore-unknown-options': true
'parse-unknown-options': false
}
})
argv.should.deep.equal({
Expand Down

0 comments on commit c872e66

Please sign in to comment.