Skip to main content

mega_evme/
cmd.rs

1use clap::{Parser, Subcommand};
2use tracing::error;
3
4use crate::common::LogArgs;
5
6/// Main CLI for the mega-evme tool
7#[derive(Parser, Debug)]
8#[command(name = "mega-evme", infer_subcommands = true, version = "0.1")]
9pub struct MainCmd {
10    /// Logging configuration
11    #[command(flatten)]
12    pub log: LogArgs,
13
14    /// Subcommand to execute
15    #[command(subcommand)]
16    pub command: Commands,
17}
18
19/// Available subcommands
20#[derive(Subcommand, Debug)]
21#[allow(clippy::large_enum_variant)]
22pub enum Commands {
23    /// Run arbitrary EVM bytecode
24    Run(crate::run::Cmd),
25    /// Run arbitrary transaction
26    Tx(crate::tx::Cmd),
27    /// Replay a transaction from RPC
28    Replay(crate::replay::Cmd),
29}
30
31/// Error types for the main command system
32#[derive(Debug, thiserror::Error)]
33pub enum Error {
34    /// Custom error with static message
35    #[error("Custom error: {0}")]
36    Custom(&'static str),
37    /// Evme error (used by run, tx, and replay commands)
38    #[error("{0}")]
39    Evme(#[from] crate::common::EvmeError),
40}
41
42impl MainCmd {
43    /// Execute the main command
44    pub async fn run(self) -> Result<(), Error> {
45        // Initialize logging first
46        self.log.init();
47
48        match self.command {
49            Commands::Run(cmd) => {
50                cmd.run().await?;
51                Ok(())
52            }
53            Commands::Tx(cmd) => {
54                cmd.run().await?;
55                Ok(())
56            }
57            Commands::Replay(cmd) => {
58                cmd.run().await?;
59                Ok(())
60            }
61        }
62        .inspect_err(|e| {
63            error!(err = ?e, "Error executing command");
64            eprintln!("{e}");
65            std::process::exit(1);
66        })
67    }
68}