Skip to content

Commit e84b683

Browse files
committed
Display user-defined global options with --help
1 parent d8fd59a commit e84b683

File tree

5 files changed

+85
-14
lines changed

5 files changed

+85
-14
lines changed

v-next/hardhat/src/internal/cli/helpers/getGlobalHelpString.ts

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
1+
import type { GlobalOptionsMap } from "@ignored/hardhat-vnext-core/types/global-options";
12
import type { Task } from "@ignored/hardhat-vnext-core/types/tasks";
23

3-
import { BUILTIN_OPTIONS } from "../../builtin-options.js";
44
import { getHardhatVersion } from "../../utils/package.js";
55

66
import {
77
GLOBAL_NAME_PADDING,
8-
formatOptionName,
98
getLongestNameLength,
109
getSection,
10+
parseGlobalOptions,
1111
parseTasks,
1212
} from "./utils.js";
1313

1414
export async function getGlobalHelpString(
1515
rootTasks: Map<string, Task>,
16+
globalOptionsMap: GlobalOptionsMap,
1617
): Promise<string> {
1718
const version = await getHardhatVersion();
1819

1920
const { tasks, subtasks } = parseTasks(rootTasks);
2021

21-
const formattedBuiltinOptions = BUILTIN_OPTIONS.map(
22-
({ name, description }) => ({
23-
name: formatOptionName(name),
24-
description,
25-
}),
26-
);
22+
const globalOptions = parseGlobalOptions(globalOptionsMap);
2723

2824
const namePadding =
29-
getLongestNameLength([...tasks, ...subtasks, ...formattedBuiltinOptions]) +
25+
getLongestNameLength([...tasks, ...subtasks, ...globalOptions]) +
3026
GLOBAL_NAME_PADDING;
3127

3228
let output = `Hardhat version ${version}
@@ -42,7 +38,7 @@ Usage: hardhat [GLOBAL OPTIONS] <TASK> [SUBTASK] [TASK OPTIONS] [--] [TASK ARGUM
4238
output += getSection("AVAILABLE SUBTASKS", subtasks, namePadding);
4339
}
4440

45-
output += getSection("GLOBAL OPTIONS", formattedBuiltinOptions, namePadding);
41+
output += getSection("GLOBAL OPTIONS", globalOptions, namePadding);
4642

4743
output += `\nTo get help for a specific task run: npx hardhat <TASK> [SUBTASK] --help`;
4844

v-next/hardhat/src/internal/cli/helpers/utils.ts

+23
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,31 @@
11
import type { ParameterType } from "@ignored/hardhat-vnext-core/config";
2+
import type { GlobalOptionsMap } from "@ignored/hardhat-vnext-core/types/global-options";
23
import type { Task } from "@ignored/hardhat-vnext-core/types/tasks";
34

5+
import { BUILTIN_OPTIONS } from "../../builtin-options.js";
6+
47
export const GLOBAL_NAME_PADDING = 6;
58

9+
export function parseGlobalOptions(
10+
globalOptionsMap: GlobalOptionsMap,
11+
): Array<{ name: string; description: string }> {
12+
const formattedBuiltinOptions = BUILTIN_OPTIONS.map(
13+
({ name, description }) => ({
14+
name: formatOptionName(name),
15+
description,
16+
}),
17+
);
18+
19+
const formattedUserOptions = Array.from(globalOptionsMap).map(([, entry]) => {
20+
return {
21+
name: formatOptionName(entry.option.name),
22+
description: entry.option.description,
23+
};
24+
});
25+
26+
return [...formattedBuiltinOptions, ...formattedUserOptions];
27+
}
28+
629
export function parseTasks(taskMap: Map<string, Task>): {
730
tasks: Array<{ name: string; description: string }>;
831
subtasks: Array<{ name: string; description: string }>;

v-next/hardhat/src/internal/cli/main.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ export async function main(
9696

9797
if (Array.isArray(taskOrId)) {
9898
if (taskOrId.length === 0) {
99-
const globalHelp = await getGlobalHelpString(hre.tasks.rootTasks);
99+
const globalHelp = await getGlobalHelpString(
100+
hre.tasks.rootTasks,
101+
globalOptionsMap,
102+
);
100103

101104
print(globalHelp);
102105
return;

v-next/hardhat/test/internal/cli/helpers/getGlobalHelpString.ts

+51-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import type { Task } from "@ignored/hardhat-vnext-core/types/tasks";
33
import assert from "node:assert/strict";
44
import { describe, it } from "node:test";
55

6+
import { buildGlobalOptionsMap } from "@ignored/hardhat-vnext-core";
7+
import {
8+
globalOption,
9+
ParameterType,
10+
} from "@ignored/hardhat-vnext-core/config";
611
import { readClosestPackageJson } from "@ignored/hardhat-vnext-utils/package";
712

813
import { getGlobalHelpString } from "../../../../src/internal/cli/helpers/getGlobalHelpString.js";
@@ -13,7 +18,7 @@ describe("getGlobalHelpString", async function () {
1318
describe("when there are no tasks", function () {
1419
it("should return the global help string", async function () {
1520
const tasks = new Map();
16-
const help = await getGlobalHelpString(tasks);
21+
const help = await getGlobalHelpString(tasks, new Map());
1722

1823
const expected = `Hardhat version ${packageJson.version}
1924
@@ -65,7 +70,7 @@ To get help for a specific task run: npx hardhat <TASK> [SUBTASK] --help`;
6570
],
6671
]);
6772

68-
const help = await getGlobalHelpString(tasks);
73+
const help = await getGlobalHelpString(tasks, new Map());
6974

7075
const expected = `Hardhat version ${packageJson.version}
7176
@@ -132,7 +137,7 @@ To get help for a specific task run: npx hardhat <TASK> [SUBTASK] --help`;
132137
],
133138
]);
134139

135-
const help = await getGlobalHelpString(tasks);
140+
const help = await getGlobalHelpString(tasks, new Map());
136141

137142
const expected = `Hardhat version ${packageJson.version}
138143
@@ -154,6 +159,49 @@ GLOBAL OPTIONS:
154159
--show-stack-traces Show stack traces (always enabled on CI servers).
155160
--version Shows hardhat's version.
156161
162+
To get help for a specific task run: npx hardhat <TASK> [SUBTASK] --help`;
163+
164+
assert.equal(help, expected);
165+
});
166+
});
167+
168+
describe("when there are user-defined global options", function () {
169+
it("should return the global help string with the user-defined global options", async function () {
170+
const tasks = new Map();
171+
const globalOptionsMap = buildGlobalOptionsMap([
172+
{
173+
id: "plugin1",
174+
globalOptions: [
175+
globalOption({
176+
name: "userOption1",
177+
description: "userOption1 description.",
178+
parameterType: ParameterType.STRING,
179+
defaultValue: "default",
180+
}),
181+
globalOption({
182+
name: "userOption2",
183+
description: "userOption2 description.",
184+
parameterType: ParameterType.STRING,
185+
defaultValue: "default",
186+
}),
187+
],
188+
},
189+
]);
190+
const help = await getGlobalHelpString(tasks, globalOptionsMap);
191+
192+
const expected = `Hardhat version ${packageJson.version}
193+
194+
Usage: hardhat [GLOBAL OPTIONS] <TASK> [SUBTASK] [TASK OPTIONS] [--] [TASK ARGUMENTS]
195+
196+
GLOBAL OPTIONS:
197+
198+
--config A Hardhat config file.
199+
--help Shows this message, or a task's help if its name is provided.
200+
--show-stack-traces Show stack traces (always enabled on CI servers).
201+
--version Shows hardhat's version.
202+
--user-option-1 userOption1 description.
203+
--user-option-2 userOption2 description.
204+
157205
To get help for a specific task run: npx hardhat <TASK> [SUBTASK] --help`;
158206

159207
assert.equal(help, expected);

v-next/hardhat/test/internal/cli/main.ts

+1
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ GLOBAL OPTIONS:
225225
--help Shows this message, or a task's help if its name is provided.
226226
--show-stack-traces Show stack traces (always enabled on CI servers).
227227
--version Shows hardhat's version.
228+
--flag A flag
228229
229230
To get help for a specific task run: npx hardhat <TASK> [SUBTASK] --help`;
230231

0 commit comments

Comments
 (0)