From 5cac9d66bc2141e831bb68e797d072cbba1c1e53 Mon Sep 17 00:00:00 2001 From: John Gee Date: Sat, 21 Sep 2019 18:20:33 +1200 Subject: [PATCH] Call default subcommand when there are unknown options on command line. (#1047) Co-authored-by: zhuqiacheng --- index.js | 2 +- tests/command.executableSubcommand.default.test.js | 13 ++++++++++--- tests/fixtures/pm-default | 3 +++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index e16f3ebe1..163cc5234 100644 --- a/index.js +++ b/index.js @@ -761,7 +761,7 @@ Command.prototype.parseArgs = function(args, unknown) { // If there were no args and we have unknown options, // then they are extraneous and we need to error. - if (unknown.length > 0) { + if (unknown.length > 0 && !this.defaultExecutable) { this.unknownOption(unknown[0]); } if (this.commands.length === 0 && diff --git a/tests/command.executableSubcommand.default.test.js b/tests/command.executableSubcommand.default.test.js index 867a660df..3bbae5c0d 100644 --- a/tests/command.executableSubcommand.default.test.js +++ b/tests/command.executableSubcommand.default.test.js @@ -10,9 +10,16 @@ test('when default subcommand and no command then call default', (done) => { }); }); -test('when default subcommand and unrecognised argument then call default', (done) => { - childProcess.execFile(bin, ['unrecognised-argument'], { }, function(_error, stdout, stderr) { - expect(stdout).toBe('default\n'); +test('when default subcommand and unrecognised argument then call default with argument', (done) => { + childProcess.execFile(bin, ['an-argument'], { }, function(_error, stdout, stderr) { + expect(stdout).toBe("default\n[ 'an-argument' ]\n"); + done(); + }); +}); + +test('when default subcommand and unrecognised option then call default with option', (done) => { + childProcess.execFile(bin, ['--an-option'], { }, function(_error, stdout, stderr) { + expect(stdout).toBe("default\n[ '--an-option' ]\n"); done(); }); }); diff --git a/tests/fixtures/pm-default b/tests/fixtures/pm-default index c053cca81..09888b08a 100755 --- a/tests/fixtures/pm-default +++ b/tests/fixtures/pm-default @@ -1,2 +1,5 @@ #!/usr/bin/env node console.log('default'); +if (process.argv.length > 2) { + console.log(process.argv.slice(2)); +}