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

See issue/7fb03f234030b91c38cc4f5b48bd30cf5fd6a1de.

4 files changed +28 -63 80bc9526 01f9f3fc
modified crates/radicle-cli/src/commands/help.rs
@@ -62,7 +62,10 @@ const COMMANDS: &[CommandItem] = &[
    CommandItem::Lexopt(crate::commands::follow::HELP),
    CommandItem::Lexopt(crate::commands::unblock::HELP),
    CommandItem::Lexopt(crate::commands::unfollow::HELP),
-
    CommandItem::Lexopt(crate::commands::unseed::HELP),
+
    CommandItem::Clap {
+
        name: "unseed",
+
        about: crate::commands::unseed::ABOUT,
+
    },
    CommandItem::Lexopt(crate::commands::remote::HELP),
    CommandItem::Lexopt(crate::commands::stats::HELP),
    CommandItem::Lexopt(crate::commands::sync::HELP),
modified crates/radicle-cli/src/commands/unseed.rs
@@ -1,70 +1,13 @@
-
use std::ffi::OsString;
-

-
use anyhow::anyhow;
-
use nonempty::NonEmpty;
+
pub mod args;

use radicle::{prelude::*, Node};

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

-
pub const HELP: Help = Help {
-
    name: "unseed",
-
    description: "Remove repository seeding policies",
-
    version: env!("RADICLE_VERSION"),
-
    usage: r#"
-
Usage
-

-
    rad unseed <rid>... [<option>...]
-

-
    The `unseed` command removes the seeding policy, if found,
-
    for the given repositories.

-
Options
-

-
    --help      Print help
-
"#,
-
};
-

-
#[derive(Debug)]
-
pub struct Options {
-
    rids: NonEmpty<RepoId>,
-
}
-

-
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);
-
        let mut rids: Vec<RepoId> = Vec::new();
-

-
        while let Some(arg) = parser.next()? {
-
            match &arg {
-
                Value(val) => {
-
                    let rid = term::args::rid(val)?;
-
                    rids.push(rid);
-
                }
-
                Long("help") | Short('h') => {
-
                    return Err(Error::Help.into());
-
                }
-
                _ => {
-
                    return Err(anyhow!(arg.unexpected()));
-
                }
-
            }
-
        }
-

-
        Ok((
-
            Options {
-
                rids: NonEmpty::from_vec(rids).ok_or(anyhow!(
-
                    "At least one Repository ID must be provided; see `rad unseed --help`"
-
                ))?,
-
            },
-
            vec![],
-
        ))
-
    }
-
}
+
pub use args::Args;
+
pub(crate) use args::ABOUT;

-
pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
+
pub fn run(options: Args, ctx: impl term::Context) -> anyhow::Result<()> {
    let profile = ctx.profile()?;
    let mut node = radicle::Node::new(profile.socket());

added crates/radicle-cli/src/commands/unseed/args.rs
@@ -0,0 +1,16 @@
+
use clap::Parser;
+
use radicle::prelude::RepoId;
+

+
pub(crate) const ABOUT: &str = "Remove repository seeding policies";
+

+
const LONG_ABOUT: &str = r#"
+
The `unseed` command removes the seeding policy, if found,
+
for the given repositories."#;
+

+
#[derive(Debug, Parser)]
+
#[command(about = ABOUT, long_about = LONG_ABOUT, disable_version_flag = true)]
+
pub struct Args {
+
    /// ID of the repository to remove the seeding policy for (may be repeated)
+
    #[arg(value_name = "RID", required = true, action = clap::ArgAction::Append)]
+
    pub rids: Vec<RepoId>,
+
}
modified crates/radicle-cli/src/main.rs
@@ -46,6 +46,7 @@ struct CliArgs {
#[derive(Subcommand, Debug)]
enum Commands {
    Issue(issue::Args),
+
    Unseed(unseed::Args),
}

#[derive(Debug)]
@@ -277,7 +278,9 @@ fn run_other(exe: &str, args: &[OsString]) -> Result<(), Option<anyhow::Error>>
            );
        }
        "unseed" => {
-
            term::run_command_args::<unseed::Options, _>(unseed::HELP, unseed::run, args.to_vec());
+
            if let Some(Commands::Unseed(args)) = CliArgs::parse().command {
+
                term::run_command_fn(unseed::run, args);
+
            }
        }
        "remote" => {
            term::run_command_args::<remote::Options, _>(remote::HELP, remote::run, args.to_vec())