MCPcopy Create free account
hub / github.com/NomicFoundation/hardhat / parseOptions

Function parseOptions

packages/hardhat/src/internal/cli/parser.ts:256–394  ·  view source on GitHub ↗
(
  cliArguments: string[],
  usedCliArguments: boolean[],
  optionDefinitions: Map<string, OptionDefinition>,
  providedArguments: TaskArguments,
  ignoreUnknownOption = false,
)

Source from the content-addressed store, hash-verified

254}
255
256function parseOptions(
257 cliArguments: string[],
258 usedCliArguments: boolean[],
259 optionDefinitions: Map<string, OptionDefinition>,
260 providedArguments: TaskArguments,
261 ignoreUnknownOption = false,
262) {
263 const optionDefinitionsByShortName = new Map<string, OptionDefinition>();
264 for (const optionDefinition of optionDefinitions.values()) {
265 if (optionDefinition.shortName !== undefined) {
266 optionDefinitionsByShortName.set(
267 optionDefinition.shortName,
268 optionDefinition,
269 );
270 }
271 }
272
273 for (let i = 0; i < cliArguments.length; i++) {
274 if (usedCliArguments[i]) {
275 continue;
276 }
277
278 if (cliArguments[i] === "--") {
279 /* A standalone '--' is ok because it is used to separate CLI tool arguments
280 * from task arguments, ensuring the tool passes subsequent options directly
281 * to the task. Everything after "--" should be considered as a positional
282 * argument. */
283 break;
284 }
285
286 const arg = cliArguments[i];
287
288 let optionDefinition: OptionDefinition | undefined;
289
290 const providedByName = arg.startsWith("--");
291 const providedByShortName = !providedByName && arg.startsWith("-");
292
293 if (providedByName) {
294 const name = kebabToCamelCase(arg.substring(2));
295 optionDefinition = optionDefinitions.get(name);
296 } else if (providedByShortName) {
297 const shortName = arg[1];
298
299 // Check if the short name is valid
300 if (Array.from(arg.substring(1)).some((c) => c !== shortName)) {
301 throw new HardhatError(
302 HardhatError.ERRORS.CORE.ARGUMENTS.CANNOT_GROUP_OPTIONS,
303 {
304 option: arg,
305 },
306 );
307 }
308
309 optionDefinition = optionDefinitionsByShortName.get(shortName);
310 } else {
311 continue;
312 }
313

Callers 3

parseGlobalOptionsFunction · 0.70
parseTaskArgumentsFunction · 0.70
utils.tsFile · 0.50

Calls 3

kebabToCamelCaseFunction · 0.90
parseArgumentValueFunction · 0.85
getMethod · 0.65

Tested by

no test coverage detected