Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cli-stop-builtin-options-at-separator.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"hardhat": patch
---

Fixed built-in CLI options after `--` being interpreted by Hardhat instead of being passed to a task as positional arguments.
4 changes: 4 additions & 0 deletions packages/hardhat/src/internal/cli/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export async function parseBuiltinGlobalOptions(
for (let i = 0; i < cliArguments.length; i++) {
const arg = cliArguments[i];

if (arg === "--") {
break;
}

if (arg === "--init") {
usedCliArguments[i] = true;
init = true;
Expand Down
73 changes: 73 additions & 0 deletions packages/hardhat/test/internal/cli/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,31 @@ describe("main", function () {
});
});

describe("builtin global options after --", function () {
useFixtureProject("cli/parsing/base-project");

it("should still print the version without --", async function () {
const lines = await runMain("npx hardhat task --version");

assert.deepEqual(lines, [await getHardhatVersion()]);
assert.equal((await getTasksAndSubtaskResults()).wasArg1Used, false);
});

it("should run the task when --version is a positional argument", async function () {
const lines = await runMain("npx hardhat task -- --version value");

assert.deepEqual(lines, []);
assert.equal((await getTasksAndSubtaskResults()).wasArg1Used, true);
});

it("should run the task when --help is a positional argument", async function () {
const lines = await runMain("npx hardhat task -- --help value");

assert.deepEqual(lines, []);
assert.equal((await getTasksAndSubtaskResults()).wasArg1Used, true);
});
});

describe("different configuration file path", function () {
useFixtureProject("cli/parsing/user-config");

Expand Down Expand Up @@ -483,6 +508,37 @@ GLOBAL OPTIONS:
});
});

it("should leave builtin options after -- unconsumed", async function () {
const cliArguments = [
"task",
"--",
"--version",
"--help",
"--show-stack-traces",
"--init",
"--config",
"path",
];
const usedCliArguments = new Array(cliArguments.length).fill(false);

const builtinGlobalOptions = await parseBuiltinGlobalOptions(
cliArguments,
usedCliArguments,
);

assert.deepEqual(
usedCliArguments,
new Array(cliArguments.length).fill(false),
);
assert.deepEqual(builtinGlobalOptions, {
init: false,
configPath: undefined,
showStackTraces: isCi(),
help: false,
version: false,
});
});

it("should recognize the init command", async function () {
const command = "npx hardhat --init --show-stack-traces";

Expand Down Expand Up @@ -1869,6 +1925,23 @@ GLOBAL OPTIONS:
assert.deepEqual(res.taskArguments, { posArg: "--arg" });
});

it("should preserve --show-stack-traces as a positional argument after --", async function () {
const cliArguments = ["task0", "--", "--show-stack-traces"];
const usedCliArguments = new Array(cliArguments.length).fill(false);

const builtinGlobalOptions = await parseBuiltinGlobalOptions(
cliArguments,
usedCliArguments,
);
const res = parseTaskAndArguments(cliArguments, usedCliArguments, hre);

assert.equal(builtinGlobalOptions.showStackTraces, isCi());
assert.ok(!Array.isArray(res), "Result should be an array");
assert.deepEqual(res.taskArguments, {
posArg: "--show-stack-traces",
});
});

it("should get the task, its arguments passed in the cli and ignore global option", function () {
const command =
"npx hardhat task1 --arg <value> --network localhost <posValue> <posValue2> <varValue1> <varValue2>";
Expand Down
Loading