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

Enhancement: More robustly handle help requests in Truffle #5706

Merged
merged 24 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
dc3c40e
turn off yargs default behavior for truffle cmd help
lsqproduction Nov 15, 2022
46d5554
change inputStrings when help or --help is present
lsqproduction Nov 15, 2022
caccaf6
add handling for `truffle --help <cmd>`
lsqproduction Nov 15, 2022
3684867
improve code comment readability
lsqproduction Nov 29, 2022
f373b4c
Add complete Help command usage info
lsqproduction Jan 6, 2023
f35bc39
Add scenario test for Help cmd
lsqproduction Nov 30, 2022
27875a4
Add handling when help cmd is in wrong place
lsqproduction Nov 30, 2022
6d24c86
Add error handling when subCommand is invalid
lsqproduction Nov 30, 2022
ad46688
Move help inputStrings process to command-utils
lsqproduction Nov 30, 2022
f82d559
add missing deps to @core/package.json
lsqproduction Jan 4, 2023
b14076f
add yarn.lock
lsqproduction Nov 30, 2022
b0f9f7b
refactor logic to process help input
lsqproduction Dec 14, 2022
1a78722
change inputStrings assignment to use const
lsqproduction Jan 4, 2023
368ff27
rework processHelpInput in command-utils
lsqproduction Jan 6, 2023
9523edf
use TruffleError for error handling
lsqproduction Jan 6, 2023
56f4106
refactor test for help command
lsqproduction Jan 6, 2023
e3cd436
expand test cases for --help
lsqproduction Jan 9, 2023
39659ea
Fix usage syntax for help meta.js
lsqproduction Jan 9, 2023
24307a9
Fix typo on error message
lsqproduction Jan 9, 2023
0265837
refactor processHelpInput
lsqproduction Jan 9, 2023
1ef1776
refact processHelpInput to inline check on cli.js
lsqproduction Jan 9, 2023
580324e
error better for unknown truffle db command
lsqproduction Jan 10, 2023
3495ea3
expand documention on handling --help
lsqproduction Jan 10, 2023
75ada28
remove incorrect syntax usage info
lsqproduction Jan 10, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 30 additions & 12 deletions packages/core/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,38 @@ listeners.forEach(listener => process.removeListener("warning", listener));

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

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);
}

const {
getCommand,
prepareOptions,
runCommand
runCommand,
displayGeneralHelp
} = require("./lib/command-utils");

//User only enter truffle with no commands, let's show them what's available.
if (inputStrings.length === 0) {
displayGeneralHelp();
process.exit();
}

//if `help` or `--help` is in the command, validate and transform the input argument for help
if (
inputStrings.some(inputString => ["help", "--help"].includes(inputString))
) {
//when user wants general help
if (inputStrings.length === 1) {
displayGeneralHelp();
process.exit();
}

//check where is --help used, mutate argument into a proper help command
const helpIndex = inputStrings.indexOf("--help");

if (helpIndex !== -1) {
inputStrings.splice(helpIndex, 1);
inputStrings.unshift("help");
}
}
cds-amal marked this conversation as resolved.
Show resolved Hide resolved

