Skip to content

Commit 0413dd1

Browse files
elas7bcoe
authored andcommitted
feat(config): If invoking .config() without parameters, set a default option
1 parent e6f957b commit 0413dd1

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,13 +622,15 @@ var argv = require('yargs')
622622
.argv;
623623
```
624624

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

628628
Tells the parser that if the option specified by `key` is passed in, it
629629
should be interpreted as a path to a JSON config file. The file is loaded
630630
and parsed, and its properties are set as arguments.
631631

632+
If invoked without parameters, `.config()` will make `--config` the option to pass the JSON config file.
633+
632634
An optional `description` can be provided to customize the config (`key`) option
633635
in the usage string.
634636

test/validation.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,24 @@ describe('validation tests', function () {
240240
])
241241
})
242242

243+
it('should be displayed in the help message with its default name', function () {
244+
var checkUsage = require('./helpers/utils').checkOutput
245+
var r = checkUsage(function () {
246+
return yargs(['--help'])
247+
.config()
248+
.help('help')
249+
.wrap(null)
250+
.argv
251+
})
252+
r.should.have.property('logs').with.length(1)
253+
r.logs.join('\n').split(/\n+/).should.deep.equal([
254+
'Options:',
255+
' --config Path to JSON config file',
256+
' --help Show help [boolean]',
257+
''
258+
])
259+
})
260+
243261
it('should allow help message to be overridden', function () {
244262
var checkUsage = require('./helpers/utils').checkOutput
245263
var r = checkUsage(function () {

yargs.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ function Yargs (processArgs, cwd, parentRequire) {
179179
parseFn = msg
180180
msg = null
181181
}
182+
183+
key = key || 'config'
182184
self.describe(key, msg || usage.deferY18nLookup('Path to JSON config file'))
183185
;(Array.isArray(key) ? key : [key]).forEach(function (k) {
184186
options.config[k] = parseFn || true

0 commit comments

Comments
 (0)