javascript - 带参数运行的安全帽
问题描述
我需要从命令行使用 hardhat 运行特定的 ts 脚本,但我需要指定参数...与此类似:
npx hardhat run --network rinkeby scripts/task-executor.ts param1 param2
其中--network rinkeby
是安全帽运行的参数 和
是task -executor.ts脚本的参数。
我找不到有关此问题的任何帖子,也无法使其正常工作。param1
param2
我还尝试定义一个安全帽任务并添加了这些参数,但如果我尝试执行它,我会得到:
Error HH9: Error while loading Hardhat's configuration.
You probably tried to import the "hardhat" module from your config or a file imported from it.
This is not possible, as Hardhat can't be initialized while its config is being defined.
因为我需要在该特定任务中导入hre
或ethers
从中导入。hardhat
有人知道如何完成我需要的吗?
非常感谢!!
解决方案
根据安全帽:
Hardhat 脚本对于不接受用户参数的简单事物以及与不太适合 Hardhat CLI 的外部工具(如 Node.js 调试器)集成很有用。
对于需要参数的脚本,您应该使用Hardhat Tasks。
您可以将任务编码在与hardhat.config.ts不同的文件中。这是使用文件sampleTask.ts中的位置参数的示例任务:
import { task } from "hardhat/config";
task("sampleTask", "A sample task with params")
.addPositionalParam("param1")
.addPositionalParam("param2")
.setAction(async (taskArgs) => {
console.log(taskArgs);
});
请记住将其导入hardhat.config.ts:
import "./tasks/sampleTask";
然后运行它:
npx hardhat sampleTask hello world
它应该打印:
{ param1: 'hello', param2: 'world' }
您可以在此处阅读有关任务的命名、位置和可选参数的更多信息。
如果需要使用hre
or ethers
,可以hre
从函数的第二个参数中获取setAction
:
task("sampleTask", "A sample task with params")
.addPositionalParam("param1")
.addPositionalParam("param2")
.setAction(async (taskArgs, hre) => {
const ethers = hre.ethers;
});
推荐阅读
- r - 我如何同时注释树的节点并使用 ggtree 为某些分支着色?
- c# - IIS 和单线程应用程序
- python - 如何使用来自同一数据帧的数据切割使用 seaborn 创建多个箱线图
- ag-grid - 我可以在列级别而不是行上有 2 条分组信息吗?
- python - 接受来自 WebRTC 信号的报价(在 Python 中)
- c# - 如果没有“临时地址”,则填充“家庭地址”的 C# 条件字符串,反之亦然。请
- azure - 什么在调用管理功能?
- javascript - 使用 Node.js 访问特定网页时的 400 状态代码。从我的浏览器访问同一个网页时会出现 200 个状态码。是什么赋予了?
- jenkins-pipeline - 从 Groovy 脚本设置管道变量的值
- java-8 - Java 8 List of List of Objects flatmap 获取最里面的列表