Skip to content

Commit

Permalink
feat(configuration): Allow to directly pass a configuration object to…
Browse files Browse the repository at this point in the history
… .config() (#480)
  • Loading branch information
elas7 authored and bcoe committed May 1, 2016
1 parent 44883f0 commit e0a7e05
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
22 changes: 21 additions & 1 deletion README.md
Expand Up @@ -650,7 +650,9 @@ var argv = require('yargs')
```

<a name="config"></a>.config([key], [description], [parseFn])
------------
-------------------------------------------------------------
.config(object)
---------------

Tells the parser that if the option specified by `key` is passed in, it
should be interpreted as a path to a JSON config file. The file is loaded
Expand All @@ -673,6 +675,24 @@ var argv = require('yargs')
.argv
```

You can also pass an explicit configuration `object`, it will be parsed
and its properties will be set as arguments.

```js
var argv = require('yargs')
.config({foo: 1, bar: 2})
.argv
console.log(argv)
```

```
$ node test.js
{ _: [],
foo: 1,
bar: 2,
'$0': 'test.js' }
```

<a name="count"></a>.count(key)
------------

Expand Down
9 changes: 9 additions & 0 deletions test/yargs.js
Expand Up @@ -683,6 +683,15 @@ describe('yargs dsl tests', function () {

argv.foo.should.equal('baz')
})

it('allows to pass a configuration object', function () {
var argv = yargs
.config({foo: 1, bar: 2})
.argv

argv.foo.should.equal(1)
argv.bar.should.equal(2)
})
})

describe('normalize', function () {
Expand Down
7 changes: 7 additions & 0 deletions yargs.js
Expand Up @@ -175,6 +175,13 @@ function Yargs (processArgs, cwd, parentRequire) {
}

self.config = function (key, msg, parseFn) {
// allow to pass a configuration object
if (typeof key === 'object') {
options.configObjects = (options.configObjects || []).concat(key)
return self
}

// allow to provide a parsing function
if (typeof msg === 'function') {
parseFn = msg
msg = null
Expand Down

0 comments on commit e0a7e05

Please sign in to comment.