anvil/
args.rs

1use crate::opts::{Anvil, AnvilSubcommand};
2use clap::{CommandFactory, Parser};
3use eyre::Result;
4use foundry_cli::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::common_setup();
20
21    Ok(())
22}
23
24/// Run the subcommand.
25pub fn run_command(args: Anvil) -> Result<()> {
26    if let Some(cmd) = &args.cmd {
27        match cmd {
28            AnvilSubcommand::Completions { shell } => {
29                clap_complete::generate(
30                    *shell,
31                    &mut Anvil::command(),
32                    "anvil",
33                    &mut std::io::stdout(),
34                );
35            }
36        }
37        return Ok(());
38    }
39
40    let _ = fdlimit::raise_fd_limit();
41    args.global.tokio_runtime().block_on(args.node.run())
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn verify_cli() {
50        Anvil::command().debug_assert();
51    }
52
53    #[test]
54    fn can_parse_help() {
55        let _: Anvil = Anvil::parse_from(["anvil", "--help"]);
56    }
57
58    #[test]
59    fn can_parse_short_version() {
60        let _: Anvil = Anvil::parse_from(["anvil", "-V"]);
61    }
62
63    #[test]
64    fn can_parse_long_version() {
65        let _: Anvil = Anvil::parse_from(["anvil", "--version"]);
66    }
67
68    #[test]
69    fn can_parse_completions() {
70        let args: Anvil = Anvil::parse_from(["anvil", "completions", "bash"]);
71        assert!(matches!(
72            args.cmd,
73            Some(AnvilSubcommand::Completions {
74                shell: foundry_cli::clap::Shell::ClapCompleteShell(clap_complete::Shell::Bash)
75            })
76        ));
77    }
78}