anvil/
args.rs

1use crate::opts::{Anvil, AnvilSubcommand};
2use clap::{CommandFactory, Parser};
3use eyre::Result;
4use foundry_cli::{handler, utils};
5
6/// Run the `anvil` command line interface.
7pub fn run() -> Result<()> {
8    setup()?;
9
10    let mut args = Anvil::parse();
11    args.global.init()?;
12    args.node.evm.resolve_rpc_alias();
13
14    run_command(args)
15}
16
17/// Setup the exception handler and other utilities.
18pub fn setup() -> Result<()> {
19    utils::install_crypto_provider();
20    handler::install();
21    utils::load_dotenv();
22    utils::enable_paint();
23
24    Ok(())
25}
26
27/// Run the subcommand.
28pub fn run_command(args: Anvil) -> Result<()> {
29    if let Some(cmd) = &args.cmd {
30        match cmd {
31            AnvilSubcommand::Completions { shell } => {
32                clap_complete::generate(
33                    *shell,
34                    &mut Anvil::command(),
35                    "anvil",
36                    &mut std::io::stdout(),
37                );
38            }
39            AnvilSubcommand::GenerateFigSpec => clap_complete::generate(
40                clap_complete_fig::Fig,
41                &mut Anvil::command(),
42                "anvil",
43                &mut std::io::stdout(),
44            ),
45        }
46        return Ok(())
47    }
48
49    let _ = fdlimit::raise_fd_limit();
50    tokio::runtime::Builder::new_multi_thread().enable_all().build()?.block_on(args.node.run())
51}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn verify_cli() {
59        Anvil::command().debug_assert();
60    }
61
62    #[test]
63    fn can_parse_help() {
64        let _: Anvil = Anvil::parse_from(["anvil", "--help"]);
65    }
66
67    #[test]
68    fn can_parse_short_version() {
69        let _: Anvil = Anvil::parse_from(["anvil", "-V"]);
70    }
71
72    #[test]
73    fn can_parse_long_version() {
74        let _: Anvil = Anvil::parse_from(["anvil", "--version"]);
75    }
76
77    #[test]
78    fn can_parse_completions() {
79        let args: Anvil = Anvil::parse_from(["anvil", "completions", "bash"]);
80        assert!(matches!(
81            args.cmd,
82            Some(AnvilSubcommand::Completions { shell: clap_complete::Shell::Bash })
83        ));
84    }
85}