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

Commit 37db8eb

Browse files
Merge pull request #5706 from trufflesuite/betterHelp
Enhancement: More robustly handle help requests in Truffle
2 parents 147698d + 75ada28 commit 37db8eb

File tree

8 files changed

+90
-18
lines changed

8 files changed

+90
-18
lines changed

packages/core/cli.js

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,40 @@ listeners.forEach(listener => process.removeListener("warning", listener));
4242

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

45-
const userWantsGeneralHelp =
46-
inputStrings.length === 0 ||
47-
(inputStrings.length === 1 && ["help", "--help"].includes(inputStrings[0]));
48-
49-
if (userWantsGeneralHelp) {
50-
const { displayGeneralHelp } = require("./lib/command-utils");
51-
displayGeneralHelp();
52-
process.exit(0);
53-
}
54-
5545
const {
5646
getCommand,
5747
prepareOptions,
58-
runCommand
48+
runCommand,
49+
displayGeneralHelp
5950
} = require("./lib/command-utils");
6051

52+
//User only enter truffle with no commands, let's show them what's available.
53+
if (inputStrings.length === 0) {
54+
displayGeneralHelp();
55+
process.exit();
56+
}
57+
58+
//if `help` or `--help` is in the command, validate and transform the input argument for help
59+
if (
60+
inputStrings.some(inputString => ["help", "--help"].includes(inputString))
61+
) {
62+
//when user wants general help
63+
if (inputStrings.length === 1) {
64+
displayGeneralHelp();
65+
process.exit();
66+
}
67+
68+
//check where is --help used, mutate argument into a proper help command
69+
const helpIndex = inputStrings.indexOf("--help");
70+
71+
if (helpIndex !== -1) {
72+
//remove `--help` from array
73+
inputStrings.splice(helpIndex, 1);
74+
//insert `help` in first position
75+
inputStrings.unshift("help");
76+
}
77+
}
78+
6179
const command = getCommand({
6280
inputStrings,
6381
options: {},
@@ -67,7 +85,9 @@ const command = getCommand({
6785
//getCommand() will return null if a command not recognized by truffle is used.
6886
if (command === null) {
6987
console.log(
70-
`\`truffle ${inputStrings}\` is not a valid truffle command. Please see \`truffle help\` for available commands.`
88+
`\`truffle ${inputStrings.join(
89+
" "
90+
)}\` is not a valid truffle command. Please see \`truffle help\` for available commands.`
7191
);
7292
process.exit(1);
7393
}

packages/core/lib/command-utils.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,10 @@ const prepareOptions = ({ command, inputStrings, options }) => {
9393
const yargs = require("yargs/yargs")();
9494
yargs
9595
.command(require(`./commands/${command.name}/meta`))
96-
//Turn off yargs' default behavior when handling "truffle --version"
97-
.version(false);
96+
//Turn off yargs' default behavior when handling `truffle --version` & `truffle <cmd> --help`
97+
.version(false)
98+
.help(false);
99+
98100
const commandOptions = yargs.parse(inputStrings);
99101

100102
// remove the task name itself put there by yargs

packages/core/lib/commands/db/run.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ module.exports = async function (args) {
1313
break;
1414

1515
default:
16-
console.log(`Unknown command: ${subCommand}`);
16+
console.log(`Unknown truffle db command: ${subCommand}`);
1717
}
1818
};

packages/core/lib/commands/help/displayCommandHelp.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
module.exports = async function (selectedCommand, subCommand, options) {
22
const commands = require("../index");
33
const globalCommandOptions = require("../../global-command-options");
4+
const TruffleError = require("@truffle/error");
45

56
let commandHelp, commandDescription;
67

8+
const inputStrings = process.argv.slice(2);
9+
710
const chosenCommand = commands[selectedCommand].meta;
811

9-
if (subCommand && chosenCommand.subCommands[subCommand]) {
12+
if (subCommand) {
13+
if (!chosenCommand.subCommands || !chosenCommand.subCommands[subCommand]) {
14+
throw new TruffleError(
15+
`"truffle ${inputStrings.join(" ")}" is an invalid command`
16+
);
17+
}
1018
commandHelp = chosenCommand.subCommands[subCommand].help;
1119
commandDescription = chosenCommand.subCommands[subCommand].description;
1220
} else {

packages/core/lib/commands/help/meta.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module.exports = {
33
description:
44
"List all commands or provide information about a specific command",
55
help: {
6-
usage: "truffle help [<command>]",
6+
usage: "truffle help [<command> [<subCommand>]]",
77
options: [
88
{
99
option: "<command>",

packages/core/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"@truffle/debugger": "^11.0.22",
3636
"@truffle/decoder": "^5.3.5",
3737
"@truffle/deployer": "^3.3.2",
38+
"@truffle/db-loader": "^0.2.10",
3839
"@truffle/environment": "^0.2.135",
3940
"@truffle/error": "^0.2.0",
4041
"@truffle/expect": "^0.1.4",
@@ -69,6 +70,7 @@
6970
"get-random-values": "^1.2.2",
7071
"glob": "^7.1.6",
7172
"inquirer": "8.2.2",
73+
"iter-tools": "^7.5.0",
7274
"js-interpreter": "2.2.0",
7375
"lodash": "^4.17.21",
7476
"mocha": "10.1.0",
@@ -82,6 +84,7 @@
8284
"universal-analytics": "^0.4.17",
8385
"web3": "1.8.1",
8486
"web3-utils": "1.8.1",
87+
"uuid": "^9.0.0",
8588
"xregexp": "^4.2.4",
8689
"yargs": "^13.3.0"
8790
},
@@ -103,4 +106,4 @@
103106
}
104107
],
105108
"namespace": "consensys"
106-
}
109+
}

packages/truffle/test/scenarios/commands/help.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,37 @@ describe("truffle help [ @standalone ]", function () {
4242
);
4343
assert(output.includes("--environment"));
4444
});
45+
46+
it("displays help for the `help` command", async function () {
47+
await CommandRunner.run("help help", config);
48+
const output = logger.contents();
49+
assert(
50+
output.includes(
51+
" Description: List all commands or provide information about a specific command"
52+
)
53+
);
54+
}).timeout(20000);
55+
56+
it("displays help for given command when `--help` is at final position of the command line", async function () {
57+
await CommandRunner.run("compile --help", config);
58+
const output = logger.contents();
59+
assert(output.includes("Description: Compile contract source files"));
60+
}).timeout(20000);
61+
62+
it("displays help for given command when `--help` is at first position of the command line", async function () {
63+
await CommandRunner.run("--help db serve", config);
64+
const output = logger.contents();
65+
assert(
66+
output.includes("Description: Start Truffle's GraphQL UI playground")
67+
);
68+
}).timeout(20000);
69+
70+
it("displays help for given command when `--help` is in the middle of the command line", async function () {
71+
await CommandRunner.run("db --help serve", config);
72+
const output = logger.contents();
73+
assert(
74+
output.includes("Description: Start Truffle's GraphQL UI playground")
75+
);
76+
}).timeout(20000);
4577
});
4678
});

yarn.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16658,6 +16658,13 @@ iter-tools@^7.0.2:
1665816658
dependencies:
1665916659
"@babel/runtime" "^7.12.1"
1666016660

16661+
iter-tools@^7.5.0:
16662+
version "7.5.0"
16663+
resolved "https://registry.yarnpkg.com/iter-tools/-/iter-tools-7.5.0.tgz#061240dcac13668e66bb787b0fcd407a58ea39ab"
16664+
integrity sha512-L0p/RG3Hwk1urilryDKqU8pQ1t5AaaMc7CHmiwJD/uh63Lv7VyjNng/esstf+Tct1587IpetpcDFdufz8sG+sQ==
16665+
dependencies:
16666+
"@babel/runtime" "^7.12.1"
16667+
1666116668
iterate-iterator@^1.0.1:
1666216669
version "1.0.1"
1666316670
resolved "https://registry.yarnpkg.com/iterate-iterator/-/iterate-iterator-1.0.1.tgz#1693a768c1ddd79c969051459453f082fe82e9f6"

0 commit comments

Comments
 (0)