Skip to content

Commit ac33930

Browse files
authored
Merge pull request #7030 from NomicFoundation/parse-flag
fix: global flag option parsing
2 parents 7e228d0 + 06aa2d5 commit ac33930

File tree

4 files changed

+58
-19
lines changed

4 files changed

+58
-19
lines changed

.changeset/spicy-ravens-type.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+
Fixed global flag option parsing ([#7028](https://github.com/NomicFoundation/hardhat/issues/7028))

v-next/hardhat/src/internal/core/arguments.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,7 @@ export function parseArgumentValue(
185185
case ArgumentType.BOOLEAN:
186186
return validateAndParseBoolean(name, value);
187187
case ArgumentType.FLAG:
188-
throw new HardhatError(
189-
HardhatError.ERRORS.CORE.INTERNAL.ASSERTION_ERROR,
190-
{
191-
message: "Flags should never accept values",
192-
},
193-
);
188+
return validateAndParseFlag(name, value);
194189
}
195190
}
196191

@@ -281,3 +276,20 @@ function validateAndParseBoolean(name: string, value: string): boolean {
281276

282277
return normalizedValue === "true";
283278
}
279+
280+
function validateAndParseFlag(name: string, value: string): boolean {
281+
const normalizedValue = value.toLowerCase();
282+
283+
if (normalizedValue !== "true" && normalizedValue !== "false") {
284+
throw new HardhatError(
285+
HardhatError.ERRORS.CORE.ARGUMENTS.INVALID_VALUE_FOR_TYPE,
286+
{
287+
value,
288+
name,
289+
type: ArgumentType.FLAG,
290+
},
291+
);
292+
}
293+
294+
return normalizedValue === "true";
295+
}

v-next/hardhat/test/internal/core/arguments.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,8 @@ describe("Arguments", () => {
183183
);
184184
});
185185

186-
it("should throw when parsing flag arguments values", () => {
187-
assertThrowsHardhatError(
188-
() => {
189-
parseArgumentValue("true", ArgumentType.FLAG, "name");
190-
},
191-
HardhatError.ERRORS.CORE.INTERNAL.ASSERTION_ERROR,
192-
{
193-
message: "Flags should never accept values",
194-
},
195-
);
186+
it("should parse flag arguments", () => {
187+
assert.equal(parseArgumentValue("true", ArgumentType.FLAG, "name"), true);
196188
});
197189

198190
it("should parse level arguments", () => {
@@ -245,9 +237,11 @@ describe("Arguments", () => {
245237
() => {
246238
parseArgumentValue("foo", ArgumentType.FLAG, "name");
247239
},
248-
HardhatError.ERRORS.CORE.INTERNAL.ASSERTION_ERROR,
240+
HardhatError.ERRORS.CORE.ARGUMENTS.INVALID_VALUE_FOR_TYPE,
249241
{
250-
message: "Flags should never accept values",
242+
value: "foo",
243+
name: "name",
244+
type: ArgumentType.FLAG,
251245
},
252246
);
253247
});

v-next/hardhat/test/internal/core/global-options.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import {
1010
RESERVED_ARGUMENT_NAMES,
1111
RESERVED_ARGUMENT_SHORT_NAMES,
1212
} from "../../../src/internal/core/arguments.js";
13-
import { globalOption } from "../../../src/internal/core/config.js";
13+
import {
14+
globalFlag,
15+
globalLevel,
16+
globalOption,
17+
} from "../../../src/internal/core/config.js";
1418
import {
1519
buildGlobalOptionDefinition,
1620
buildGlobalOptionDefinitions,
@@ -457,11 +461,29 @@ describe("Global Options", () => {
457461
type: ArgumentType.BIGINT,
458462
defaultValue: 0n,
459463
}),
464+
globalFlag({
465+
name: "globalOption4",
466+
description: "globalOption4 description",
467+
}),
468+
globalFlag({
469+
name: "globalOption5",
470+
description: "globalOption5 description",
471+
}),
472+
globalLevel({
473+
name: "globalOption6",
474+
description: "globalOption6 description",
475+
}),
476+
globalLevel({
477+
name: "globalOption7",
478+
description: "globalOption7 description",
479+
}),
460480
],
461481
},
462482
]);
463483

464484
setEnvVar("HARDHAT_GLOBAL_OPTION_3", "5n");
485+
setEnvVar("HARDHAT_GLOBAL_OPTION_5", "true");
486+
setEnvVar("HARDHAT_GLOBAL_OPTION_7", "2");
465487

466488
const globalOptions = resolveGlobalOptions(
467489
/* eslint-disable-next-line @typescript-eslint/consistent-type-assertions --
@@ -470,6 +492,8 @@ describe("Global Options", () => {
470492
{
471493
globalOption1: false,
472494
globalOption2: "user",
495+
globalOption4: true,
496+
globalOption6: 2,
473497
} as Partial<GlobalOptions>,
474498
globalOptionDefinitions,
475499
);
@@ -478,6 +502,10 @@ describe("Global Options", () => {
478502
globalOption1: false,
479503
globalOption2: "user",
480504
globalOption3: 5n,
505+
globalOption4: true,
506+
globalOption5: true,
507+
globalOption6: 2,
508+
globalOption7: 2,
481509
});
482510
});
483511

0 commit comments

Comments
 (0)