Skip to content

Commit

Permalink
feat: allow configuration of prefix for boolean negation (yargs#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
yitzchak authored and bcoe committed Aug 10, 2017
1 parent 8ae91cd commit 00bde7d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
19 changes: 19 additions & 0 deletions README.md
Expand Up @@ -250,6 +250,25 @@ node example.js -x 1 2 -x 3 4
{ _: [], x: [[1, 2], [3, 4]] }
```
### negation prefix
* default: `no-`
* key: `negation-prefix`
The prefix to use for negated boolean variables.
```sh
node example.js --no-foo
{ _: [], foo: false }
```
_if set to `quux`:_
```sh
node example.js --quuxfoo
{ _: [], foo: false }
```
### populate --
* default: `false`.
Expand Down
6 changes: 4 additions & 2 deletions index.js
Expand Up @@ -16,6 +16,7 @@ function parse (args, opts) {
'dot-notation': true,
'parse-numbers': true,
'boolean-negation': true,
'negation-prefix': 'no-',
'duplicate-arguments-array': true,
'flatten-duplicate-arrays': true,
'populate--': false
Expand Down Expand Up @@ -45,6 +46,7 @@ function parse (args, opts) {
coercions: {}
}
var negative = /^-[0-9]+(\.[0-9]+)?/
var negatedBoolean = new RegExp('^--' + configuration['negation-prefix'] + '(.+)')

;[].concat(opts.array).filter(Boolean).forEach(function (key) {
flags.arrays[key] = true
Expand Down Expand Up @@ -141,8 +143,8 @@ function parse (args, opts) {
} else {
setArg(m[1], m[2])
}
} else if (arg.match(/^--no-.+/) && configuration['boolean-negation']) {
key = arg.match(/^--no-(.+)/)[1]
} else if (arg.match(negatedBoolean) && configuration['boolean-negation']) {
key = arg.match(negatedBoolean)[1]
setArg(key, false)

// -- seperated by space.
Expand Down
10 changes: 10 additions & 0 deletions test/yargs-parser.js
Expand Up @@ -1968,6 +1968,16 @@ describe('yargs-parser', function () {
parsed['no-dice'].should.equal(true)
expect(parsed.dice).to.equal(undefined)
})

it('negates boolean arguments with correct prefix', function () {
var parsed = parser(['--foodice'], {
configuration: {
'negation-prefix': 'foo'
}
})

expect(parsed['dice']).to.equal(false)
})
})

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

0 comments on commit 00bde7d

Please sign in to comment.