forge/
args.rs

1use crate::{
2    cmd::{cache::CacheSubcommands, generate::GenerateSubcommands, watch},
3    opts::{Forge, ForgeSubcommand},
4};
5use clap::{CommandFactory, Parser};
6use clap_complete::generate;
7use eyre::Result;
8use foundry_cli::{handler, utils};
9use foundry_common::shell;
10use foundry_evm::inspectors::cheatcodes::{set_execution_context, ForgeContext};
11
12/// Run the `forge` command line interface.
13pub fn run() -> Result<()> {
14    setup()?;
15
16    let args = Forge::parse();
17    args.global.init()?;
18
19    run_command(args)
20}
21
22/// Setup the global logger and other utilities.
23pub fn setup() -> Result<()> {
24    utils::install_crypto_provider();
25    handler::install();
26    utils::load_dotenv();
27    utils::subscriber();
28    utils::enable_paint();
29
30    Ok(())
31}
32
33/// Run the subcommand.
34pub fn run_command(args: Forge) -> Result<()> {
35    // Set the execution context based on the subcommand.
36    let context = match &args.cmd {
37        ForgeSubcommand::Test(_) => ForgeContext::Test,
38        ForgeSubcommand::Coverage(_) => ForgeContext::Coverage,
39        ForgeSubcommand::Snapshot(_) => ForgeContext::Snapshot,
40        ForgeSubcommand::Script(cmd) => {
41            if cmd.broadcast {
42                ForgeContext::ScriptBroadcast
43            } else if cmd.resume {
44                ForgeContext::ScriptResume
45            } else {
46                ForgeContext::ScriptDryRun
47            }
48        }
49        _ => ForgeContext::Unknown,
50    };
51    set_execution_context(context);
52
53    // Run the subcommand.
54    match args.cmd {
55        ForgeSubcommand::Test(cmd) => {
56            if cmd.is_watch() {
57                utils::block_on(watch::watch_test(cmd))
58            } else {
59                let silent = cmd.junit || shell::is_json();
60                let outcome = utils::block_on(cmd.run())?;
61                outcome.ensure_ok(silent)
62            }
63        }
64        ForgeSubcommand::Script(cmd) => utils::block_on(cmd.run_script()),
65        ForgeSubcommand::Coverage(cmd) => {
66            if cmd.is_watch() {
67                utils::block_on(watch::watch_coverage(cmd))
68            } else {
69                utils::block_on(cmd.run())
70            }
71        }
72        ForgeSubcommand::Bind(cmd) => cmd.run(),
73        ForgeSubcommand::Build(cmd) => {
74            if cmd.is_watch() {
75                utils::block_on(watch::watch_build(cmd))
76            } else {
77                cmd.run().map(drop)
78            }
79        }
80        ForgeSubcommand::VerifyContract(args) => utils::block_on(args.run()),
81        ForgeSubcommand::VerifyCheck(args) => utils::block_on(args.run()),
82        ForgeSubcommand::VerifyBytecode(cmd) => utils::block_on(cmd.run()),
83        ForgeSubcommand::Clone(cmd) => utils::block_on(cmd.run()),
84        ForgeSubcommand::Cache(cmd) => match cmd.sub {
85            CacheSubcommands::Clean(cmd) => cmd.run(),
86            CacheSubcommands::Ls(cmd) => cmd.run(),
87        },
88        ForgeSubcommand::Create(cmd) => utils::block_on(cmd.run()),
89        ForgeSubcommand::Update(cmd) => cmd.run(),
90        ForgeSubcommand::Install(cmd) => cmd.run(),
91        ForgeSubcommand::Remove(cmd) => cmd.run(),
92        ForgeSubcommand::Remappings(cmd) => cmd.run(),
93        ForgeSubcommand::Init(cmd) => cmd.run(),
94        ForgeSubcommand::Completions { shell } => {
95            generate(shell, &mut Forge::command(), "forge", &mut std::io::stdout());
96            Ok(())
97        }
98        ForgeSubcommand::GenerateFigSpec => {
99            clap_complete::generate(
100                clap_complete_fig::Fig,
101                &mut Forge::command(),
102                "forge",
103                &mut std::io::stdout(),
104            );
105            Ok(())
106        }
107        ForgeSubcommand::Clean { root } => {
108            let config = utils::load_config_with_root(root.as_deref())?;
109            let project = config.project()?;
110            config.cleanup(&project)?;
111            Ok(())
112        }
113        ForgeSubcommand::Snapshot(cmd) => {
114            if cmd.is_watch() {
115                utils::block_on(watch::watch_gas_snapshot(cmd))
116            } else {
117                utils::block_on(cmd.run())
118            }
119        }
120        ForgeSubcommand::Fmt(cmd) => {
121            if cmd.is_watch() {
122                utils::block_on(watch::watch_fmt(cmd))
123            } else {
124                cmd.run()
125            }
126        }
127        ForgeSubcommand::Config(cmd) => cmd.run(),
128        ForgeSubcommand::Flatten(cmd) => cmd.run(),
129        ForgeSubcommand::Inspect(cmd) => cmd.run(),
130        ForgeSubcommand::Tree(cmd) => cmd.run(),
131        ForgeSubcommand::Geiger(cmd) => {
132            let n = cmd.run()?;
133            if n > 0 {
134                std::process::exit(n as i32);
135            }
136            Ok(())
137        }
138        ForgeSubcommand::Doc(cmd) => {
139            if cmd.is_watch() {
140                utils::block_on(watch::watch_doc(cmd))
141            } else {
142                utils::block_on(cmd.run())?;
143                Ok(())
144            }
145        }
146        ForgeSubcommand::Selectors { command } => utils::block_on(command.run()),
147        ForgeSubcommand::Generate(cmd) => match cmd.sub {
148            GenerateSubcommands::Test(cmd) => cmd.run(),
149        },
150        ForgeSubcommand::Compiler(cmd) => cmd.run(),
151        ForgeSubcommand::Soldeer(cmd) => utils::block_on(cmd.run()),
152        ForgeSubcommand::Eip712(cmd) => cmd.run(),
153        ForgeSubcommand::BindJson(cmd) => cmd.run(),
154        ForgeSubcommand::Lint(cmd) => cmd.run(),
155    }
156}