Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2125 from skywing/develop+unsupportedflag3
Browse files Browse the repository at this point in the history
Warn/error about unsupported flags #468
  • Loading branch information
eggplantzzz authored Jul 1, 2019
2 parents 081ec48 + dd34c0d commit 42e8ddc
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 8 deletions.
36 changes: 34 additions & 2 deletions packages/truffle-core/lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,41 @@ class Command {
}
});

const newOptions = Object.assign({}, clone, argv);

//Check unsupported command line flag according to the option list in help
try {
const inputOptions = inputStrings
.map(string => {
return string.startsWith("--") ? string : null;
})
.filter(item => {
return item != null;
});

const validOptions = result.command.help.options
.map(item => {
let opt = item.option.split(" ")[0];
return opt.startsWith("--") ? opt : null;
})
.filter(item => {
return item != null;
});

let invalidOptions = inputOptions.filter(
opt => !validOptions.includes(opt)
);

if (invalidOptions.length > 0) {
if (options.logger) {
const log = options.logger.log || options.logger.debug;
log(
"> Warning: possible unsupported (undocumented in help) command line option: " +
invalidOptions
);
}
}

const newOptions = Object.assign({}, clone, argv);

result.command.run(newOptions, callback);
analytics.send({
command: result.name ? result.name : "other",
Expand Down
9 changes: 3 additions & 6 deletions packages/truffle-core/lib/commands/obtain.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ module.exports = {
command: "obtain",
description: "Fetch and cache a specified compiler",
help: {
usage: "truffle obtain <--<compiler_name> <version>>",
usage: "truffle obtain [--solc <version>]",
options: [
{
option: "<--<compiler_name> <version>>",
description:
`Download and cache a version of the specified compiler.\n` +
` compiler_name must be one of the following: ` +
`'solc'.(required)`
option: "--solc <version>",
description: `Download and cache a version of the solc compiler. (required)`
}
]
},
Expand Down
33 changes: 33 additions & 0 deletions packages/truffle-core/test/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,37 @@ describe("Commander", function() {
var actualCommand = commander.getCommand("console").command;
assert.equal(actualCommand, commands.console);
});

it("will warn and display error for unsupported flags in commands", function() {
var actualCommand = commander.getCommand("mig").command;
assert.equal(actualCommand, commands.migrate);

const originalLog = console.log || console.debug;
let warning = "";
console.log = function(msg) {
console.log = originalLog;
warning = msg;
};

commander.run(
[
"migrate",
"--network",
"localhost",
"--unsupportedflag",
"invalidoption",
"--unsupportedflag2",
"invalidflag2"
],
{ noAliases: true, logger: console },
function() {
//ignore. not part of test
}
);

assert.equal(
warning,
"> Warning: possible unsupported (undocumented in help) command line option: --unsupportedflag,--unsupportedflag2"
);
});
});

0 comments on commit 42e8ddc

Please sign in to comment.