Skip to content

Commit 3c6869a

Browse files
jean-emmanuelbcoe
authored andcommittedJan 30, 2019
feat: Add .parserConfiguration() method, deprecating package.json config (#1262)
BREAKING CHANGE: we now warn if the yargs stanza package.json is used.
1 parent da75ea2 commit 3c6869a

File tree

5 files changed

+49
-1
lines changed

5 files changed

+49
-1
lines changed
 

‎docs/api.md

+4
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,10 @@ If the arguments have been parsed, this contain detailed parsed arguments. See
11491149
the documentation in [yargs-parser `.detailed()`][https://github.com/yargs/yargs-parser/blob/master/README.md#requireyargs-parserdetailedargs-opts]
11501150
for details of this object
11511151

1152+
<a name="parserConfiguration"></a>.parserConfiguration(obj)
1153+
------------
1154+
`parserConfiguration()` allows you to configure yargs-parser, see [yargs-parser's configuration](https://github.com/yargs/yargs-parser#configuration).
1155+
11521156
<a name="pkg-conf"></a>
11531157
.pkgConf(key, [cwd])
11541158
------------

‎test/fixtures/configured-bin.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env node
2+
var argv = require('./yargs/index.js')
3+
.parserConfiguration({'dot-notation': true})
4+
.help('help')
5+
.version()
6+
.argv
7+
console.log(argv)

‎test/integration.js

+12
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,18 @@ describe('integration tests', () => {
191191
return done()
192192
})
193193
})
194+
195+
it('is overridden by yargs.parserConfiguration', (done) => {
196+
testCmd('./configured-bin.js', [ '--foo.bar', '--no-baz' ], (code, stdout) => {
197+
if (code) {
198+
return done(new Error(`cmd exited with code ${code}`))
199+
}
200+
201+
stdout.should.not.match(/foo\.bar/)
202+
stdout.should.match(/noBaz/)
203+
return done()
204+
})
205+
})
194206
})
195207

196208
after(() => {

‎test/yargs.js

+10
Original file line numberDiff line numberDiff line change
@@ -1612,6 +1612,16 @@ describe('yargs dsl tests', () => {
16121612
})
16131613
})
16141614

1615+
describe('parserConfiguration', () => {
1616+
it('overrides the default parser configuration ', () => {
1617+
const argv = yargs('--foo.bar 1 --no-baz 2')
1618+
.parserConfiguration({'boolean-negation': false, 'dot-notation': false})
1619+
.parse()
1620+
expect(argv['foo.bar']).to.equal(1)
1621+
argv.noBaz.should.equal(2)
1622+
})
1623+
})
1624+
16151625
describe('skipValidation', () => {
16161626
it('skips validation if an option with skipValidation is present', () => {
16171627
const argv = yargs(['--koala', '--skip'])

‎yargs.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,14 @@ function Yargs (processArgs, cwd, parentRequire) {
765765
}
766766
self.getStrict = () => strict
767767

768+
let parserConfig = {}
769+
self.parserConfiguration = function parserConfiguration (config) {
770+
argsert('<object>', [config], arguments.length)
771+
parserConfig = config
772+
return self
773+
}
774+
self.getParserConfiguration = () => parserConfig
775+
768776
self.showHelp = function (level) {
769777
argsert('[string|function]', [level], arguments.length)
770778
if (!self.parsed) self._parseArgs(processArgs) // run parser, if it has not already been executed.
@@ -1010,7 +1018,14 @@ function Yargs (processArgs, cwd, parentRequire) {
10101018
args = args || processArgs
10111019

10121020
options.__ = y18n.__
1013-
options.configuration = pkgUp()['yargs'] || {}
1021+
options.configuration = self.getParserConfiguration()
1022+
1023+
// Deprecated
1024+
let pkgConfig = pkgUp()['yargs']
1025+
if (pkgConfig) {
1026+
console.warn('Configuring yargs through package.json is deprecated and will be removed in the next major release, please use the JS API instead.')
1027+
options.configuration = Object.assign({}, pkgConfig, options.configuration)
1028+
}
10141029

10151030
const parsed = Parser.detailed(args, options)
10161031
let argv = parsed.argv

0 commit comments

Comments
 (0)
Please sign in to comment.