Skip to content

Commit

Permalink
Merge pull request #5681 from NomicFoundation/feature/help-command-sh…
Browse files Browse the repository at this point in the history
…ow-values-for-arguments-and-options

Help messages don't display the default values of options and positional arguments
  • Loading branch information
ChristopherDedominici authored Aug 28, 2024
2 parents f101294 + 5ab19f4 commit b8d9b45
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 18 deletions.
2 changes: 1 addition & 1 deletion v-next/hardhat-errors/src/descriptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ Please double check your arguments.`,
},
INVALID_NAME: {
number: 503,
messageTemplate: "Argument name {name} is invalid",
messageTemplate: `Argument name "{name}" is invalid. It must consist only of alphanumeric characters and cannot start with a number.`,
websiteTitle: "Invalid argument name",
websiteDescription: `One of your Hardhat or task argument names is invalid.
Expand Down
26 changes: 19 additions & 7 deletions v-next/hardhat/src/internal/cli/helpers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ArgumentType } from "@ignored/hardhat-vnext-core/config";
import type { ArgumentTypeToValueType } from "@ignored/hardhat-vnext-core/types/arguments";
import type { GlobalOptionDefinitions } from "@ignored/hardhat-vnext-core/types/global-options";
import type { Task } from "@ignored/hardhat-vnext-core/types/tasks";

Expand All @@ -10,6 +11,7 @@ interface ArgumentDescriptor {
name: string;
description: string;
type?: ArgumentType;
defaultValue?: ArgumentTypeToValueType<ArgumentType>;
isRequired?: boolean;
}

Expand Down Expand Up @@ -67,14 +69,22 @@ export function parseOptions(task: Task): {
name: toCommandLineOption(optionName),
description: option.description,
type: option.type,
...(option.defaultValue !== undefined && {
defaultValue: option.defaultValue,
}),
});
}

for (const argument of task.positionalArguments) {
for (const { name, description, defaultValue } of task.positionalArguments) {
positionalArguments.push({
name: argument.name,
description: argument.description,
isRequired: argument.defaultValue === undefined,
name,
description,
isRequired: defaultValue === undefined,
...(defaultValue !== undefined && {
defaultValue: Array.isArray(defaultValue)
? defaultValue.join(", ")
: defaultValue,
}),
});
}

Expand All @@ -96,9 +106,11 @@ export function getSection(
): string {
return `\n${title}:\n\n${items
.sort((a, b) => a.name.localeCompare(b.name))
.map(
({ name, description }) => ` ${name.padEnd(namePadding)}${description}`,
)
.map(({ name, description, defaultValue }) => {
const defaultValueStr =
defaultValue !== undefined ? ` (default: ${defaultValue})` : "";
return ` ${name.padEnd(namePadding)}${description}${defaultValueStr}`;
})
.join("\n")}\n`;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { HardhatUserConfig } from "@ignored/hardhat-vnext-core/config";

import { task } from "@ignored/hardhat-vnext-core/config";

const customTask = task("test", "subtask")
.setAction(async () => {})
.addOption({
name: "opt",
description: "opt description",
defaultValue: "opt default value",
})
.addPositionalArgument({
name: "pos1",
description: "pos1 description",
})
.addPositionalArgument({
name: "pos2",
description: "pos2 description",
defaultValue: "pos2 default value",
})
.addVariadicArgument({
name: "var1",
description: "var1 description",
defaultValue: ["var1 default value 1", "var1 default value 2"],
})
.build();

const config: HardhatUserConfig = {
tasks: [customTask],
};

export default config;
1 change: 1 addition & 0 deletions v-next/hardhat/test/internal/cli/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ describe("utils", function () {
name: "anotherPositionalArgument",
description: "Another example argument",
isRequired: false,
defaultValue: "default",
},
],
});
Expand Down
55 changes: 45 additions & 10 deletions v-next/hardhat/test/internal/cli/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,24 +244,59 @@ To get help for a specific task run: npx hardhat <TASK> [SUBTASK] --help`;
});

describe("subtask help", function () {
useFixtureProject("cli/parsing/subtask-help");
describe("empty subtask", () => {
useFixtureProject("cli/parsing/subtask-help");

it("should print an help message for the task's subtask", async function () {
let lines: string = "";
it("should print an help message for the task's subtask", async function () {
let lines: string = "";

const command = "npx hardhat empty-task --help";
const cliArguments = command.split(" ").slice(2);
const command = "npx hardhat empty-task --help";
const cliArguments = command.split(" ").slice(2);

await main(cliArguments, (msg) => {
lines = msg;
});
await main(cliArguments, (msg) => {
lines = msg;
});

const expected = `${chalk.bold("empty task description")}
const expected = `${chalk.bold("empty task description")}
Usage: hardhat [GLOBAL OPTIONS] empty-task <SUBTASK> [SUBTASK OPTIONS] [--] [SUBTASK POSITIONAL ARGUMENTS]
`;

assert.equal(lines, expected);
assert.equal(lines, expected);
});
});

describe("task with default values", () => {
useFixtureProject("cli/parsing/default-values");

it("should print the default values for the task's subtask", async function () {
let lines: string = "";

const command = "npx hardhat test subtask --help";
const cliArguments = command.split(" ").slice(2);

await main(cliArguments, (msg) => {
lines = msg;
});

const expected = `${chalk.bold("subtask")}
Usage: hardhat [GLOBAL OPTIONS] test [--opt <STRING>] [--] pos1 [pos2] [var1]
OPTIONS:
--opt opt description (default: opt default value)
POSITIONAL ARGUMENTS:
pos1 pos1 description
pos2 pos2 description (default: pos2 default value)
var1 var1 description (default: var1 default value 1, var1 default value 2)
For global options help run: hardhat --help`;

assert.equal(lines, expected);
});
});
});

Expand Down

0 comments on commit b8d9b45

Please sign in to comment.