Skip to content

Commit

Permalink
feat: when parsing stops, we now populate "--" by default (#88)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: rather than placing arguments in "_", when parsing is stopped via "--"; we now populate an array called "--" by default.
  • Loading branch information
bcoe committed May 1, 2017
1 parent 4a298d5 commit cd666db
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 60 deletions.
36 changes: 19 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,6 @@ yargs engine.
<a name="configuration"></a>
### Options
#### `--`
* default: `false`.
_If disabled:_
```sh
node example.js a -b -- x y
{ _: [ 'a', 'x', 'y' ], b: true }
```
_If enabled:_
```sh
node example.js a -b -- x y
{ _: [ 'a' ], '--': [ 'x', 'y' ], b: true }
```
### Configuration
The yargs-parser applies several automated transformations on the keys provided
Expand Down Expand Up @@ -267,6 +250,25 @@ node example.js -x 1 2 -x 3 4
{ _: [], x: [[1, 2], [3, 4]] }
```
### `populate--`
* default: `true`.
Should unparsed flags be stored in `--` or `_`.
_If disabled:_
```sh
node example.js a -b -- x y
{ _: [ 'a', 'x', 'y' ], b: true }
```
_If enabled:_
```sh
node example.js a -b -- x y
{ _: [ 'a' ], '--': [ 'x', 'y' ], b: true }
```
## Special Thanks
The yargs project evolves from optimist and minimist. It owes its
Expand Down
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ function parse (args, opts) {
'parse-numbers': true,
'boolean-negation': true,
'duplicate-arguments-array': true,
'flatten-duplicate-arrays': true
'flatten-duplicate-arrays': true,
'populate--': true
}, opts.configuration)
var defaults = opts.default || {}
var configObjects = opts.configObjects || []
var envPrefix = opts.envPrefix
var notFlagsOption = opts['--']
var notFlagsOption = configuration['populate--']
var notFlagsArgv = notFlagsOption ? '--' : '_'
var newAliases = {}
// allow a i18n handler to be passed in, default to a fake one (util.format).
Expand Down
82 changes: 41 additions & 41 deletions test/yargs-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ describe('yargs-parser', function () {
'--key', 'value',
'-b', '--bool', '--no-meep', '--multi=baz',
'--', '--not-a-flag', '-', '-h', '-multi', '--', 'eek'
])
], {
configuration: {
'populate--': false
}
})
parse.should.have.property('c', true)
parse.should.have.property('a', true)
parse.should.have.property('t', true)
Expand Down Expand Up @@ -1260,46 +1264,6 @@ describe('yargs-parser', function () {
})
})

describe('option --', function () {
describe('when it is not defined', function () {
it('should not initialize the \'--\' array', function () {
var result = parser([
'bare',
'--', '-h', 'eek', '--'
])
result.should.have.property('_').and.deep.equal(['bare', '-h', 'eek', '--'])
result.should.not.have.property('--')
})
})

describe('when it is defined', function () {
it('should set bare flags to \'_\' array and non-flags to \'--\' array', function () {
var result = parser([
'--name=meowmers', 'bare', '-cats', 'woo', 'moxy',
'-h', 'awesome', '--multi=quux',
'--key', 'value',
'-b', '--bool', '--no-meep', '--multi=baz',
'--', '--not-a-flag', '-', '-h', '-multi', '--', 'eek'
], {
'--': true
})
result.should.have.property('c', true)
result.should.have.property('a', true)
result.should.have.property('t', true)
result.should.have.property('s', 'woo')
result.should.have.property('h', 'awesome')
result.should.have.property('b', true)
result.should.have.property('bool', true)
result.should.have.property('key', 'value')
result.should.have.property('multi').and.deep.equal(['quux', 'baz'])
result.should.have.property('meep', false)
result.should.have.property('name', 'meowmers')
result.should.have.property('_').and.deep.equal(['bare', 'moxy'])
result.should.have.property('--').and.deep.equal(['--not-a-flag', '-', '-h', '-multi', '--', 'eek'])
})
})
})

describe('count', function () {
it('should count the number of times a boolean is present', function () {
var parsed
Expand Down Expand Up @@ -2244,6 +2208,42 @@ describe('yargs-parser', function () {
})
})
})

describe('populate--', function () {
it('should populate "_" if "populate-- false', function () {
var result = parser([
'bare',
'--', '-h', 'eek', '--'
], {
configuration: {'populate--': false}
})
result.should.have.property('_').and.deep.equal(['bare', '-h', 'eek', '--'])
result.should.not.have.property('--')
})

it('should populate the "--" array by default', function () {
var result = parser([
'--name=meowmers', 'bare', '-cats', 'woo', 'moxy',
'-h', 'awesome', '--multi=quux',
'--key', 'value',
'-b', '--bool', '--no-meep', '--multi=baz',
'--', '--not-a-flag', '-', '-h', '-multi', '--', 'eek'
])
result.should.have.property('c', true)
result.should.have.property('a', true)
result.should.have.property('t', true)
result.should.have.property('s', 'woo')
result.should.have.property('h', 'awesome')
result.should.have.property('b', true)
result.should.have.property('bool', true)
result.should.have.property('key', 'value')
result.should.have.property('multi').and.deep.equal(['quux', 'baz'])
result.should.have.property('meep', false)
result.should.have.property('name', 'meowmers')
result.should.have.property('_').and.deep.equal(['bare', 'moxy'])
result.should.have.property('--').and.deep.equal(['--not-a-flag', '-', '-h', '-multi', '--', 'eek'])
})
})
})

// addresses: https://github.com/yargs/yargs-parser/issues/41
Expand Down

0 comments on commit cd666db

Please sign in to comment.