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            AnvilSubcommand::GenerateFigSpec => {
37                clap_complete::generate(
38                    foundry_common::clap::Shell::Fig,
39                    &mut Anvil::command(),
40                    "anvil",
41                    &mut std::io::stdout(),
42                );
43                sh_eprintln!(
44                    "[deprecated] `anvil generate-fig-spec` is deprecated; use `anvil completions fig`"
45                )?;
46            }
47        }
48        return Ok(());
49    }
50
51    let _ = fdlimit::raise_fd_limit();
52    args.global.tokio_runtime().block_on(args.node.run())
53}
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[test]
60    fn verify_cli() {
61        Anvil::command().debug_assert();
62    }
63
64    #[test]
65    fn can_parse_help() {
66        let _: Anvil = Anvil::parse_from(["anvil", "--help"]);
67    }
68
69    #[test]
70    fn can_parse_short_version() {
71        let _: Anvil = Anvil::parse_from(["anvil", "-V"]);
72    }
73
74    #[test]
75    fn can_parse_long_version() {
76        let _: Anvil = Anvil::parse_from(["anvil", "--version"]);
77    }
78
79    #[test]
80    fn can_parse_completions() {
81        let args: Anvil = Anvil::parse_from(["anvil", "completions", "bash"]);
82        assert!(matches!(
83            args.cmd,
84            Some(AnvilSubcommand::Completions {
85                shell: foundry_common::clap::Shell::ClapCompleteShell(clap_complete::Shell::Bash)
86            })
87        ));
88    }
89}