Skip to content

Commit

Permalink
fix: stop applying parser to context object (#675)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Oct 16, 2016
1 parent 81984e6 commit 3fe9b8f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
16 changes: 16 additions & 0 deletions test/yargs.js
Expand Up @@ -938,6 +938,22 @@ describe('yargs dsl tests', function () {
argv.what.should.equal(true)
})

// see: https://github.com/yargs/yargs/issues/671
it('does not fail if context object has cyclical reference', function () {
var argv = null
var context = {state: 'grumpy but rich'}
context.res = context
yargs()
.command('batman <api-token>', 'batman command', function () {}, function (_argv) {
argv = _argv
})
.parse('batman robin --what', context, function (_err, argv, _output) {})

argv.state.should.equal('grumpy but rich')
argv['api-token'].should.equal('robin')
argv.what.should.equal(true)
})

it('overwrites the prior context object, when parse is called multiple times', function () {
var argv = null
var parser = yargs()
Expand Down
15 changes: 8 additions & 7 deletions yargs.js
@@ -1,3 +1,4 @@
const assign = require('./lib/assign')
const Command = require('./lib/command')
const Completion = require('./lib/completion')
const Parser = require('yargs-parser')
Expand Down Expand Up @@ -165,6 +166,8 @@ function Yargs (processArgs, cwd, parentRequire) {
command.unfreeze()
strict = frozen.strict
completionCommand = frozen.completionCommand
parseFn = null
parseContext = null
frozen = undefined
}

Expand Down Expand Up @@ -422,13 +425,12 @@ function Yargs (processArgs, cwd, parentRequire) {
}

var parseFn = null
var parseContext = null
self.parse = function (args, shortCircuit, _parseFn) {
var config = null

// a context object can optionally be provided, this allows
// additional information to be passed to a command handler.
if (typeof shortCircuit === 'object') {
config = shortCircuit
parseContext = shortCircuit
shortCircuit = _parseFn
}

Expand All @@ -447,13 +449,11 @@ function Yargs (processArgs, cwd, parentRequire) {
freeze()
exitProcess = false
}
if (config) self.config(config)

var parsed = parseArgs(args, shortCircuit)
if (parseFn) {
parseFn(exitError, parsed, output)
unfreeze()
parseFn = null
}

return parsed
Expand Down Expand Up @@ -580,7 +580,7 @@ function Yargs (processArgs, cwd, parentRequire) {
}
self.getGroups = function () {
// combine explicit and preserved groups. explicit groups should be first
return require('./lib/assign')(groups, preservedGroups)
return assign(groups, preservedGroups)
}

// as long as options.envPrefix is not undefined,
Expand Down Expand Up @@ -823,7 +823,8 @@ function Yargs (processArgs, cwd, parentRequire) {
options.__ = y18n.__
options.configuration = pkgUp(cwd)['yargs'] || {}
const parsed = Parser.detailed(args, options)
const argv = parsed.argv
var argv = parsed.argv
if (parseContext) argv = assign(parseContext, argv)
var aliases = parsed.aliases

argv.$0 = self.$0
Expand Down

0 comments on commit 3fe9b8f

Please sign in to comment.