Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error out if unknown command #896

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,12 @@ Command.prototype.parse = function(argv) {
return this.executeSubCommand(argv, args, parsed.unknown);
}

// Output unknown command error
if (args.length > 0) {
console.error('error: unknown command %s', args[0]);
this.outputHelp();
}

return result;
};

Expand Down Expand Up @@ -584,7 +590,9 @@ Command.prototype.executeSubCommand = function(argv, args, unknown) {
} else if (err.code === 'EACCES') {
console.error('error: %s(1) not executable. try chmod or run with root', bin);
}
process.exit(1);
if (process.env.NODE_ENV !== 'test') {
process.exit(1);
}
});

// Store the reference to the child process
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env node

var program = require('../../');

program
.version('0.0.1')
.command('foo [name]', 'dummy command to test unknown command')
.parse(process.argv);
2 changes: 2 additions & 0 deletions test/fixtures/cmd-foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
console.log('foo');
4 changes: 2 additions & 2 deletions test/test.command.allowUnknownOption.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ program
});
program.parse('node test sub -m'.split(' '));

stubError.callCount.should.equal(1);
stubError.callCount.should.equal(2);
stubExit.calledOnce.should.be.true();

// command with `allowUnknownOption`
Expand All @@ -48,7 +48,7 @@ program
});
program.parse('node test sub2 -m'.split(' '));

stubError.callCount.should.equal(0);
stubError.callCount.should.equal(1);
stubExit.calledOnce.should.be.false();


Expand Down
14 changes: 14 additions & 0 deletions test/test.command.executableSubcommandUnknown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var exec = require('child_process').exec
, path = require('path')
, should = require('should');

var bin = path.join(__dirname, './fixtures/cmd')

exec(bin + ' foo', function (error, stdout, stderr) {
stdout.should.equal('foo\n');
});

const unknownSubcmd = 'foo_invalid';
exec(bin + ' ' + unknownSubcmd, function (error, stdout, stderr) {
stderr.should.equal('error: unknown command ' + unknownSubcmd + '\n');
});
2 changes: 1 addition & 1 deletion test/test.command.noConflict.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ sinon.stub(process.stdout, 'write');

program
.version('0.0.1')
.command('version')
.command('version', 'description')
.action(function() {
console.log('Version command invoked');
});
Expand Down
21 changes: 21 additions & 0 deletions test/test.known.command.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var program = require('../')
, sinon = require('sinon')
, should = require('should');

sinon.stub(process, 'exit');
sinon.stub(process.stdout, 'write');

var stubError = sinon.stub(console, 'error');

var cmd = 'my_command';

program.command(cmd, 'description');

program.parse(['node', 'test', cmd]);

stubError.callCount.should.equal(0);
var output = process.stdout.write.args;
output.should.deepEqual([]);

sinon.restore();

2 changes: 1 addition & 1 deletion test/test.options.version.custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var program = require('../')

var capturedExitCode, capturedOutput, oldProcessExit, oldProcessStdoutWrite;

program.version('0.0.1', '-r, --revision');
program.version('0.0.1', '-r, --revision').description('description');

['-r', '--revision'].forEach(function (flag) {
capturedExitCode = -1;
Expand Down
2 changes: 1 addition & 1 deletion test/test.options.version.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var program = require('../')

var capturedExitCode, capturedOutput, oldProcessExit, oldProcessStdoutWrite;

program.version('0.0.1');
program.version('0.0.1').description('description');

['-V', '--version'].forEach(function (flag) {
capturedExitCode = -1;
Expand Down
19 changes: 19 additions & 0 deletions test/test.unknown.command.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var program = require('../')
, sinon = require('sinon')
, should = require('should');

sinon.stub(process, 'exit');
sinon.stub(process.stdout, 'write');

var stubError = sinon.stub(console, 'error');

var cmd = 'my_command', invalidCmd = 'invalid_command';

program.command(cmd, 'description');

program.parse(['node', 'test', invalidCmd]);

stubError.callCount.should.equal(1);

sinon.restore();