reth_cli/
lib.rs

1//! Cli abstraction for reth based nodes.
2
3#![doc(
4    html_logo_url = "https://raw.githubusercontent.com/paradigmxyz/reth/main/assets/reth-docs.png",
5    html_favicon_url = "https://avatars0.githubusercontent.com/u/97369466?s=256",
6    issue_tracker_base_url = "https://github.com/paradigmxyz/reth/issues/"
7)]
8#![cfg_attr(not(test), warn(unused_crate_dependencies))]
9#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
10
11use clap::{Error, Parser};
12use reth_cli_runner::CliRunner;
13use reth_db::ClientVersion;
14use std::{borrow::Cow, ffi::OsString};
15
16/// The chainspec module defines the different chainspecs that can be used by the node.
17pub mod chainspec;
18use crate::chainspec::ChainSpecParser;
19
20/// Reth based node cli.
21///
22/// This trait is supposed to be implemented by the main struct of the CLI.
23///
24/// It provides commonly used functionality for running commands and information about the CL, such
25/// as the name and version.
26pub trait RethCli: Sized {
27    /// The associated `ChainSpecParser` type
28    type ChainSpecParser: ChainSpecParser;
29
30    /// The name of the implementation, eg. `reth`, `op-reth`, etc.
31    fn name(&self) -> Cow<'static, str>;
32
33    /// The version of the node, such as `reth/v1.0.0`
34    fn version(&self) -> Cow<'static, str>;
35
36    /// Parse args from iterator from [`std::env::args_os()`].
37    fn parse_args() -> Result<Self, Error>
38    where
39        Self: Parser,
40    {
41        <Self as RethCli>::try_parse_from(std::env::args_os())
42    }
43
44    /// Parse args from the given iterator.
45    fn try_parse_from<I, T>(itr: I) -> Result<Self, Error>
46    where
47        Self: Parser,
48        I: IntoIterator<Item = T>,
49        T: Into<OsString> + Clone,
50    {
51        <Self as Parser>::try_parse_from(itr)
52    }
53
54    /// Executes a command.
55    fn with_runner<F, R>(self, f: F, runner: CliRunner) -> R
56    where
57        F: FnOnce(Self, CliRunner) -> R,
58    {
59        f(self, runner)
60    }
61
62    /// Parses and executes a command.
63    fn execute<F, R>(f: F) -> Result<R, Error>
64    where
65        Self: Parser,
66        F: FnOnce(Self, CliRunner) -> R,
67    {
68        let cli = Self::parse_args()?;
69        let runner = CliRunner::try_default_runtime()?;
70        Ok(cli.with_runner(f, runner))
71    }
72
73    /// The client version of the node.
74    fn client_version() -> ClientVersion;
75}