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    handler::install();
20    utils::load_dotenv();
21    utils::enable_paint();
22
23    Ok(())
24}
25
26/// Run the subcommand.
27pub fn run_command(args: Anvil) -> Result<()> {
28    if let Some(cmd) = &args.cmd {
29        match cmd {
30            AnvilSubcommand::Completions { shell } => {
31                clap_complete::generate(
32                    *shell,
33                    &mut Anvil::command(),
34                    "anvil",
35                    &mut std::io::stdout(),
36                );
37            }
38            AnvilSubcommand::GenerateFigSpec => clap_complete::generate(
39                clap_complete_fig::Fig,
40                &mut Anvil::command(),
41                "anvil",
42                &mut std::io::stdout(),
43            ),
44        }
45        return Ok(())
46    }
47
48    let _ = fdlimit::raise_fd_limit();
49    tokio::runtime::Builder::new_multi_thread().enable_all().build()?.block_on(args.node.run())
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn verify_cli() {
58        Anvil::command().debug_assert();
59    }
60
61    #[test]
62    fn can_parse_help() {
63        let _: Anvil = Anvil::parse_from(["anvil", "--help"]);
64    }
65
66    #[test]
67    fn can_parse_short_version() {
68        let _: Anvil = Anvil::parse_from(["anvil", "-V"]);
69    }
70
71    #[test]
72    fn can_parse_long_version() {
73        let _: Anvil = Anvil::parse_from(["anvil", "--version"]);
74    }
75
76    #[test]
77    fn can_parse_completions() {
78        let args: Anvil = Anvil::parse_from(["anvil", "completions", "bash"]);
79        assert!(matches!(
80            args.cmd,
81            Some(AnvilSubcommand::Completions { shell: clap_complete::Shell::Bash })
82        ));
83    }
84}