Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat!: populate error if incompatible narg/count or array/count optio…
…ns are used (#191)
  • Loading branch information
juergba authored and bcoe committed Oct 29, 2019
1 parent 212814f commit 84a401f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
16 changes: 16 additions & 0 deletions index.js
Expand Up @@ -131,6 +131,8 @@ function parse (args, opts) {
})
})

checkConfiguration()

var argv = { _: [] }
var notFlags = []

Expand Down Expand Up @@ -874,6 +876,20 @@ function parse (args, opts) {
return num === undefined
}

// check user configuration settings for inconsistencies
function checkConfiguration () {
// count keys should not be set as array/narg
Object.keys(flags.counts).find(key => {
if (checkAllAliases(key, flags.arrays)) {
error = Error(__('Invalid configuration: %s, opts.count excludes opts.array.', key))
return true
} else if (checkAllAliases(key, flags.nargs)) {
error = Error(__('Invalid configuration: %s, opts.count excludes opts.narg.', key))
return true
}
})
}

return {
argv: argv,
error: error,
Expand Down
18 changes: 18 additions & 0 deletions test/yargs-parser.js
Expand Up @@ -1487,6 +1487,24 @@ describe('yargs-parser', function () {
], { count: 'v' })
parsed.v.should.equal(8)
})

it('should add an error if counter is also set as array', function () {
var argv = parser.detailed(['--counter', '--counter', '--counter'], {
count: ['counter'],
array: ['counter']
})

argv.error.message.should.equal('Invalid configuration: counter, opts.count excludes opts.array.')
})

it('should add an error if counter is also set as narg', function () {
var argv = parser.detailed(['--counter', 'foo', 'bar'], {
count: ['counter'],
narg: { 'counter': 2 }
})

argv.error.message.should.equal('Invalid configuration: counter, opts.count excludes opts.narg.')
})
})

describe('array', function () {
Expand Down

0 comments on commit 84a401f

Please sign in to comment.