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

See issue/7fb03f234030b91c38cc4f5b48bd30cf5fd6a1de.

4 files changed +19 -47 4787b53b 753b7aef
modified crates/radicle-cli/src/commands/help.rs
@@ -55,7 +55,10 @@ const COMMANDS: &[CommandItem] = &[
    CommandItem::Lexopt(crate::commands::ls::HELP),
    CommandItem::Lexopt(crate::commands::node::HELP),
    CommandItem::Lexopt(crate::commands::patch::HELP),
-
    CommandItem::Lexopt(crate::commands::path::HELP),
+
    CommandItem::Clap {
+
        name: "path",
+
        about: crate::commands::path::ABOUT,
+
    },
    CommandItem::Lexopt(crate::commands::clean::HELP),
    CommandItem::Lexopt(crate::commands::rad_self::HELP),
    CommandItem::Lexopt(crate::commands::seed::HELP),
modified crates/radicle-cli/src/commands/path.rs
@@ -1,54 +1,13 @@
-
#![allow(clippy::or_fun_call)]
-
use std::ffi::OsString;
-

-
use anyhow::anyhow;
+
mod args;

use radicle::profile;

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

-
pub const HELP: Help = Help {
-
    name: "path",
-
    description: "Display the Radicle home path",
-
    version: env!("RADICLE_VERSION"),
-
    usage: r#"
-
Usage
-

-
    rad path [<option>...]
-

-
    If no argument is specified, the Radicle home path is displayed.
-

-
Options
-

-
    --help    Print help

-
"#,
-
};
-

-
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());
-
                }
-
                _ => return Err(anyhow!(arg.unexpected())),
-
            }
-
        }
-

-
        Ok((Options {}, vec![]))
-
    }
-
}
+
pub use args::Args;
+
pub(crate) use args::ABOUT;

-
pub fn run(_options: Options, _ctx: impl term::Context) -> anyhow::Result<()> {
+
pub fn run(_args: Args, _ctx: impl term::Context) -> anyhow::Result<()> {
    let home = profile::home()?;

    println!("{}", home.path().display());
added crates/radicle-cli/src/commands/path/args.rs
@@ -0,0 +1,7 @@
+
use clap::Parser;
+

+
pub const ABOUT: &str = "Display the Radicle home path";
+

+
#[derive(Parser, Debug)]
+
#[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),
+
    Path(path::Args),
}

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