Skip to content

Commit

Permalink
fix: add config lookup for .implies() (#556)
Browse files Browse the repository at this point in the history
* WIP: add config lookup for .implies()

* further improvements for making implies() config aware

* directly look up configuration via yargs object again

* tune logic in validation.js to check for configuration

* fix config lookup for .implies() + test
  • Loading branch information
maxrimue authored and bcoe committed Jul 16, 2016
1 parent 3c2e479 commit 8d7585c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
10 changes: 8 additions & 2 deletions lib/validation.js
Expand Up @@ -219,6 +219,12 @@ module.exports = function (yargs, usage, y18n) {
const implyFail = []

Object.keys(implied).forEach(function (key) {
var booleanNegation
if (yargs.getOptions().configuration['boolean-negation'] === false) {
booleanNegation = false
} else {
booleanNegation = true
}
var num
const origKey = key
var value = implied[key]
Expand All @@ -230,7 +236,7 @@ module.exports = function (yargs, usage, y18n) {
if (typeof key === 'number') {
// check length of argv._
key = argv._.length >= key
} else if (key.match(/^--no-.+/)) {
} else if (key.match(/^--no-.+/) && booleanNegation) {
// check if key doesn't exist
key = key.match(/^--no-(.+)/)[1]
key = !argv[key]
Expand All @@ -244,7 +250,7 @@ module.exports = function (yargs, usage, y18n) {

if (typeof value === 'number') {
value = argv._.length >= value
} else if (value.match(/^--no-.+/)) {
} else if (value.match(/^--no-.+/) && booleanNegation) {
value = value.match(/^--no-(.+)/)[1]
value = !argv[value]
} else {
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/package.json
Expand Up @@ -4,6 +4,7 @@
"type": "svn"
},
"yargs": {
"dot-notation": false
"dot-notation": false,
"boolean-negation": false
}
}
12 changes: 12 additions & 0 deletions test/validation.js
Expand Up @@ -57,6 +57,18 @@ describe('validation tests', function () {
})
.argv
})

it('does not treat --no- as a special case if boolean negation is disabled', function (done) {
yargs(['--foo'], './test/fixtures')
.implies({
'foo': '--no-foo'
})
.fail(function (msg) {
msg.should.match(/Implications failed/)
return done()
})
.argv
})
})

describe('demand', function () {
Expand Down
2 changes: 1 addition & 1 deletion yargs.js
Expand Up @@ -636,7 +636,7 @@ function Yargs (processArgs, cwd, parentRequire) {

function parseArgs (args, shortCircuit) {
options.__ = y18n.__
options.configuration = pkgUp()['yargs'] || {}
options.configuration = pkgUp(cwd)['yargs'] || {}
const parsed = Parser.detailed(args, options)
const argv = parsed.argv
var aliases = parsed.aliases
Expand Down

0 comments on commit 8d7585c

Please sign in to comment.