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

Commit

Permalink
Move help inputStrings process to command-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
lsqproduction committed Nov 30, 2022
1 parent 2501ee8 commit 3e18921
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 27 deletions.
34 changes: 7 additions & 27 deletions packages/core/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,15 @@ if (!semver.gte(process.version, minimumNodeVersion)) {
const listeners = process.listeners("warning");
listeners.forEach(listener => process.removeListener("warning", listener));

const inputStrings = process.argv.slice(2);
let inputStrings = process.argv.slice(2);

if (inputStrings.length > 2 && ["help", "--help"].includes(inputStrings[1])) {
const help = require("./lib/commands/help").meta;
console.log(
"Error: please use below syntax to displaying help information\n",
"\n ",
help.help.usage
);
process.exit();
}
//check if user wants some help
const helpNeeded = inputStrings.some(r => ["help", "--help"].indexOf(r) >= 0);

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

if (userWantsGeneralHelp) {
const { displayGeneralHelp } = require("./lib/command-utils");
displayGeneralHelp();
process.exit(0);
}
// when `truffle --help <cmd>` is used, convert inputStrings to run as `truffle help <cmd>`
if (inputStrings.length > 1 && inputStrings[0] === "--help") {
inputStrings[inputStrings.indexOf("--help")] = "help";
}
// when `truffle <cmd> help | --help` is used, convert inputStrings to run as `truffle help <cmd>`
if (["help", "--help"].includes(inputStrings[inputStrings.length - 1])) {
inputStrings.pop();
inputStrings.unshift("help");
if (helpNeeded) {
//check what help does user want
const { processHelpInput } = require("./lib/command-utils");
inputStrings = processHelpInput(inputStrings);
}

const {
Expand Down
36 changes: 36 additions & 0 deletions packages/core/lib/command-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,41 @@ const runCommand = async function (command, options) {
return await command.run(options);
};

const processHelpInput = inputStrings => {
//is it wrong syntax of help?
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",
"\n ",
help.help.usage
);
process.exit();
}

// when `truffle --help <cmd>` is used, convert inputStrings to run as `truffle help <cmd>`
if (inputStrings.length > 1 && inputStrings[0] === "--help") {
inputStrings[inputStrings.indexOf("--help")] = "help";
}

// when `truffle <cmd> help | --help` is used, convert inputStrings to run as `truffle help <cmd>`
if (["help", "--help"].includes(inputStrings[inputStrings.length - 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) {
displayGeneralHelp();
process.exit(0);
}
return inputStrings;
};

const displayGeneralHelp = () => {
const yargs = require("yargs/yargs")();
commands.forEach(command => {
Expand Down Expand Up @@ -307,6 +342,7 @@ const deriveConfigEnvironment = function (detectedConfig, network, url) {
};

module.exports = {
processHelpInput,
displayGeneralHelp,
getCommand,
prepareOptions,
Expand Down

0 comments on commit 3e18921

Please sign in to comment.