Skip to content
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

feat!: populate error if incompatible narg/count or array/count options are used #191

Merged
merged 2 commits into from Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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