Skip to content

Commit

Permalink
resolves tj#766 suggest to users run --help command
Browse files Browse the repository at this point in the history
Adding one more console.error statements to bellow methods:
- Command.prototype.missingArgument
- Command.prototype.optionMissingArgument
- Command.prototype.unknownOption
- Command.prototype.variadicArgNotLast

This console.error messages propose to user run the same command
with `--help` option to get more info about the command which try
to run. This is something that most UNIX like commands do
(like rm, cp, mv, etc)
  • Loading branch information
GeorgeGkas committed Feb 27, 2018
1 parent c92ca1d commit b7b7287
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 3 deletions.
4 changes: 4 additions & 0 deletions index.js
Expand Up @@ -787,6 +787,7 @@ Command.prototype.opts = function() {
Command.prototype.missingArgument = function(name) {
console.error();
console.error(" error: missing required argument `%s'", name);
console.error(" Try '--help' for more information.");
console.error();
process.exit(1);
};
Expand All @@ -806,6 +807,7 @@ Command.prototype.optionMissingArgument = function(option, flag) {
} else {
console.error(" error: option `%s' argument missing", option.flags);
}
console.error(" Try '--help' for more information.");
console.error();
process.exit(1);
};
Expand All @@ -821,6 +823,7 @@ Command.prototype.unknownOption = function(flag) {
if (this._allowUnknownOption) return;
console.error();
console.error(" error: unknown option `%s'", flag);
console.error(" Try '--help' for more information.");
console.error();
process.exit(1);
};
Expand All @@ -835,6 +838,7 @@ Command.prototype.unknownOption = function(flag) {
Command.prototype.variadicArgNotLast = function(name) {
console.error();
console.error(" error: variadic arguments must be last `%s'", name);
console.error(" Try '--help' for more information.");
console.error();
process.exit(1);
};
Expand Down
4 changes: 2 additions & 2 deletions test/test.command.allowUnknownOption.js
Expand Up @@ -13,7 +13,7 @@ program
.option('-p, --pepper', 'add pepper');
program.parse('node test -m'.split(' '));

stubError.callCount.should.equal(3);
stubError.callCount.should.equal(4);


// test subcommand
Expand All @@ -24,7 +24,7 @@ program
});
program.parse('node test sub -m'.split(' '));

stubError.callCount.should.equal(3);
stubError.callCount.should.equal(4);
stubExit.calledOnce.should.be.true();

// command with `allowUnknownOption`
Expand Down
2 changes: 1 addition & 1 deletion test/test.options.args.required.js
Expand Up @@ -14,7 +14,7 @@ console.error = function () {

process.on('exit', function (code) {
code.should.equal(1);
info.length.should.equal(3);
info.length.should.equal(4);
info[1].should.equal(" error: option `-c, --cheese <type>' argument missing");
process.exit(0)
});
Expand Down
1 change: 1 addition & 0 deletions test/test.variadic.args.js
Expand Up @@ -60,5 +60,6 @@ console.error = oldConsoleError;
[
'',
' error: variadic arguments must be last `variadicArg\'',
' Try \'--help\' for more information.',
''
].join('\n').should.eql(errorMessage);

0 comments on commit b7b7287

Please sign in to comment.