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

Allow configuration of prefix for boolean negation #94

Merged
merged 3 commits into from
Aug 10, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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