Skip to content

Commit 175394a

Browse files
authored
Merge pull request #5495 from NomicFoundation/sort-help-messages
Sort help messages
2 parents 590b1b1 + 1a6f115 commit 175394a

File tree

5 files changed

+81
-34
lines changed

5 files changed

+81
-34
lines changed

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

+13-3
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,12 @@ export function getSection(
9999
items: ArgumentDescriptor[],
100100
namePadding: number,
101101
): string {
102-
return `\n${title}:\n\n${items.map(({ name, description }) => ` ${name.padEnd(namePadding)}${description}`).join("\n")}\n`;
102+
return `\n${title}:\n\n${items
103+
.sort((a, b) => a.name.localeCompare(b.name))
104+
.map(
105+
({ name, description }) => ` ${name.padEnd(namePadding)}${description}`,
106+
)
107+
.join("\n")}\n`;
103108
}
104109

105110
export function getUsageString(
@@ -110,11 +115,16 @@ export function getUsageString(
110115
let output = `Usage: hardhat [GLOBAL OPTIONS] ${task.id.join(" ")}`;
111116

112117
if (options.length > 0) {
113-
output += ` ${options.map((o) => `[${o.name}${o.type === "BOOLEAN" ? "" : ` <${o.type}>`}]`).join(" ")}`;
118+
output += ` ${options
119+
.sort((a, b) => a.name.localeCompare(b.name))
120+
.map((o) => `[${o.name}${o.type === "BOOLEAN" ? "" : ` <${o.type}>`}]`)
121+
.join(" ")}`;
114122
}
115123

116124
if (positionalArguments.length > 0) {
117-
output += ` [--] ${positionalArguments.map((a) => (a.isRequired === true ? a.name : `[${a.name}]`)).join(" ")}`;
125+
output += ` [--] ${positionalArguments
126+
.map((a) => (a.isRequired === true ? a.name : `[${a.name}]`))
127+
.join(" ")}`;
118128
}
119129

120130
return output;

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,9 @@ GLOBAL OPTIONS:
197197
--help Shows this message, or a task's help if its name is provided.
198198
--init Initializes a Hardhat project.
199199
--show-stack-traces Show stack traces (always enabled on CI servers).
200-
--version Shows hardhat's version.
201200
--user-option-1 userOption1 description.
202201
--user-option-2 userOption2 description.
202+
--version Shows hardhat's version.
203203
204204
To get help for a specific task run: npx hardhat <TASK> [SUBTASK] --help`;
205205

@@ -208,7 +208,7 @@ To get help for a specific task run: npx hardhat <TASK> [SUBTASK] --help`;
208208
});
209209

