From 21b1e35679800608480e2793a6a9b1604f33466a Mon Sep 17 00:00:00 2001 From: "Benjamin E. Coe" Date: Sun, 26 Feb 2017 00:21:26 -0600 Subject: [PATCH] fix: running parse() multiple times on the same yargs instance caused exception if help() enabled (#790) --- test/command.js | 15 +++++++++++++++ yargs.js | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/test/command.js b/test/command.js index 9396e144e..8c6df70bb 100644 --- a/test/command.js +++ b/test/command.js @@ -747,6 +747,21 @@ describe('Command', function () { counter.should.equal(2) }) + // addresses: https://github.com/yargs/yargs/issues/776 + it('allows command handler to be invoked repeatedly when help is enabled', function (done) { + var counter = 0 + var y = yargs([]) + .command('foo', 'the foo command', {}, function (argv) { + counter++ + }) + .help() + y.parse(['foo'], function () {}) + y.parse(['foo'], function () { + counter.should.equal(2) + return done() + }) + }) + // addresses https://github.com/yargs/yargs/issues/522 it('does not require builder function to return', function () { var argv = yargs('yo') diff --git a/yargs.js b/yargs.js index d1ffd31a9..a1e351f76 100644 --- a/yargs.js +++ b/yargs.js @@ -967,7 +967,7 @@ function Yargs (processArgs, cwd, parentRequire) { // consider any multi-char helpOpt alias as a valid help command // unless all helpOpt aliases are single-char // note that parsed.aliases is a normalized bidirectional map :) - var helpCmds = [helpOpt].concat(aliases[helpOpt]) + var helpCmds = [helpOpt].concat(aliases[helpOpt] || []) var multiCharHelpCmds = helpCmds.filter(function (k) { return k.length > 1 })