From daa121c24c68e743f5a3982007525dd8c54c0337 Mon Sep 17 00:00:00 2001 From: liang liang Date: Wed, 14 Dec 2022 15:12:47 -0500 Subject: [PATCH] refactor logic to process help input --- packages/core/cli.js | 14 +++++++++----- packages/core/lib/command-utils.js | 16 ++++++---------- packages/truffle/test/scenarios/commands/help.js | 4 ++-- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/packages/core/cli.js b/packages/core/cli.js index 3b23b8bf589..3a56c4aace9 100755 --- a/packages/core/cli.js +++ b/packages/core/cli.js @@ -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 { diff --git a/packages/core/lib/command-utils.js b/packages/core/lib/command-utils.js index 5d1055ba3c7..0ebd8a6c3b1 100644 --- a/packages/core/lib/command-utils.js +++ b/packages/core/lib/command-utils.js @@ -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 ); @@ -221,19 +221,15 @@ const processHelpInput = inputStrings => { } // when `truffle help | --help` is used, convert inputStrings to run as `truffle help ` - 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; }; diff --git a/packages/truffle/test/scenarios/commands/help.js b/packages/truffle/test/scenarios/commands/help.js index 76f79e037dd..aaf0f83a988 100644 --- a/packages/truffle/test/scenarios/commands/help.js +++ b/packages/truffle/test/scenarios/commands/help.js @@ -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); @@ -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);