Skip to content

Commit bb8dfe3

Browse files
committed
make positional params optional with default value
1 parent 4ebe42e commit bb8dfe3

File tree

4 files changed

+148
-62
lines changed

4 files changed

+148
-62
lines changed

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,16 @@ export function parseSubtasks(
5959

6060
export function parseOptions(task: Task): {
6161
options: Array<{ name: string; description: string; type: ParameterType }>;
62-
positionalArguments: Array<{ name: string; description: string }>;
62+
positionalArguments: Array<{
63+
name: string;
64+
description: string;
65+
isRequired: boolean;
66+
}>;
6367
} {
6468
const options = [];
6569
const positionalArguments = [];
6670

67-
for (const [optionName, option] of task.namedParameters) {
71+
for (const [optionName, option] of task.options) {
6872
options.push({
6973
name: formatOptionName(optionName),
7074
description: option.description,
@@ -76,6 +80,7 @@ export function parseOptions(task: Task): {
7680
positionalArguments.push({
7781
name: argument.name,
7882
description: argument.description,
83+
isRequired: argument.defaultValue === undefined,
7984
});
8085
}
8186

@@ -117,7 +122,7 @@ export function getUsageString(
117122
}
118123

119124
if (positionalArguments.length > 0) {
120-
output += ` [--] ${positionalArguments.map((a) => a.name).join(" ")}`;
125+
output += ` [--] ${positionalArguments.map((a) => (a.isRequired ? a.name : `[${a.name}]`)).join(" ")}`;
121126
}
122127

123128
return output;

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ To get help for a specific task run: npx hardhat <TASK> [SUBTASK] --help`;
3838
id: ["task1"],
3939
description: "task1 description",
4040
actions: [{ pluginId: "task1-plugin-id", action: () => {} }],
41-
namedParameters: new Map(),
41+
options: new Map(),
4242
positionalParameters: [],
4343
pluginId: "task1-plugin-id",
4444
subtasks: new Map(),
@@ -52,7 +52,7 @@ To get help for a specific task run: npx hardhat <TASK> [SUBTASK] --help`;
5252
id: ["task2"],
5353
description: "task2 description",
5454
actions: [{ pluginId: "task2-plugin-id", action: () => {} }],
55-
namedParameters: new Map(),
55+
options: new Map(),
5656
positionalParameters: [],
5757
pluginId: "task2-plugin-id",
5858
subtasks: new Map(),
@@ -95,7 +95,7 @@ To get help for a specific task run: npx hardhat <TASK> [SUBTASK] --help`;
9595
id: ["task1"],
9696
description: "task1 description",
9797
actions: [{ pluginId: "task1-plugin-id", action: () => {} }],
98-
namedParameters: new Map(),
98+
options: new Map(),
9999
positionalParameters: [],
100100
pluginId: "task1-plugin-id",
101101
subtasks: new Map().set("subtask1", {
@@ -119,7 +119,7 @@ To get help for a specific task run: npx hardhat <TASK> [SUBTASK] --help`;
119119
id: ["task2"],
120120
description: "task2 description",
121121
actions: [{ pluginId: "task2-plugin-id", action: () => {} }],
122-
namedParameters: new Map(),
122+
options: new Map(),
123123
positionalParameters: [],
124124
pluginId: "task2-plugin-id",
125125
subtasks: new Map(),

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe("getHelpString", function () {
1515
id: ["task"],
1616
description: "task description",
1717
actions: [{ pluginId: "task-plugin-id", action: () => {} }],
18-
namedParameters: new Map(),
18+
options: new Map(),
1919
positionalParameters: [],
2020
pluginId: "task-plugin-id",
2121
subtasks: new Map().set("subtask", {
@@ -51,7 +51,7 @@ To get help for a specific task run: npx hardhat task <SUBTASK> --help`;
5151
id: ["task"],
5252
description: "task description",
5353
actions: [{ pluginId: "task-plugin-id", action: () => {} }],
54-
namedParameters: new Map()
54+
options: new Map()
5555
.set("option", {
5656
name: "option",
5757
description: "An example option",
@@ -92,7 +92,7 @@ For global options help run: hardhat --help`;
9292
id: ["task"],
9393
description: "task description",
9494
actions: [{ pluginId: "task-plugin-id", action: () => {} }],
95-
namedParameters: new Map()
95+
options: new Map()
9696
.set("option", {
9797
name: "option",
9898
description: "An example option",
@@ -144,7 +144,7 @@ For global options help run: hardhat --help`;
144144
id: ["task"],
145145
description: "task description",
146146
actions: [{ pluginId: "task-plugin-id", action: () => {} }],
147-
namedParameters: new Map()
147+
options: new Map()
148148
.set("option", {
149149
name: "option",
150150
description: "An example option",

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

+132-51
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe("utils", function () {
2222
id: ["task"],
2323
description: "task description",
2424
actions: [{ pluginId: "task-plugin-id", action: () => {} }],
25-
namedParameters: new Map(),
25+
options: new Map(),
2626
positionalParameters: [],
2727
pluginId: "task-plugin-id",
2828
subtasks: new Map().set("subtask", {
@@ -53,7 +53,7 @@ describe("utils", function () {
5353
id: ["task"],
5454
description: "task description",
5555
actions: [{ pluginId: "task-plugin-id", action: () => {} }],
56-
namedParameters: new Map(),
56+
options: new Map(),
5757
positionalParameters: [],
5858
pluginId: "task-plugin-id",
5959
subtasks: new Map().set("subtask", {
@@ -86,7 +86,7 @@ describe("utils", function () {
8686
id: ["task"],
8787
description: "task description",
8888
actions: [{ pluginId: "task-plugin-id", action: () => {} }],
89-
namedParameters: new Map(),
89+
options: new Map(),
9090
positionalParameters: [],
9191
pluginId: "task-plugin-id",
9292
subtasks: new Map().set("subtask", {
@@ -116,7 +116,7 @@ describe("utils", function () {
116116
id: ["task"],
117117
description: "task description",
118118
actions: [{ pluginId: "task-plugin-id", action: () => {} }],
119-
namedParameters: new Map()
119+
options: new Map()
120120
.set("option", {
121121
name: "option",
122122
description: "An example option",
@@ -134,6 +134,13 @@ describe("utils", function () {
134134
parameterType: ParameterType.STRING,
135135
isVariadic: false,
136136
},
137+
{
138+
name: "anotherPositionalArgument",
139+
description: "Another example argument",
140+
parameterType: ParameterType.STRING,
141+
isVariadic: false,
142+
defaultValue: "default",
143+
},
137144
],
138145
pluginId: "task-plugin-id",
139146
subtasks: new Map(),
@@ -160,6 +167,12 @@ describe("utils", function () {
160167
{
161168
name: "positionalArgument",
162169
description: "An example argument",
170+
isRequired: true,
171+
},
172+
{
173+
name: "anotherPositionalArgument",
174+
description: "Another example argument",
175+
isRequired: false,
163176
},
164177
],
165178
});
@@ -207,56 +220,124 @@ Section Title:
207220
});
208221

209222
describe("getUsageString", function () {
210-
it("should return a usage string", function () {
211-
const task: Task = {
212-
id: ["task"],
213-
description: "task description",
214-
actions: [{ pluginId: "task-plugin-id", action: () => {} }],
215-
namedParameters: new Map()
216-
.set("option", {
217-
name: "option",
218-
description: "An example option",
219-
parameterType: ParameterType.STRING,
220-
})
221-
.set("anotherOption", {
222-
name: "anotherOption",
223-
description: "Another example option",
224-
parameterType: ParameterType.BOOLEAN,
225-
}),
226-
positionalParameters: [
227-
{
228-
name: "positionalArgument",
229-
description: "An example argument",
230-
parameterType: ParameterType.STRING,
231-
isVariadic: false,
232-
},
233-
],
234-
pluginId: "task-plugin-id",
235-
subtasks: new Map(),
236-
isEmpty: false,
237-
run: async () => {},
238-
};
223+
describe("with a required positional parameter", function () {
224+
it("should return a usage string", function () {
225+
const task: Task = {
226+
id: ["task"],
227+
description: "task description",
228+
actions: [{ pluginId: "task-plugin-id", action: () => {} }],
229+
options: new Map()
230+
.set("option", {
231+
name: "option",
232+
description: "An example option",
233+
parameterType: ParameterType.STRING,
234+
})
235+
.set("anotherOption", {
236+
name: "anotherOption",
237+
description: "Another example option",
238+
parameterType: ParameterType.BOOLEAN,
239+
}),
240+
positionalParameters: [
241+
{
242+
name: "positionalArgument",
243+
description: "An example argument",
244+
parameterType: ParameterType.STRING,
245+
isVariadic: false,
246+
},
247+
],
248+
pluginId: "task-plugin-id",
249+
subtasks: new Map(),
250+
isEmpty: false,
251+
run: async () => {},
252+
};
239253

240-
const usageString = getUsageString(
241-
task,
242-
[
243-
{
244-
name: "--option",
245-
description: "An example option",
246-
type: ParameterType.STRING,
247-
},
248-
{
249-
name: "--another-option",
250-
description: "Another example option",
251-
type: ParameterType.BOOLEAN,
252-
},
253-
],
254-
[{ name: "positionalArgument", description: "An example argument" }],
255-
);
254+
const usageString = getUsageString(
255+
task,
256+
[
257+
{
258+
name: "--option",
259+
description: "An example option",
260+
type: ParameterType.STRING,
261+
},
262+
{
263+
name: "--another-option",
264+
description: "Another example option",
265+
type: ParameterType.BOOLEAN,
266+
},
267+
],
268+
[
269+
{
270+
name: "positionalArgument",
271+
description: "An example argument",
272+
isRequired: true,
273+
},
274+
],
275+
);
256276

257-
const expected = `Usage: hardhat [GLOBAL OPTIONS] task [--option <STRING>] [--another-option] [--] positionalArgument`;
277+
const expected = `Usage: hardhat [GLOBAL OPTIONS] task [--option <STRING>] [--another-option] [--] positionalArgument`;
278+
279+
assert.equal(usageString, expected);
280+
});
281+
});
258282

259-
assert.equal(usageString, expected);
283+
describe("with an optional positional parameter", function () {
284+
it("should return a usage string", function () {
285+
const task: Task = {
286+
id: ["task"],
287+
description: "task description",
288+
actions: [{ pluginId: "task-plugin-id", action: () => {} }],
289+
options: new Map()
290+
.set("option", {
291+
name: "option",
292+
description: "An example option",
293+
parameterType: ParameterType.STRING,
294+
})
295+
.set("anotherOption", {
296+
name: "anotherOption",
297+
description: "Another example option",
298+
parameterType: ParameterType.BOOLEAN,
299+
}),
300+
positionalParameters: [
301+
{
302+
name: "positionalArgument",
303+
description: "An example argument",
304+
parameterType: ParameterType.STRING,
305+
isVariadic: false,
306+
},
307+
],
308+
pluginId: "task-plugin-id",
309+
subtasks: new Map(),
310+
isEmpty: false,
311+
run: async () => {},
312+
};
313+
314+
const usageString = getUsageString(
315+
task,
316+
[
317+
{
318+
name: "--option",
319+
description: "An example option",
320+
type: ParameterType.STRING,
321+
},
322+
{
323+
name: "--another-option",
324+
description: "Another example option",
325+
type: ParameterType.BOOLEAN,
326+
},
327+
],
328+
[
329+
{
330+
name: "positionalArgument",
331+
description: "An example argument",
332+
isRequired: false,
333+
},
334+
],
335+
);
336+
337+
const expected = `Usage: hardhat [GLOBAL OPTIONS] task [--option <STRING>] [--another-option] [--] [positionalArgument]`;
338+
339+
assert.equal(usageString, expected);
340+
});
260341
});
261342
});
262343
});

0 commit comments

Comments
 (0)