forge/opts.rs
1use crate::cmd::{
2 bind::BindArgs, bind_json, build::BuildArgs, cache::CacheArgs, clone::CloneArgs,
3 compiler::CompilerArgs, config, coverage, create::CreateArgs, doc::DocArgs, eip712, flatten,
4 fmt::FmtArgs, fuzz::FuzzArgs, geiger, init::InitArgs, inspect, install::InstallArgs,
5 lint::LintArgs, remappings::RemappingArgs, remove::RemoveArgs, selectors::SelectorsSubcommands,
6 snapshot, soldeer, test, tree, update,
7};
8use clap::{Parser, Subcommand, ValueHint};
9use forge_script::ScriptArgs;
10use forge_verify::{VerifyArgs, VerifyBytecodeArgs, VerifyCheckArgs};
11use foundry_cli::opts::GlobalArgs;
12use foundry_common::version::{LONG_VERSION, SHORT_VERSION};
13use std::path::PathBuf;
14
15/// Build, test, fuzz, debug and deploy Solidity contracts.
16#[derive(Parser)]
17#[command(
18 name = "forge",
19 version = SHORT_VERSION,
20 long_version = LONG_VERSION,
21 after_help = "Find more information in the book: https://getfoundry.sh/forge/overview",
22 next_display_order = None,
23)]
24pub struct Forge {
25 /// Include the global arguments.
26 #[command(flatten)]
27 pub global: GlobalArgs,
28
29 #[command(subcommand)]
30 pub cmd: ForgeSubcommand,
31}
32
33#[derive(Subcommand)]
34pub enum ForgeSubcommand {
35 /// Run the project's tests
36 ///
37 /// Examples:
38 /// - forge test
39 /// - forge test --match-test test_Increment -vvvv (show traces for a matching test)
40 /// - forge test --match-contract CounterTest --fuzz-runs 1000
41 #[command(verbatim_doc_comment, visible_alias = "t")]
42 Test(test::TestArgs),
43
44 /// Run and manage Forge fuzzing corpora.
45 Fuzz(FuzzArgs),
46
47 /// Run a smart contract as a script, building transactions that can be sent onchain
48 ///
49 /// Examples:
50 /// - forge script script/Counter.s.sol (simulate the script locally)
51 /// - forge script script/Counter.s.sol --rpc-url $RPC_URL --broadcast --account dev
52 /// - forge script script/Counter.s.sol --rpc-url $RPC_URL --resume
53 #[command(verbatim_doc_comment)]
54 Script(ScriptArgs),
55
56 /// Generate coverage reports
57 ///
58 /// Examples:
59 /// - forge coverage
60 /// - forge coverage --report lcov --report-file lcov.info
61 /// - forge coverage --match-contract CounterTest
62 #[command(verbatim_doc_comment)]
63 Coverage(coverage::CoverageArgs),
64
65 /// Generate Rust bindings for smart contracts.
66 #[command(alias = "bi")]
67 Bind(BindArgs),
68
69 /// Build the project's smart contracts
70 ///
71 /// Examples:
72 /// - forge build
73 /// - forge build --sizes (print a contract size report)
74 /// - forge build --watch (rebuild on file changes)
75 #[command(verbatim_doc_comment, visible_aliases = ["b", "compile"])]
76 Build {
77 /// Require foundry.lock to match direct Git dependency submodules.
78 #[arg(long)]
79 locked: bool,
80 #[command(flatten)]
81 args: BuildArgs,
82 },
83
84 /// Clone a contract from Etherscan
85 ///
86 /// Examples:
87 /// - forge clone $WETH weth --etherscan-api-key $KEY (clone WETH into ./weth)
88 /// - forge clone --chain sepolia --etherscan-api-key $KEY $ADDRESS my-contract
89 #[command(verbatim_doc_comment)]
90 Clone(CloneArgs),
91
92 /// Update one or multiple dependencies.
93 ///
94 /// If no arguments are provided, then all dependencies are updated.
95 #[command(visible_alias = "u")]
96 Update(update::UpdateArgs),
97
98 /// Install one or multiple dependencies
99 ///
100 /// If no arguments are provided, then existing dependencies will be installed.
101 ///
102 /// Examples:
103 /// - forge install (install all dependencies of the project)
104 /// - forge install openzeppelin/openzeppelin-contracts
105 /// - forge install openzeppelin/openzeppelin-contracts@v5.0.2 (pin a version)
106 #[command(verbatim_doc_comment, visible_aliases = ["i", "add"])]
107 Install(InstallArgs),
108
109 /// Remove one or multiple dependencies.
110 #[command(visible_alias = "rm")]
111 Remove(RemoveArgs),
112
113 /// Get the automatically inferred remappings for the project.
114 #[command(visible_alias = "re")]
115 Remappings(RemappingArgs),
116
117 /// Verify smart contracts on Etherscan and Sourcify
118 ///
119 /// Examples:
120 /// - forge verify-contract --chain sepolia $ADDRESS src/Counter.sol:Counter --watch
121 /// - forge verify-contract $ADDRESS src/Counter.sol:Counter --verifier sourcify
122 #[command(verbatim_doc_comment, visible_alias = "v")]
123 VerifyContract(VerifyArgs),
124
125 /// Check verification status on the selected verifier.
126 #[command(visible_alias = "vc")]
127 VerifyCheck(VerifyCheckArgs),
128
129 /// Verify the deployed bytecode against its source on Etherscan.
130 #[command(visible_alias = "vb")]
131 VerifyBytecode(VerifyBytecodeArgs),
132
133 /// Deploy a smart contract
134 ///
135 /// Examples:
136 /// - forge create Counter --rpc-url $RPC_URL --account dev --broadcast
137 /// - forge create Token --private-key $PK --broadcast --constructor-args Token TKN
138 #[command(verbatim_doc_comment, visible_alias = "c")]
139 Create(CreateArgs),
140
141 /// Create a new Forge project.
142 Init(InitArgs),
143
144 /// Generate shell completions script.
145 #[command(visible_alias = "com")]
146 Completions {
147 #[arg(value_enum)]
148 shell: foundry_cli::clap::Shell,
149 },
150
151 /// Remove the build artifacts and cache directories.
152 #[command(visible_alias = "cl")]
153 Clean {
154 /// The project's root path.
155 ///
156 /// By default root of the Git repository, if in one,
157 /// or the current working directory.
158 #[arg(long, value_hint = ValueHint::DirPath, value_name = "PATH")]
159 root: Option<PathBuf>,
160 },
161
162 /// Manage the Foundry cache.
163 Cache(CacheArgs),
164
165 /// Create a gas snapshot of each test's gas usage
166 ///
167 /// Examples:
168 /// - forge snapshot
169 /// - forge snapshot --diff (compare against the existing .gas-snapshot file)
170 /// - forge snapshot --check (fail if gas usage does not match .gas-snapshot)
171 #[command(verbatim_doc_comment, visible_alias = "s")]
172 Snapshot(snapshot::GasSnapshotArgs),
173
174 /// Display the current config.
175 #[command(visible_alias = "co")]
176 Config(config::ConfigArgs),
177
178 /// Flatten a source file and all of its imports into one file.
179 #[command(visible_alias = "f")]
180 Flatten(flatten::FlattenArgs),
181
182 /// Format Solidity source files
183 ///
184 /// Examples:
185 /// - forge fmt
186 /// - forge fmt --check (report formatting issues without writing changes)
187 /// - forge fmt src/Counter.sol
188 #[command(verbatim_doc_comment)]
189 Fmt(FmtArgs),
190
191 /// Lint Solidity source files
192 #[command(visible_alias = "l")]
193 Lint(LintArgs),
194
195 /// Get specialized information about a smart contract
196 ///
197 /// Examples:
198 /// - forge inspect Counter abi
199 /// - forge inspect Counter bytecode
200 /// - forge inspect src/Counter.sol:Counter storageLayout
201 #[command(verbatim_doc_comment, visible_alias = "in")]
202 Inspect(inspect::InspectArgs),
203
204 /// Display a tree visualization of the project's dependency graph.
205 #[command(visible_alias = "tr")]
206 Tree(tree::TreeArgs),
207
208 /// DEPRECATED: Detects usage of unsafe cheat codes in a project and its dependencies.
209 ///
210 /// This is an alias for `forge lint --only-lint unsafe-cheatcode`.
211 Geiger(geiger::GeigerArgs),
212
213 /// Generate documentation for the project.
214 Doc(DocArgs),
215
216 /// Function selector utilities.
217 #[command(visible_alias = "se")]
218 Selectors {
219 #[command(subcommand)]
220 command: SelectorsSubcommands,
221 },
222
223 /// Compiler utilities.
224 Compiler(CompilerArgs),
225
226 /// Soldeer dependency manager.
227 Soldeer(soldeer::SoldeerArgs),
228
229 /// Generate EIP-712 struct encodings for structs from a given file.
230 Eip712(eip712::Eip712Args),
231
232 /// Generate bindings for serialization/deserialization of project structs via JSON cheatcodes.
233 BindJson(bind_json::BindJsonArgs),
234}
235
236#[cfg(test)]
237mod tests {
238 use super::*;
239 use clap::CommandFactory;
240
241 #[test]
242 fn verify_cli() {
243 Forge::command().debug_assert();
244 }
245}