Skip to content

Commit

Permalink
fix: print usage menu for an unknown flag (#130)
Browse files Browse the repository at this point in the history
When an unknown option was present it would only print a single-line error message stating that the flag was unknwon. Now it also prints the usage menu. This was done by overriding the `commander` code for unknown options handling.
  • Loading branch information
presto21 authored Feb 12, 2020
1 parent 05c7164 commit def7dcc
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 3 deletions.
115 changes: 113 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"pad": "^2.2.1",
"require-all": "^3.0.0",
"semver": "^5.5.1",
"sinon": "^8.1.1",
"update-notifier": "^2.5.0",
"validator": "^11.0.0",
"yaml-js": "^0.2.3"
Expand Down
2 changes: 1 addition & 1 deletion src/cli-validator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
require('./utils/checkVersion')('8.9.0');
require('./utils/updateNotifier');

const program = require('commander');
const program = require('./utils/modified-commander');
const cliValidator = require('./runValidator');
const version = require('../../package.json').version;

Expand Down
23 changes: 23 additions & 0 deletions src/cli-validator/utils/modified-commander.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const program = require('commander');

// This module is used to modify the commander code. When
// an unsupported option is given, only a single-line error
// is printed. Now, it will also print out the usage menu.

// deletes the function in order to reimplement it
delete program['unknownOption'];

// reimplementing the funciton
program.unknownOption = function(flag) {
if (this._allowUnknownOption) return;
console.error();
console.error(" error: unknown option `%s'", flag);
console.error();

// this is the extra code added to the function
this.outputHelp();

process.exit(1);
};

module.exports = program;
19 changes: 19 additions & 0 deletions test/cli-validator/tests/optionHandling.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const intercept = require('intercept-stdout');
const expect = require('expect');
const stripAnsiFrom = require('strip-ansi');
const commandLineValidator = require('../../../src/cli-validator/runValidator');
const modifiedCommander = require('../../../src/cli-validator/utils/modified-commander');
const sinon = require('sinon');

// for an explanation of the text interceptor,
// see the comments for the first test in expectedOutput.js
Expand Down Expand Up @@ -316,4 +318,21 @@ describe('cli tool - test option handling', function() {
expect(warningCount).toEqual(3); // without the config this value is 5
expect(errorCount).toEqual(3); // without the config this value is 0
});

it('should return an error and usage menu when there is an unknown option', async function() {
const errorStub = sinon.stub(console, 'error');
const helpStub = sinon.stub(modifiedCommander, 'outputHelp');
const exitStub = sinon.stub(process, 'exit');

modifiedCommander.unknownOption('r');

expect(errorStub.called).toEqual(true);
expect(errorStub.secondCall.args[0].trim()).toEqual(
"error: unknown option `%s'"
);
expect(errorStub.secondCall.args[1].trim()).toEqual('r');
expect(helpStub.called).toEqual(true);
expect(exitStub.called).toEqual(true);
expect(exitStub.firstCall.args[0]).toEqual(1);
});
});

0 comments on commit def7dcc

Please sign in to comment.