Skip to content

Commit 00bde7d

Browse files
yitzchakbcoe
authored andcommitted
feat: allow configuration of prefix for boolean negation (#94)
1 parent 8ae91cd commit 00bde7d

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,25 @@ node example.js -x 1 2 -x 3 4
250250
{ _: [], x: [[1, 2], [3, 4]] }
251251
```
252252
253+
### negation prefix
254+
255+
* default: `no-`
256+
* key: `negation-prefix`
257+
258+
The prefix to use for negated boolean variables.
259+
260+
```sh
261+
node example.js --no-foo
262+
{ _: [], foo: false }
263+
```
264+
265+
_if set to `quux`:_
266+
267+
```sh
268+
node example.js --quuxfoo
269+
{ _: [], foo: false }
270+
```
271+
253272
### populate --
254273
255274
* default: `false`.

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ function parse (args, opts) {
1616
'dot-notation': true,
1717
'parse-numbers': true,
1818
'boolean-negation': true,
19+
'negation-prefix': 'no-',
1920
'duplicate-arguments-array': true,
2021
'flatten-duplicate-arrays': true,
2122
'populate--': false
@@ -45,6 +46,7 @@ function parse (args, opts) {
4546
coercions: {}
4647
}
4748
var negative = /^-[0-9]+(\.[0-9]+)?/
49+
var negatedBoolean = new RegExp('^--' + configuration['negation-prefix'] + '(.+)')
4850

4951
;[].concat(opts.array).filter(Boolean).forEach(function (key) {
5052
flags.arrays[key] = true
@@ -141,8 +143,8 @@ function parse (args, opts) {
141143
} else {
142144
setArg(m[1], m[2])
143145
}
144-
} else if (arg.match(/^--no-.+/) && configuration['boolean-negation']) {
145-
key = arg.match(/^--no-(.+)/)[1]
146+
} else if (arg.match(negatedBoolean) && configuration['boolean-negation']) {
147+
key = arg.match(negatedBoolean)[1]
146148
setArg(key, false)
147149

148150
// -- seperated by space.

test/yargs-parser.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1968,6 +1968,16 @@ describe('yargs-parser', function () {
19681968
parsed['no-dice'].should.equal(true)
19691969
expect(parsed.dice).to.equal(undefined)
19701970
})
1971+
1972+
it('negates boolean arguments with correct prefix', function () {
1973+
var parsed = parser(['--foodice'], {
1974+
configuration: {
1975+
'negation-prefix': 'foo'
1976+
}
1977+
})
1978+
1979+
expect(parsed['dice']).to.equal(false)
1980+
})
19711981
})
19721982

19731983
describe('duplicate arguments array', function () {

0 commit comments

Comments
 (0)