feat: integrate CLI runner in CLI trait (#9146)

This commit is contained in:
Aurélien
2024-06-27 18:11:06 +02:00
committed by GitHub
parent 26b79f84f3
commit c23fe39dd3
3 changed files with 27 additions and 0 deletions

1
Cargo.lock generated
View File

@ -6579,6 +6579,7 @@ name = "reth-cli"
version = "1.0.0"
dependencies = [
"clap",
"reth-cli-runner",
]
[[package]]

View File

@ -11,5 +11,8 @@ repository.workspace = true
[dependencies]
# reth
reth-cli-runner.workspace = true
# misc
clap.workspace = true

View File

@ -10,6 +10,8 @@
use std::{borrow::Cow, ffi::OsString};
use reth_cli_runner::CliRunner;
use clap::{Error, Parser};
/// Reth based node cli.
@ -42,4 +44,25 @@ pub trait RethCli: Sized {
{
<Self as Parser>::try_parse_from(itr)
}
/// Executes a command.
fn with_runner<F, R>(self, f: F) -> R
where
F: FnOnce(Self, CliRunner) -> R,
{
let runner = CliRunner::default();
f(self, runner)
}
/// Parses and executes a command.
fn execute<F, R>(f: F) -> Result<R, Error>
where
Self: Parser + Sized,
F: FnOnce(Self, CliRunner) -> R,
{
let cli = Self::parse_args()?;
Ok(cli.with_runner(f))
}
}