Skip to main content

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    foundry_cli::opts::GlobalArgs::check_markdown_help::<Anvil>();
11
12    let mut args = Anvil::parse();
13    args.global.init()?;
14    args.node.evm.resolve_rpc_alias();
15
16    run_command(args)
17}
18
19/// Setup the exception handler and other utilities.
20pub fn setup() -> Result<()> {
21    utils::common_setup();
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        }
39        return Ok(());
40    }
41
42    let _ = fdlimit::raise_fd_limit();
43    args.global.tokio_runtime().block_on(args.node.run())
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn verify_cli() {
52        Anvil::command().debug_assert();
53    }
54
55    #[test]
56    fn can_parse_help() {
57        let _: Anvil = Anvil::parse_from(["anvil", "--help"]);
58    }
59
60    #[test]
61    fn can_parse_short_version() {
62        let _: Anvil = Anvil::parse_from(["anvil", "-V"]);
63    }
64
65    #[test]
66    fn can_parse_long_version() {
67        let _: Anvil = Anvil::parse_from(["anvil", "--version"]);
68    }
69
70    #[test]
71    fn can_parse_completions() {
72        let args: Anvil = Anvil::parse_from(["anvil", "completions", "bash"]);
73        assert!(matches!(
74            args.cmd,
75            Some(AnvilSubcommand::Completions {
76                shell: foundry_cli::clap::Shell::ClapCompleteShell(clap_complete::Shell::Bash)
77            })
78        ));
79    }
80}