const command = getCommand({
inputStrings,
options: {},
Expand All @@ -67,7 +83,9 @@ const command = getCommand({
//getCommand() will return null if a command not recognized by truffle is used.
if (command === null) {
console.log(
`\`truffle ${inputStrings}\` is not a valid truffle command. Please see \`truffle help\` for available commands.`
`\`truffle ${inputStrings.join(
" "
)}\` is not a valid truffle command. Please see \`truffle help\` for available commands.`
);
process.exit(1);
}
Expand Down
6 changes: 4 additions & 2 deletions packages/core/lib/command-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ const prepareOptions = ({ command, inputStrings, options }) => {
const yargs = require("yargs/yargs")();
yargs
.command(require(`./commands/${command.name}/meta`))
//Turn off yargs' default behavior when handling "truffle --version"
.version(false);
//Turn off yargs' default behavior when handling `truffle --version` & `truffle <cmd> --help`
.version(false)
.help(false);

const commandOptions = yargs.parse(inputStrings);

// remove the task name itself put there by yargs
Expand Down
2 changes: 1 addition & 1 deletion packages/core/lib/commands/db/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ module.exports = async function (args) {
break;

default:
console.log(`Unknown command: ${subCommand}`);
console.log(`Unknown truffle db command: ${subCommand}`);
eggplantzzz marked this conversation as resolved.
Show resolved Hide resolved
}
};
10 changes: 9 additions & 1 deletion packages/core/lib/commands/help/displayCommandHelp.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
module.exports = async function (selectedCommand, subCommand, options) {
const commands = require("../index");
const globalCommandOptions = require("../../global-command-options");
const TruffleError = require("@truffle/error");

let commandHelp, commandDescription;

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

const chosenCommand = commands[selectedCommand].meta;

if (subCommand && chosenCommand.subCommands[subCommand]) {
if (subCommand) {
if (!chosenCommand.subCommands || !chosenCommand.subCommands[subCommand]) {
throw new TruffleError(
`"truffle ${inputStrings.join(" ")}" is an invalid command`
);
}
commandHelp = chosenCommand.subCommands[subCommand].help;
commandDescription = chosenCommand.subCommands[subCommand].description;
} else {
Expand Down
9 changes: 8 additions & 1 deletion packages/core/lib/commands/help/meta.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
const OS = require("os");

module.exports = {
command: "help",
description:
"List all commands or provide information about a specific command",
help: {
usage: "truffle help [<command>]",
usage:
"truffle help [<command> [<subCommand>]]" +
OS.EOL +
eggplantzzz marked this conversation as resolved.
Show resolved Hide resolved
" truffle --help [<command> [<subCommand>]]" +
OS.EOL +
" truffle [<command> [<subCommand>]] --help",
options: [
{
option: "<command>",
Expand Down
5 changes: 4 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@truffle/debugger": "^11.0.22",
"@truffle/decoder": "^5.3.5",
"@truffle/deployer": "^3.3.2",
"@truffle/db-loader": "^0.2.10",
eggplantzzz marked this conversation as resolved.
Show resolved Hide resolved
"@truffle/environment": "^0.2.135",
"@truffle/error": "^0.2.0",
"@truffle/expect": "^0.1.4",
Expand Down Expand Up @@ -69,6 +70,7 @@
"get-random-values": "^1.2.2",
"glob": "^7.1.6",
"inquirer": "8.2.2",
"iter-tools": "^7.5.0",
"js-interpreter": "2.2.0",
"lodash": "^4.17.21",
"mocha": "10.1.0",
Expand All @@ -82,6 +84,7 @@
"universal-analytics": "^0.4.17",
"web3": "1.8.1",
"web3-utils": "1.8.1",
"uuid": "^9.0.0",
"xregexp": "^4.2.4",
"yargs": "^13.3.0"
},
Expand All @@ -103,4 +106,4 @@
}
],
"namespace": "consensys"
}
}
32 changes: 32 additions & 0 deletions packages/truffle/test/scenarios/commands/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,37 @@ describe("truffle help [ @standalone ]", function () {
);
assert(output.includes("--environment"));
});

it("displays help for the `help` command", async function () {
await CommandRunner.run("help help", config);
const output = logger.contents();
assert(
output.includes(
" Description: List all commands or provide information about a specific command"
)
);
}).timeout(20000);

it("displays help for given command when `--help` is at final position of the command line", async function () {
await CommandRunner.run("compile --help", config);
const output = logger.contents();
assert(output.includes("Description: Compile contract source files"));
}).timeout(20000);

it("displays help for given command when `--help` is at first position of the command line", async function () {
await CommandRunner.run("--help db serve", config);
const output = logger.contents();
assert(
output.includes("Description: Start Truffle's GraphQL UI playground")
);
}).timeout(20000);

it("displays help for given command when `--help` is in the middle of the command line", async function () {
await CommandRunner.run("db --help serve", config);
const output = logger.contents();
assert(
output.includes("Description: Start Truffle's GraphQL UI playground")
);
}).timeout(20000);
Comment on lines +56 to +76
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't a change request. Just pointing out a unit test could validate the various --help positions, leaving only testing truffle help for integration tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm gonna come ask you about integration testing.

});
});
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16659,6 +16659,13 @@ iter-tools@^7.0.2:
dependencies:
"@babel/runtime" "^7.12.1"

iter-tools@^7.5.0:
version "7.5.0"
resolved "https://registry.yarnpkg.com/iter-tools/-/iter-tools-7.5.0.tgz#061240dcac13668e66bb787b0fcd407a58ea39ab"
integrity sha512-L0p/RG3Hwk1urilryDKqU8pQ1t5AaaMc7CHmiwJD/uh63Lv7VyjNng/esstf+Tct1587IpetpcDFdufz8sG+sQ==
dependencies:
"@babel/runtime" "^7.12.1"

iterate-iterator@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/iterate-iterator/-/iterate-iterator-1.0.1.tgz#1693a768c1ddd79c969051459453f082fe82e9f6"
Expand Down