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

Add -- option which controls how arguments after -- flag are returned. #84

Merged
merged 1 commit into from
Apr 15, 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
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@ Parses command line arguments returning a simple mapping of keys and values.
* `opts.string`: keys should be treated as strings (even if they resemble a number `-x 33`).
* `opts.configuration`: provide configuration options to the yargs-parser (see: [configuration](#configuration)).
* `opts.number`: keys should be treated as numbers.
* `opts['--']`: arguments after the end-of-options flag `--` will be set to the `argv.['--']` array instead of being set to the `argv._` array.

**returns:**

* `obj`: an object representing the parsed value of `args`
* `key/value`: key value pairs for each argument and their aliases.
* `_`: an array representing the positional arguments.
* [optional] `--`: an array with arguments after the end-of-options flag `--`.

### require('yargs-parser').detailed(args, opts={})

Expand All @@ -100,6 +102,24 @@ yargs engine.
* `configuration`: the configuration loaded from the `yargs` stanza in package.json.

<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
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ function parse (args, opts) {
var defaults = opts.default || {}
var configObjects = opts.configObjects || []
var envPrefix = opts.envPrefix
var notFlagsOption = opts['--']
var notFlagsArgv = notFlagsOption ? '--' : '_'
var newAliases = {}
// allow a i18n handler to be passed in, default to a fake one (util.format).
var __ = opts.__ || function (str) {
Expand Down Expand Up @@ -98,6 +100,10 @@ function parse (args, opts) {

var argv = { _: [] }

if (notFlagsOption) {
argv[notFlagsArgv] = []
}

Object.keys(flags.bools).forEach(function (key) {
setArg(key, !(key in defaults) ? false : defaults[key])
setDefaulted(key)
Expand Down Expand Up @@ -290,7 +296,7 @@ function parse (args, opts) {
})

notFlags.forEach(function (key) {
argv._.push(key)
argv[notFlagsArgv].push(key)
})

// how many arguments should we consume, based
Expand Down
40 changes: 40 additions & 0 deletions test/yargs-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,46 @@ 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