Radish alpha
h
rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5
Radicle Heartwood Protocol & Stack
Radicle
Git
cli/stats: Use clap
Merged lorenz opened 7 months ago

See issue/7fb03f234030b91c38cc4f5b48bd30cf5fd6a1de.

4 files changed +21 -43 f1c7c986 80bc9526
modified crates/radicle-cli/src/commands/help.rs
@@ -64,7 +64,10 @@ const COMMANDS: &[CommandItem] = &[
    CommandItem::Lexopt(crate::commands::unfollow::HELP),
    CommandItem::Lexopt(crate::commands::unseed::HELP),
    CommandItem::Lexopt(crate::commands::remote::HELP),
-
    CommandItem::Lexopt(crate::commands::stats::HELP),
+
    CommandItem::Clap {
+
        name: "stats",
+
        about: crate::commands::stats::ABOUT,
+
    },
    CommandItem::Lexopt(crate::commands::sync::HELP),
];

modified crates/radicle-cli/src/commands/stats.rs
@@ -1,4 +1,5 @@
-
use std::ffi::OsString;
+
mod args;
+

use std::path::Path;

use localtime::LocalDuration;
@@ -13,22 +14,9 @@ use radicle_term::Element;
use serde::Serialize;

use crate::terminal as term;
-
use crate::terminal::args::{Args, Error, Help};
-

-
pub const HELP: Help = Help {
-
    name: "stats",
-
    description: "Displays aggregated repository and node metrics",
-
    version: env!("RADICLE_VERSION"),
-
    usage: r#"
-
Usage
-

-
    rad stats [<option>...]

-
Options
-

-
    --help       Print help
-
"#,
-
};
+
pub use args::Args;
+
pub(crate) use args::ABOUT;

#[derive(Default, Serialize)]
#[serde(rename_all = "camelCase")]
@@ -65,30 +53,7 @@ struct Stats {
    nodes: NodeStats,
}

-
#[derive(Default, Debug, Eq, PartialEq)]
-
pub struct Options {}
-

-
impl Args for Options {
-
    fn from_args(args: Vec<OsString>) -> anyhow::Result<(Self, Vec<OsString>)> {
-
        use lexopt::prelude::*;
-

-
        let mut parser = lexopt::Parser::from_args(args);
-

-
        #[allow(clippy::never_loop)]
-
        while let Some(arg) = parser.next()? {
-
            match arg {
-
                Long("help") | Short('h') => {
-
                    return Err(Error::Help.into());
-
                }
-
                _ => anyhow::bail!(arg.unexpected()),
-
            }
-
        }
-

-
        Ok((Options {}, vec![]))
-
    }
-
}
-

-
pub fn run(_options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
+
pub fn run(_args: Args, ctx: impl term::Context) -> anyhow::Result<()> {
    let profile = ctx.profile()?;
    let storage = &profile.storage;
    let mut stats = Stats::default();
added crates/radicle-cli/src/commands/stats/args.rs
@@ -0,0 +1,7 @@
+
use clap::Parser;
+

+
pub(crate) const ABOUT: &str = "Displays aggregated repository and node metrics";
+

+
#[derive(Debug, Parser)]
+
#[command(about = ABOUT, disable_version_flag = true)]
+
pub struct Args {}
modified crates/radicle-cli/src/main.rs
@@ -46,6 +46,7 @@ struct CliArgs {
#[derive(Subcommand, Debug)]
enum Commands {
    Issue(issue::Args),
+
    Stats(stats::Args),
}

#[derive(Debug)]
@@ -167,7 +168,7 @@ fn run(command: Command) -> Result<(), Option<anyhow::Error>> {
/// arguments again. This needs to be done for each migrated command
/// individually, otherwise `clap` would fail to parse on an non-migrated and
/// therefore unknown command.
-
fn run_other(exe: &str, args: &[OsString]) -> Result<(), Option<anyhow::Error>> {
+
pub(crate) fn run_other(exe: &str, args: &[OsString]) -> Result<(), Option<anyhow::Error>> {
    match exe {
        "auth" => {
            term::run_command_args::<auth::Options, _>(auth::HELP, auth::run, args.to_vec());
@@ -283,7 +284,9 @@ fn run_other(exe: &str, args: &[OsString]) -> Result<(), Option<anyhow::Error>>
            term::run_command_args::<remote::Options, _>(remote::HELP, remote::run, args.to_vec())
        }
        "stats" => {
-
            term::run_command_args::<stats::Options, _>(stats::HELP, stats::run, args.to_vec())
+
            if let Some(Commands::Stats(args)) = CliArgs::parse().command {
+
                term::run_command_fn(stats::run, args);
+
            }
        }
        "watch" => {
            term::run_command_args::<watch::Options, _>(watch::HELP, watch::run, args.to_vec())