Skip to content

Commit

Permalink
feat(config): If invoking .config() without parameters, set a default…
Browse files Browse the repository at this point in the history
… option
  • Loading branch information
elas7 authored and bcoe committed Apr 4, 2016
1 parent e6f957b commit 0413dd1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -622,13 +622,15 @@ var argv = require('yargs')
.argv;
```

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

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
and parsed, and its properties are set as arguments.

If invoked without parameters, `.config()` will make `--config` the option to pass the JSON config file.

An optional `description` can be provided to customize the config (`key`) option
in the usage string.

Expand Down
18 changes: 18 additions & 0 deletions test/validation.js
Expand Up @@ -240,6 +240,24 @@ describe('validation tests', function () {
])
})

it('should be displayed in the help message with its default name', function () {
var checkUsage = require('./helpers/utils').checkOutput
var r = checkUsage(function () {
return yargs(['--help'])
.config()
.help('help')
.wrap(null)
.argv
})
r.should.have.property('logs').with.length(1)
r.logs.join('\n').split(/\n+/).should.deep.equal([
'Options:',
' --config Path to JSON config file',
' --help Show help [boolean]',
''
])
})

it('should allow help message to be overridden', function () {
var checkUsage = require('./helpers/utils').checkOutput
var r = checkUsage(function () {
Expand Down
2 changes: 2 additions & 0 deletions yargs.js
Expand Up @@ -179,6 +179,8 @@ function Yargs (processArgs, cwd, parentRequire) {
parseFn = msg
msg = null
}

key = key || 'config'
self.describe(key, msg || usage.deferY18nLookup('Path to JSON config file'))
;(Array.isArray(key) ? key : [key]).forEach(function (k) {
options.config[k] = parseFn || true
Expand Down

0 comments on commit 0413dd1

Please sign in to comment.