From 5cdcec8d48f6ebe57ec01d1148ab910e488c35f4 Mon Sep 17 00:00:00 2001 From: Sergei Blinov Date: Sat, 29 Nov 2025 13:40:58 +0100 Subject: [PATCH] feat: Add support for shell completion in CLI using clap_complete --- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + crates/sui/Cargo.toml | 1 + crates/sui/src/sui_commands.rs | 14 ++++++++++++++ 4 files changed, 26 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 389b34467405e..54b44707c0f8a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2681,6 +2681,15 @@ dependencies = [ "terminal_size", ] +[[package]] +name = "clap_complete" +version = "4.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5a2d6eec27fce550d708b2be5d798797e5a55b246b323ef36924a0001996352" +dependencies = [ + "clap", +] + [[package]] name = "clap_derive" version = "4.5.4" @@ -13408,6 +13417,7 @@ dependencies = [ "bip32", "camino", "clap", + "clap_complete", "codespan-reporting", "colored", "csv", diff --git a/Cargo.toml b/Cargo.toml index 5b0f0de9a223d..14b9af2464968 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -340,6 +340,7 @@ camino = "1.1.1" cfg-if = "1.0.0" chrono = { version = "0.4.39", features = ["clock", "serde"] } clap = { version = "4.4", features = ["derive", "wrap_help"] } +clap_complete = "4.4" codespan-reporting = "0.11.1" collectable = "0.0.2" colored = "2.0.0" diff --git a/crates/sui/Cargo.toml b/crates/sui/Cargo.toml index 03774a1deee5a..55545ea945cdb 100644 --- a/crates/sui/Cargo.toml +++ b/crates/sui/Cargo.toml @@ -22,6 +22,7 @@ bin-version.workspace = true bip32.workspace = true camino.workspace = true clap.workspace = true +clap_complete.workspace = true codespan-reporting.workspace = true datatest-stable.workspace = true futures.workspace = true diff --git a/crates/sui/src/sui_commands.rs b/crates/sui/src/sui_commands.rs index 06000a931d730..9c40665808ae1 100644 --- a/crates/sui/src/sui_commands.rs +++ b/crates/sui/src/sui_commands.rs @@ -404,6 +404,14 @@ pub enum SuiCommand { #[command(flatten)] replay_config: SR2::ReplayConfigStable, }, + + /// Generate shell completion scripts for CLI + #[clap(name = "completion")] + Completion { + /// If provided, outputs the completion file for given shell + #[arg(long = "generate", value_enum)] + generator: clap_complete::Shell, + }, } impl SuiCommand { @@ -839,6 +847,12 @@ impl SuiCommand { Ok(()) } + SuiCommand::Completion { generator } => { + let mut app: Command = SuiCommand::command(); + let name = app.get_name().to_string(); + clap_complete::generate(generator, &mut app, name, &mut std::io::stdout()); + Ok(()) + } } } }