Radish alpha
h
rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5
Radicle Heartwood Protocol & Stack
Radicle
Git
cli/completion: Add shell completion for cli
Merged did:key:z6MkraQd...nVj4 opened 5 months ago

Add shell completions for Bash, Elvish, Fish, PowerShell, and Zsh using the clap_complete crate

4 files changed +63 -31 7e5a1aba 93d2ed8c
modified Cargo.lock
@@ -478,6 +478,15 @@ dependencies = [
]

[[package]]
+
name = "clap_complete"
+
version = "4.5.60"
+
source = "registry+https://github.com/rust-lang/crates.io-index"
+
checksum = "8e602857739c5a4291dfa33b5a298aeac9006185229a700e5810a3ef7272d971"
+
dependencies = [
+
 "clap",
+
]
+

+
[[package]]
name = "clap_derive"
version = "4.5.41"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -2819,6 +2828,7 @@ dependencies = [
 "anyhow",
 "chrono",
 "clap",
+
 "clap_complete",
 "dunce",
 "human-panic",
 "itertools",
modified crates/radicle-cli/Cargo.toml
@@ -17,6 +17,7 @@ path = "src/main.rs"
anyhow = "1"
chrono = { workspace = true, features = ["clock", "std"] }
clap = { version = "4.5.44", features = ["derive"] }
+
clap_complete = "4.5"
dunce = { workspace = true }
human-panic.workspace = true
itertools.workspace = true
modified crates/radicle-cli/examples/rad-help.md
@@ -12,36 +12,37 @@ Do you have feedback?
Usage: rad <COMMAND>

Commands:
-
  auth      Manage identities and profiles
-
  block     Block repositories or nodes from being seeded or followed
-
  checkout  Checkout a repository into the local directory
-
  clean     Remove all remotes from a repository
-
  clone     Clone a Radicle repository
-
  config    Manage your local Radicle configuration
-
  debug     Write out information to help debug your Radicle node remotely
-
  follow    Manage node follow policies
-
  fork      Create a fork of a repository
-
  id        Manage repository identities
-
  inbox     Manage your Radicle notifications
-
  init      Initialize a Radicle repository
-
  inspect   Inspect a Radicle repository
-
  issue     Manage issues
-
  ls        List repositories
-
  node      Control and query the Radicle Node
-
  patch     Manage patches
-
  path      Display the Radicle home path
-
  publish   Publish a repository to the network
-
  remote    Manage a repository's remotes
-
  seed      Manage repository seeding policies
-
  self      Show information about your identity and device
-
  stats     Displays aggregated repository and node metrics
-
  sync      Sync repositories to the network
-
  unblock   Unblock repositories or nodes to allow them to be seeded or followed
-
  unfollow  Unfollow a peer
-
  unseed    Remove repository seeding policies
-
  watch     Wait for some state to be updated
-
  version   Print the version information of the CLI
-
  help      Print this message or the help of the given subcommand(s)
+
  auth        Manage identities and profiles
+
  block       Block repositories or nodes from being seeded or followed
+
  checkout    Checkout a repository into the local directory
+
  clean       Remove all remotes from a repository
+
  clone       Clone a Radicle repository
+
  config      Manage your local Radicle configuration
+
  debug       Write out information to help debug your Radicle node remotely
+
  follow      Manage node follow policies
+
  fork        Create a fork of a repository
+
  id          Manage repository identities
+
  inbox       Manage your Radicle notifications
+
  init        Initialize a Radicle repository
+
  inspect     Inspect a Radicle repository
+
  issue       Manage issues
+
  ls          List repositories
+
  node        Control and query the Radicle Node
+
  patch       Manage patches
+
  path        Display the Radicle home path
+
  publish     Publish a repository to the network
+
  remote      Manage a repository's remotes
+
  seed        Manage repository seeding policies
+
  self        Show information about your identity and device
+
  stats       Displays aggregated repository and node metrics
+
  sync        Sync repositories to the network
+
  unblock     Unblock repositories or nodes to allow them to be seeded or followed
+
  unfollow    Unfollow a peer
+
  unseed      Remove repository seeding policies
+
  watch       Wait for some state to be updated
+
  version     Print the version information of the CLI
+
  completions  Print static completions information for a given shell
+
  help        Print this message or the help of the given subcommand(s)

Options:
  -h, --help
modified crates/radicle-cli/src/main.rs
@@ -7,7 +7,7 @@ use std::{io::ErrorKind, process};
use anyhow::anyhow;
use clap::builder::styling::AnsiColor;
use clap::builder::Styles;
-
use clap::{Parser, Subcommand};
+
use clap::{CommandFactory as _, Parser, Subcommand};

use radicle::version::Version;
use radicle_cli::commands::*;
@@ -98,6 +98,13 @@ enum Command {
        json: bool,
    },

+
    /// Print static completions information for a given shell
+
    #[command(hide = true)]
+
    Completions {
+
        /// The type of shell to provide static completions for
+
        shell: clap_complete::Shell,
+
    },
+

    #[command(external_subcommand)]
    External(Vec<OsString>),
}
@@ -176,10 +183,23 @@ fn run_command(command: Command, ctx: impl term::Context) -> Result<(), anyhow::
        Command::Unseed(args) => unseed::run(args, ctx),
        Command::Watch(args) => watch::run(args, ctx),
        Command::Version { json } => write_version(json),
+
        Command::Completions { shell } => {
+
            print_completions(shell, &mut CliArgs::command());
+
            Ok(())
+
        }
        Command::External(args) => ExternalCommand::new(args).run(),
    }
}

+
fn print_completions<G: clap_complete::Generator>(generator: G, cmd: &mut clap::Command) {
+
    clap_complete::generate(
+
        generator,
+
        cmd,
+
        cmd.get_name().to_string(),
+
        &mut io::stdout(),
+
    );
+
}
+

struct ExternalCommand {
    command: OsString,
    args: Vec<OsString>,