Skip to content

Commit 4c160a8

Browse files
committed
Global options added to task help
1 parent a7f9950 commit 4c160a8

File tree

7 files changed

+142
-27
lines changed

7 files changed

+142
-27
lines changed

.changeset/neat-apes-beam.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"hardhat": patch
3+
---
4+
5+
Display **GLOBAL OPTIONS** in help messages of all tasks that support them ([#7114](https://github.com/NomicFoundation/hardhat/issues/7114))

v-next/hardhat/src/internal/cli/help/get-global-help-string.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import {
77
GLOBAL_NAME_PADDING,
88
getLongestNameLength,
99
getSection,
10-
parseGlobalOptions,
1110
parseTasks,
11+
parseGlobalOptions,
1212
} from "./utils.js";
1313

1414
export async function getGlobalHelpString(

v-next/hardhat/src/internal/cli/help/get-help-string.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { GlobalOptionDefinitions } from "../../../types/global-options.js";
12
import type { Task } from "../../../types/tasks.js";
23

34
import {
@@ -7,18 +8,28 @@ import {
78
getSection,
89
parseSubtasks,
910
getUsageString,
11+
parseGlobalOptions,
1012
} from "./utils.js";
1113

12-
export async function getHelpString(task: Task): Promise<string> {
14+
export async function getHelpString(
15+
task: Task,
16+
globalOptionDefinitions: GlobalOptionDefinitions,
17+
): Promise<string> {
1318
const { default: chalk } = await import("chalk");
1419

1520
const { options, positionalArguments } = parseOptions(task);
1621

1722
const subtasks = parseSubtasks(task);
1823

24+
const globalOptions = parseGlobalOptions(globalOptionDefinitions);
25+
1926
const namePadding =
20-
getLongestNameLength([...options, ...positionalArguments, ...subtasks]) +
21-
GLOBAL_NAME_PADDING;
27+
getLongestNameLength([
28+
...options,
29+
...positionalArguments,
30+
...subtasks,
31+
...globalOptions,
32+
]) + GLOBAL_NAME_PADDING;
2233

2334
let output = `${chalk.bold(task.description)}`;
2435

@@ -28,6 +39,8 @@ export async function getHelpString(task: Task): Promise<string> {
2839
if (subtasks.length > 0) {
2940
output += getSection("AVAILABLE SUBTASKS", subtasks, namePadding);
3041

42+
output += getSection("GLOBAL OPTIONS", globalOptions, namePadding);
43+
3144
output += `\nTo get help for a specific task run: npx hardhat ${task.id.join(" ")} <SUBTASK> --help`;
3245
}
3346

@@ -54,7 +67,7 @@ export async function getHelpString(task: Task): Promise<string> {
5467
output += getSection("AVAILABLE SUBTASKS", subtasks, namePadding);
5568
}
5669

57-
output += `\nFor global options help run: hardhat --help`;
70+
output += getSection("GLOBAL OPTIONS", globalOptions, namePadding);
5871

5972
return output;
6073
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ export async function main(
178178
}
179179

180180
if (builtinGlobalOptions.help || task.isEmpty) {
181-
const taskHelp = await getHelpString(task);
181+
const taskHelp = await getHelpString(task, globalOptionDefinitions);
182182

183183
print(taskHelp);
184184
return;

v-next/hardhat/test/internal/cli/help/get-global-help-string.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,34 @@ To get help for a specific task run: npx hardhat <TASK> [SUBTASK] --help`;
6666
},
6767
],
6868
]);
69-
const globalOptionDefinitions = new Map();
69+
70+
const globalOptionDefinitions = new Map([
71+
[
72+
"userOption1",
73+
{
74+
pluginId: "builtin",
75+
option: globalOption({
76+
name: "userOption1",
77+
description: "userOption1 description.",
78+
type: ArgumentType.STRING,
79+
defaultValue: "default",
80+
}),
81+
},
82+
],
83+
[
84+
"userOption2",
85+
{
86+
pluginId: "builtin",
87+
option: globalOption({
88+
name: "userOption2",
89+
shortName: "o",
90+
description: "userOption2 description.",
91+
type: ArgumentType.STRING,
92+
defaultValue: "default",
93+
}),
94+
},
95+
],
96+
]);
7097

7198
const help = await getGlobalHelpString(tasks, globalOptionDefinitions);
7299

@@ -76,12 +103,13 @@ Usage: hardhat [GLOBAL OPTIONS] <TASK> [SUBTASK] [TASK OPTIONS] [--] [TASK ARGUM
76103
77104
AVAILABLE TASKS:
78105
79-
task1 task1 description
80-
task2 task2 description
106+
task1 task1 description
107+
task2 task2 description
81108
82109
GLOBAL OPTIONS:
83110
84-
111+
--user-option-1 userOption1 description.
112+
--user-option-2, -o userOption2 description.
85113
86114
To get help for a specific task run: npx hardhat <TASK> [SUBTASK] --help`;
87115

v-next/hardhat/test/internal/cli/help/get-help-string.ts

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { describe, it } from "node:test";
55

66
import chalk from "chalk";
77

8+
import { globalOption } from "../../../../src/config.js";
89
import { getHelpString } from "../../../../src/internal/cli/help/get-help-string.js";
910
import { ArgumentType } from "../../../../src/types/arguments.js";
1011

@@ -28,15 +29,48 @@ describe("getHelpString", function () {
2829
run: async () => {},
2930
};
3031

31-
const help = await getHelpString(task);
32+
const globalOptionDefinitions = new Map([
33+
[
34+
"userOption1",
35+
{
36+
pluginId: "builtin",
37+
option: globalOption({
38+
name: "userOption1",
39+
description: "userOption1 description.",
40+
type: ArgumentType.STRING,
41+
defaultValue: "default",
42+
}),
43+
},
44+
],
45+
[
46+
"userOption2",
47+
{
48+
pluginId: "builtin",
49+
option: globalOption({
50+
name: "userOption2",
51+
shortName: "o",
52+
description: "userOption2 description.",
53+
type: ArgumentType.STRING,
54+
defaultValue: "default",
55+
}),
56+
},
57+
],
58+
]);
59+
60+
const help = await getHelpString(task, globalOptionDefinitions);
3261

3362
const expected = `${chalk.bold("task description")}
3463
3564
Usage: hardhat [GLOBAL OPTIONS] task <SUBTASK> [SUBTASK OPTIONS] [--] [SUBTASK POSITIONAL ARGUMENTS]
3665
3766
AVAILABLE SUBTASKS:
3867
39-
task subtask An example empty subtask task
68+
task subtask An example empty subtask task
69+
70+
GLOBAL OPTIONS:
71+
72+
--user-option-1 userOption1 description.
73+
--user-option-2, -o userOption2 description.
4074
4175
To get help for a specific task run: npx hardhat task <SUBTASK> --help`;
4276

@@ -69,7 +103,9 @@ To get help for a specific task run: npx hardhat task <SUBTASK> --help`;
69103
run: async () => {},
70104
};
71105

72-
const help = await getHelpString(task);
106+
const globalOptionDefinitions = new Map();
107+
108+
const help = await getHelpString(task, globalOptionDefinitions);
73109

74110
const expected = `${chalk.bold("task description")}
75111
@@ -80,7 +116,10 @@ OPTIONS:
80116
--another-option Another example option
81117
--option An example option
82118
83-
For global options help run: hardhat --help`;
119+
GLOBAL OPTIONS:
120+
121+
122+
`;
84123

85124
assert.equal(help, expected);
86125
});
@@ -123,7 +162,9 @@ For global options help run: hardhat --help`;
123162
run: async () => {},
124163
};
125164

126-
const help = await getHelpString(task);
165+
const globalOptionDefinitions = new Map();
166+
167+
const help = await getHelpString(task, globalOptionDefinitions);
127168

128169
const expected = `${chalk.bold("task description")}
129170
@@ -139,7 +180,10 @@ POSITIONAL ARGUMENTS:
139180
anotherPositionalArgument Another example positional argument
140181
positionalArgument An example positional argument
141182
142-
For global options help run: hardhat --help`;
183+
GLOBAL OPTIONS:
184+
185+
186+
`;
143187

144188
assert.equal(help, expected);
145189
});
@@ -184,7 +228,9 @@ For global options help run: hardhat --help`;
184228
run: async () => {},
185229
};
186230

187-
const help = await getHelpString(task);
231+
const globalOptionDefinitions = new Map();
232+
233+
const help = await getHelpString(task, globalOptionDefinitions);
188234

189235
const expected = `${chalk.bold("task description")}
190236
@@ -204,7 +250,10 @@ AVAILABLE SUBTASKS:
204250
task another-subtask Another example subtask
205251
task subtask An example subtask
206252
207-
For global options help run: hardhat --help`;
253+
GLOBAL OPTIONS:
254+
255+
256+
`;
208257

209258
assert.equal(help, expected);
210259
});

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

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -307,15 +307,25 @@ Usage: hardhat [GLOBAL OPTIONS] test-task [--opt <STRING>] [--] pos1 [pos2] [var
307307
308308
OPTIONS:
309309
310-
--opt opt description (default: opt default value)
310+
--opt opt description (default: opt default value)
311311
312312
POSITIONAL ARGUMENTS:
313313
314-
pos1 pos1 description
315-
pos2 pos2 description (default: pos2 default value)
316-
var1 var1 description (default: var1 default value 1, var1 default value 2)
314+
pos1 pos1 description
315+
pos2 pos2 description (default: pos2 default value)
316+
var1 var1 description (default: var1 default value 1, var1 default value 2)
317317
318-
For global options help run: hardhat --help`;
318+
GLOBAL OPTIONS:
319+
320+
--build-profile The build profile to use
321+
--config A Hardhat config file.
322+
--coverage Enables code coverage
323+
--help, -h Shows this message, or a task's help if its name is provided.
324+
--init Initializes a Hardhat project.
325+
--network The network to connect to
326+
--show-stack-traces Show stack traces (always enabled on CI servers).
327+
--version Shows hardhat's version.
328+
`;
319329

320330
assert.equal(lines.join(""), expected);
321331
});
@@ -335,16 +345,26 @@ Usage: hardhat [GLOBAL OPTIONS] task [--arg-1 <STRING>] [--arg-4] [--arg-5 <LEVE
335345
336346
OPTIONS:
337347
338-
--arg-1, -o (default: <default-value1>)
339-
--arg-4, -f (default: false)
340-
--arg-5, -l (default: 0)
348+
--arg-1, -o (default: <default-value1>)
349+
--arg-4, -f (default: false)
350+
--arg-5, -l (default: 0)
341351
342352
POSITIONAL ARGUMENTS:
343353
344354
arg2
345355
arg3
346356
347-
For global options help run: hardhat --help`;
357+
GLOBAL OPTIONS:
358+
359+
--build-profile The build profile to use
360+
--config A Hardhat config file.
361+
--coverage Enables code coverage
362+
--help, -h Shows this message, or a task's help if its name is provided.
363+
--init Initializes a Hardhat project.
364+
--network The network to connect to
365+
--show-stack-traces Show stack traces (always enabled on CI servers).
366+
--version Shows hardhat's version.
367+
`;
348368

349369
assert.equal(lines.join(""), expected);
350370
});

0 commit comments

Comments
 (0)