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

Commit

Permalink
refactor logic to process help input
Browse files Browse the repository at this point in the history
  • Loading branch information
lsqproduction committed Dec 14, 2022
1 parent 28dcc7a commit daa121c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
14 changes: 9 additions & 5 deletions packages/core/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,16 @@ listeners.forEach(listener => process.removeListener("warning", listener));
let inputStrings = process.argv.slice(2);

//check if user wants some help
const helpNeeded = inputStrings.some(r => ["help", "--help"].indexOf(r) >= 0);

if (helpNeeded) {
//check what help does user want
if (inputStrings.length === 0) {
const { displayGeneralHelp } = require("./lib/command-utils");
displayGeneralHelp();
process.exit();
} else if (
inputStrings.some(inputString => ["help", "--help"].includes(inputString))
) {
//check what kind of help the user is looking for
const { processHelpInput } = require("./lib/command-utils");
inputStrings = processHelpInput(inputStrings);
processHelpInput(inputStrings);
}

const {
Expand Down
16 changes: 6 additions & 10 deletions packages/core/lib/command-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,11 @@ const runCommand = async function (command, options) {
};

const processHelpInput = inputStrings => {
//is it wrong syntax of help?
//When we think user is trying to access help incorrectly, provide them with some helpful information.
if (inputStrings.length > 2 && ["help", "--help"].includes(inputStrings[1])) {
const help = require("./commands/help").meta;
console.log(
"Error: please use below syntax to displaying help information\n",
"Error: please use the following syntax for accessing help information\n",
"\n ",
help.help.usage
);
Expand All @@ -221,19 +221,15 @@ const processHelpInput = inputStrings => {
}

// when `truffle <cmd> help | --help` is used, convert inputStrings to run as `truffle help <cmd>`
if (["help", "--help"].includes(inputStrings[inputStrings.length - 1])) {
if (["help", "--help"].includes(inputStrings[1])) {
inputStrings.pop();
inputStrings.unshift("help");
}

const userWantsGeneralHelp =
inputStrings.length === 0 ||
(inputStrings.length === 1 && ["help", "--help"].includes(inputStrings[0]));

//is it general help?
if (userWantsGeneralHelp) {
//when user wants general help
if (inputStrings.length === 1) {
displayGeneralHelp();
process.exit(0);
process.exit();
}
return inputStrings;
};
Expand Down
4 changes: 2 additions & 2 deletions packages/truffle/test/scenarios/commands/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe("truffle help [ @standalone ]", function () {
const output = logger.contents();
assert(
output.includes(
"Error: please use below syntax to displaying help information"
"Error: please use the following syntax for accessing help information"
)
);
}).timeout(20000);
Expand All @@ -92,7 +92,7 @@ describe("truffle help [ @standalone ]", function () {
const output = logger.contents();
assert(
output.includes(
"Error: please use below syntax to displaying help information"
"Error: please use the following syntax for accessing help information"
)
);
}).timeout(20000);
Expand Down

0 comments on commit daa121c

Please sign in to comment.