Skip to content

Commit 2572ca8

Browse files
ruimarquesbcoe
authored andcommitted
feat: add -- option which allows arguments after the -- flag to be returned separated from positional arguments (#84)
1 parent b0dc63d commit 2572ca8

3 files changed

Lines changed: 67 additions & 1 deletion

File tree

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,14 @@ Parses command line arguments returning a simple mapping of keys and values.
7272
* `opts.string`: keys should be treated as strings (even if they resemble a number `-x 33`).
7373
* `opts.configuration`: provide configuration options to the yargs-parser (see: [configuration](#configuration)).
7474
* `opts.number`: keys should be treated as numbers.
75+
* `opts['--']`: arguments after the end-of-options flag `--` will be set to the `argv.['--']` array instead of being set to the `argv._` array.
7576
7677
**returns:**
7778
7879
* `obj`: an object representing the parsed value of `args`
7980
* `key/value`: key value pairs for each argument and their aliases.
8081
* `_`: an array representing the positional arguments.
82+
* [optional] `--`: an array with arguments after the end-of-options flag `--`.
8183
8284
### require('yargs-parser').detailed(args, opts={})
8385
@@ -100,6 +102,24 @@ yargs engine.
100102
* `configuration`: the configuration loaded from the `yargs` stanza in package.json.
101103
102104
<a name="configuration"></a>
105+
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+
```
103123
### Configuration
104124
105125
The yargs-parser applies several automated transformations on the keys provided

index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ function parse (args, opts) {
2222
var defaults = opts.default || {}
2323
var configObjects = opts.configObjects || []
2424
var envPrefix = opts.envPrefix
25+
var notFlagsOption = opts['--']
26+
var notFlagsArgv = notFlagsOption ? '--' : '_'
2527
var newAliases = {}
2628
// allow a i18n handler to be passed in, default to a fake one (util.format).
2729
var __ = opts.__ || function (str) {
@@ -98,6 +100,10 @@ function parse (args, opts) {
98100

99101
var argv = { _: [] }
100102

103+
if (notFlagsOption) {
104+
argv[notFlagsArgv] = []
105+
}
106+
101107
Object.keys(flags.bools).forEach(function (key) {
102108
setArg(key, !(key in defaults) ? false : defaults[key])
103109
setDefaulted(key)
@@ -290,7 +296,7 @@ function parse (args, opts) {
290296
})
291297

292298
notFlags.forEach(function (key) {
293-
argv._.push(key)
299+
argv[notFlagsArgv].push(key)
294300
})
295301

296302
// how many arguments should we consume, based

test/yargs-parser.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,46 @@ describe('yargs-parser', function () {
12601260
})
12611261
})
12621262

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+
12631303
describe('count', function () {
12641304
it('should count the number of times a boolean is present', function () {
12651305
var parsed

0 commit comments

Comments
 (0)