Skip to content

Commit

Permalink
Call default subcommand when there are unknown options on command lin…
Browse files Browse the repository at this point in the history
…e. (#1047)

Co-authored-by: zhuqiacheng <[email protected]>
  • Loading branch information
shadowspawn and zhuqiacheng authored Sep 21, 2019
1 parent 9593656 commit 5cac9d6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down
13 changes: 10 additions & 3 deletions tests/command.executableSubcommand.default.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
3 changes: 3 additions & 0 deletions tests/fixtures/pm-default
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
#!/usr/bin/env node
console.log('default');
if (process.argv.length > 2) {
console.log(process.argv.slice(2));
}

0 comments on commit 5cac9d6

Please sign in to comment.