Skip to content

Commit cd666db

Browse files
authored
feat: when parsing stops, we now populate "--" by default (#88)
BREAKING CHANGE: rather than placing arguments in "_", when parsing is stopped via "--"; we now populate an array called "--" by default.
1 parent 4a298d5 commit cd666db

File tree

3 files changed

+63
-60
lines changed

3 files changed

+63
-60
lines changed

README.md

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -103,23 +103,6 @@ yargs engine.
103103
104104
<a name="configuration"></a>
105105
106-
### Options
107-
108-
#### `--`
109-
* default: `false`.
110-
111-
_If disabled:_
112-
```sh
113-
node example.js a -b -- x y
114-
{ _: [ 'a', 'x', 'y' ], b: true }
115-
```
116-
117-
_If enabled:_
118-
119-
```sh
120-
node example.js a -b -- x y
121-
{ _: [ 'a' ], '--': [ 'x', 'y' ], b: true }
122-
```
123106
### Configuration
124107
125108
The yargs-parser applies several automated transformations on the keys provided
@@ -267,6 +250,25 @@ node example.js -x 1 2 -x 3 4
267250
{ _: [], x: [[1, 2], [3, 4]] }
268251
```
269252
253+
### `populate--`
254+
255+
* default: `true`.
256+
257+
Should unparsed flags be stored in `--` or `_`.
258+
259+
_If disabled:_
260+
```sh
261+
node example.js a -b -- x y
262+
{ _: [ 'a', 'x', 'y' ], b: true }
263+
```
264+
265+
_If enabled:_
266+
267+
```sh
268+
node example.js a -b -- x y
269+
{ _: [ 'a' ], '--': [ 'x', 'y' ], b: true }
270+
```
271+
270272
## Special Thanks
271273
272274
The yargs project evolves from optimist and minimist. It owes its

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ function parse (args, opts) {
1717
'parse-numbers': true,
1818
'boolean-negation': true,
1919
'duplicate-arguments-array': true,
20-
'flatten-duplicate-arrays': true
20+
'flatten-duplicate-arrays': true,
21+
'populate--': true
2122
}, opts.configuration)
2223
var defaults = opts.default || {}
2324
var configObjects = opts.configObjects || []
2425
var envPrefix = opts.envPrefix
25-
var notFlagsOption = opts['--']
26+
var notFlagsOption = configuration['populate--']
2627
var notFlagsArgv = notFlagsOption ? '--' : '_'
2728
var newAliases = {}
2829
// allow a i18n handler to be passed in, default to a fake one (util.format).

test/yargs-parser.js

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ describe('yargs-parser', function () {
9494
'--key', 'value',
9595
'-b', '--bool', '--no-meep', '--multi=baz',
9696
'--', '--not-a-flag', '-', '-h', '-multi', '--', 'eek'
97-
])
97+
], {
98+
configuration: {
99+
'populate--': false
100+
}
101+
})
98102
parse.should.have.property('c', true)
99103
parse.should.have.property('a', true)
100104
parse.should.have.property('t', true)
@@ -1260,46 +1264,6 @@ describe('yargs-parser', function () {
12601264
})
12611265
})
12621266

1263-
describe('option --', function () {
1264-
describe('when it is not defined', function () {
1265-
it('should not initialize the \'--\' array', function () {
1266-
var result = parser([
1267-
'bare',
1268-
'--', '-h', 'eek', '--'
1269-
])
1270-
result.should.have.property('_').and.deep.equal(['bare', '-h', 'eek', '--'])
1271-
result.should.not.have.property('--')
1272-
})
1273-
})
1274-
1275-
describe('when it is defined', function () {
1276-
it('should set bare flags to \'_\' array and non-flags to \'--\' array', function () {
1277-
var result = parser([
1278-
'--name=meowmers', 'bare', '-cats', 'woo', 'moxy',
1279-
'-h', 'awesome', '--multi=quux',
1280-
'--key', 'value',
1281-
'-b', '--bool', '--no-meep', '--multi=baz',
1282-
'--', '--not-a-flag', '-', '-h', '-multi', '--', 'eek'
1283-
], {
1284-
'--': true
1285-
})
1286-
result.should.have.property('c', true)
1287-
result.should.have.property('a', true)
1288-
result.should.have.property('t', true)
1289-
result.should.have.property('s', 'woo')
1290-
result.should.have.property('h', 'awesome')
1291-
result.should.have.property('b', true)
1292-
result.should.have.property('bool', true)
1293-
result.should.have.property('key', 'value')
1294-
result.should.have.property('multi').and.deep.equal(['quux', 'baz'])
1295-
result.should.have.property('meep', false)
1296-
result.should.have.property('name', 'meowmers')
1297-
result.should.have.property('_').and.deep.equal(['bare', 'moxy'])
1298-
result.should.have.property('--').and.deep.equal(['--not-a-flag', '-', '-h', '-multi', '--', 'eek'])
1299-
})
1300-
})
1301-
})
1302-
13031267
describe('count', function () {
13041268
it('should count the number of times a boolean is present', function () {
13051269
var parsed
@@ -2244,6 +2208,42 @@ describe('yargs-parser', function () {
22442208
})
22452209
})
22462210
})
2211+
2212+
describe('populate--', function () {
2213+
it('should populate "_" if "populate-- false', function () {
2214+
var result = parser([
2215+
'bare',
2216+
'--', '-h', 'eek', '--'
2217+
], {
2218+
configuration: {'populate--': false}
2219+
})
2220+
result.should.have.property('_').and.deep.equal(['bare', '-h', 'eek', '--'])
2221+
result.should.not.have.property('--')
2222+
})
2223+
2224+
it('should populate the "--" array by default', function () {
2225+
var result = parser([
2226+
'--name=meowmers', 'bare', '-cats', 'woo', 'moxy',
2227+
'-h', 'awesome', '--multi=quux',
2228+
'--key', 'value',
2229+
'-b', '--bool', '--no-meep', '--multi=baz',
2230+
'--', '--not-a-flag', '-', '-h', '-multi', '--', 'eek'
2231+
])
2232+
result.should.have.property('c', true)
2233+
result.should.have.property('a', true)
2234+
result.should.have.property('t', true)
2235+
result.should.have.property('s', 'woo')
2236+
result.should.have.property('h', 'awesome')
2237+
result.should.have.property('b', true)
2238+
result.should.have.property('bool', true)
2239+
result.should.have.property('key', 'value')
2240+
result.should.have.property('multi').and.deep.equal(['quux', 'baz'])
2241+
result.should.have.property('meep', false)
2242+
result.should.have.property('name', 'meowmers')
2243+
result.should.have.property('_').and.deep.equal(['bare', 'moxy'])
2244+
result.should.have.property('--').and.deep.equal(['--not-a-flag', '-', '-h', '-multi', '--', 'eek'])
2245+
})
2246+
})
22472247
})
22482248

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

0 commit comments

Comments
 (0)