210210
describe("when there are tasks, subtasks and global options", function () {
211-
it("should return the global help string with the tasks, subtasks and global options", async function () {
211+
it("should return the global help string with the tasks, subtasks and global options, all sorted by name", async function () {
212212
const tasks: Map<string, Task> = new Map([
213213
[
214214
"task1",
@@ -295,9 +295,9 @@ GLOBAL OPTIONS:
295295
--help Shows this message, or a task's help if its name is provided.
296296
--init Initializes a Hardhat project.
297297
--show-stack-traces Show stack traces (always enabled on CI servers).
298-
--version Shows hardhat's version.
299298
--user-option-1 userOption1 description.
300299
--user-option-2 userOption2 description.
300+
--version Shows hardhat's version.
301301
302302
To get help for a specific task run: npx hardhat <TASK> [SUBTASK] --help`;
303303

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

+31-18
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ To get help for a specific task run: npx hardhat task <SUBTASK> --help`;
4646

4747
describe("when the task is not empty", function () {
4848
describe("when there are options", function () {
49-
it("should return the task's help string", async function () {
49+
it("should return the task's help string with options sorted by name", async function () {
5050
const task: Task = {
5151
id: ["task"],
5252
description: "task description",
@@ -73,12 +73,12 @@ To get help for a specific task run: npx hardhat task <SUBTASK> --help`;
7373

7474
const expected = `${chalk.bold("task description")}
7575
76-
Usage: hardhat [GLOBAL OPTIONS] task [--option <STRING>] [--another-option]
76+
Usage: hardhat [GLOBAL OPTIONS] task [--another-option] [--option <STRING>]
7777
7878
OPTIONS:
7979
80-
--option An example option
8180
--another-option Another example option
81+
--option An example option
8282
8383
For global options help run: hardhat --help`;
8484

@@ -87,7 +87,7 @@ For global options help run: hardhat --help`;
8787
});
8888

8989
describe("when there are positional arguments", function () {
90-
it("should return the task's help string", async function () {
90+
it("should return the task's help string with options and positional arguments sorted by name, except in the usage string", async function () {
9191
const task: Task = {
9292
id: ["task"],
9393
description: "task description",
@@ -110,6 +110,12 @@ For global options help run: hardhat --help`;
110110
type: ArgumentType.STRING,
111111
isVariadic: false,
112112
},
113+
{
114+
name: "anotherPositionalArgument",
115+
description: "Another example positional argument",
116+
type: ArgumentType.STRING,
117+
isVariadic: false,
118+
},
113119
],
114120
pluginId: "task-plugin-id",
115121
subtasks: new Map(),
@@ -121,16 +127,17 @@ For global options help run: hardhat --help`;
121127

122128
const expected = `${chalk.bold("task description")}
123129
124-
Usage: hardhat [GLOBAL OPTIONS] task [--option <STRING>] [--another-option] [--] positionalArgument
130+
Usage: hardhat [GLOBAL OPTIONS] task [--another-option] [--option <STRING>] [--] positionalArgument anotherPositionalArgument
125131
126132
OPTIONS:
127133
128-
--option An example option
129-
--another-option Another example option
134+
--another-option Another example option
135+
--option An example option
130136
131137
POSITIONAL ARGUMENTS:
132138
133-
positionalArgument An example positional argument
139+
anotherPositionalArgument Another example positional argument
140+
positionalArgument An example positional argument
134141
135142
For global options help run: hardhat --help`;
136143

@@ -139,7 +146,7 @@ For global options help run: hardhat --help`;
139146
});
140147

141148
describe("when there are subtasks", function () {
142-
it("should return the task's help string", async function () {
149+
it("should return the task's help string with subtasks sorted by name", async function () {
143150
const task: Task = {
144151
id: ["task"],
145152
description: "task description",
@@ -164,10 +171,15 @@ For global options help run: hardhat --help`;
164171
},
165172
],
166173
pluginId: "task-plugin-id",
167-
subtasks: new Map().set("subtask", {
168-
id: ["task", "subtask"],
169-
description: "An example subtask",
170-
}),
174+
subtasks: new Map()
175+
.set("subtask", {
176+
id: ["task", "subtask"],
177+
description: "An example subtask",
178+
})
179+
.set("another-subtask", {
180+
id: ["task", "another-subtask"],
181+
description: "Another example subtask",
182+
}),
171183
isEmpty: false,
172184
run: async () => {},
173185
};
@@ -176,20 +188,21 @@ For global options help run: hardhat --help`;
176188

177189
const expected = `${chalk.bold("task description")}
178190
179-
Usage: hardhat [GLOBAL OPTIONS] task [--option <STRING>] [--another-option] [--] positionalArgument
191+
Usage: hardhat [GLOBAL OPTIONS] task [--another-option] [--option <STRING>] [--] positionalArgument
180192
181193
OPTIONS:
182194
183-
--option An example option
184-
--another-option Another example option
195+
--another-option Another example option
196+
--option An example option
185197
186198
POSITIONAL ARGUMENTS:
187199
188-
positionalArgument An example positional argument
200+
positionalArgument An example positional argument
189201
190202
AVAILABLE SUBTASKS:
191203
192-
task subtask An example subtask
204+
task another-subtask Another example subtask
205+
task subtask An example subtask
193206
194207
For global options help run: hardhat --help`;
195208

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

+33-9
Original file line numberDiff line numberDiff line change
@@ -196,23 +196,25 @@ describe("utils", function () {
196196
});
197197

198198
describe("getSection", function () {
199-
it("should return a section", function () {
199+
it("should return a section with items sorted by name", function () {
200200
const section = getSection(
201201
"Section Title",
202202
[
203203
{ name: "content", description: "content description" },
204204
{ name: "content2", description: "content description2" },
205205
{ name: "content3", description: "content description3" },
206+
{ name: "another-item", description: "content description4" },
206207
],
207-
14,
208+
18,
208209
);
209210

210211
const expected = `
211212
Section Title:
212213
213-
content content description
214-
content2 content description2
215-
content3 content description3
214+
another-item content description4
215+
content content description
216+
content2 content description2
217+
content3 content description3
216218
`;
217219

218220
assert.equal(section, expected);
@@ -221,7 +223,7 @@ Section Title:
221223

222224
describe("getUsageString", function () {
223225
describe("with a required positional argument", function () {
224-
it("should return a usage string", function () {
226+
it("should return a usage string with options sorted by name, preserving positional argument order", function () {
225227
const task: Task = {
226228
id: ["task"],
227229
description: "task description",
@@ -244,6 +246,12 @@ Section Title:
244246
type: ArgumentType.STRING,
245247
isVariadic: false,
246248
},
249+
{
250+
name: "anotherPositionalArgument",
251+
description: "Another example argument",
252+
type: ArgumentType.STRING,
253+
isVariadic: false,
254+
},
247255
],
248256
pluginId: "task-plugin-id",
249257
subtasks: new Map(),
@@ -271,17 +279,22 @@ Section Title:
271279
description: "An example argument",
272280
isRequired: true,
273281
},
282+
{
283+
name: "anotherPositionalArgument",
284+
description: "Another example argument",
285+
isRequired: true,
286+
},
274287
],
275288
);
276289

277-
const expected = `Usage: hardhat [GLOBAL OPTIONS] task [--option <STRING>] [--another-option] [--] positionalArgument`;
290+
const expected = `Usage: hardhat [GLOBAL OPTIONS] task [--another-option] [--option <STRING>] [--] positionalArgument anotherPositionalArgument`;
278291

279292
assert.equal(usageString, expected);
280293
});
281294
});
282295

283296
describe("with an optional positional argument", function () {
284-
it("should return a usage string", function () {
297+
it("should return a usage string with options sorted by name, preserving positional argument order", function () {
285298
const task: Task = {
286299
id: ["task"],
287300
description: "task description",
@@ -304,6 +317,12 @@ Section Title:
304317
type: ArgumentType.STRING,
305318
isVariadic: false,
306319
},
320+
{
321+
name: "anotherPositionalArgument",
322+
description: "Another example argument",
323+
type: ArgumentType.STRING,
324+
isVariadic: false,
325+
},
307326
],
308327
pluginId: "task-plugin-id",
309328
subtasks: new Map(),
@@ -331,10 +350,15 @@ Section Title:
331350
description: "An example argument",
332351
isRequired: false,
333352
},
353+
{
354+
name: "anotherPositionalArgument",
355+
description: "Another example argument",
356+
isRequired: false,
357+
},
334358
],
335359
);
336360

337-
const expected = `Usage: hardhat [GLOBAL OPTIONS] task [--option <STRING>] [--another-option] [--] [positionalArgument]`;
361+
const expected = `Usage: hardhat [GLOBAL OPTIONS] task [--another-option] [--option <STRING>] [--] [positionalArgument] [anotherPositionalArgument]`;
338362

339363
assert.equal(usageString, expected);
340364
});

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,11 @@ AVAILABLE TASKS:
229229
GLOBAL OPTIONS:
230230
231231
--config A Hardhat config file.
232+
--foo-plugin-flag A flag
232233
--help Shows this message, or a task's help if its name is provided.
233234
--init Initializes a Hardhat project.
234235
--show-stack-traces Show stack traces (always enabled on CI servers).
235236
--version Shows hardhat's version.
236-
--foo-plugin-flag A flag
237237
238238
To get help for a specific task run: npx hardhat <TASK> [SUBTASK] --help`;
239239

0 commit comments

Comments
 (0)