diff --git a/Cargo.lock b/Cargo.lock index 358c72f57..4c8d5e6bf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -758,6 +758,15 @@ dependencies = [ "strsim", ] +[[package]] +name = "clap_complete" +version = "4.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abb745187d7f4d76267b37485a65e0149edd0e91a4cfcdd3f27524ad86cee9f3" +dependencies = [ + "clap", +] + [[package]] name = "clap_derive" version = "4.4.7" @@ -2105,6 +2114,7 @@ dependencies = [ "boa_gc", "bs58", "clap", + "clap_complete", "console", "crossterm", "daemonize", diff --git a/crates/jstz_cli/Cargo.toml b/crates/jstz_cli/Cargo.toml index 210e0b3d5..7bc145143 100644 --- a/crates/jstz_cli/Cargo.toml +++ b/crates/jstz_cli/Cargo.toml @@ -56,6 +56,7 @@ log = "0.4.20" dialoguer = "0.11.0" prettytable = "0.10.0" serde_with = { version = "3.6.1", features = ["macros"] } +clap_complete = "4.4.10" [[bin]] name = "jstz" diff --git a/crates/jstz_cli/src/completions.rs b/crates/jstz_cli/src/completions.rs new file mode 100644 index 000000000..fed45d04d --- /dev/null +++ b/crates/jstz_cli/src/completions.rs @@ -0,0 +1,12 @@ +use std::io; + +use clap::CommandFactory; +use clap_complete::Shell; + +use crate::{error::Result, Command}; + +pub fn exec(shell: Shell) -> Result<()> { + let cmd = &mut Command::command(); + clap_complete::generate(shell, cmd, "jstz", &mut io::stdout()); + Ok(()) +} diff --git a/crates/jstz_cli/src/main.rs b/crates/jstz_cli/src/main.rs index 9b08876d8..e03ea859a 100644 --- a/crates/jstz_cli/src/main.rs +++ b/crates/jstz_cli/src/main.rs @@ -1,7 +1,9 @@ use clap::Parser; +use clap_complete::Shell; mod account; mod bridge; +mod completions; mod config; mod deploy; mod docs; @@ -38,6 +40,12 @@ enum Command { /// 🔑 Interact with jstz's key-value store. #[command(subcommand)] Kv(kv::Command), + /// 🐚 Generates shell completions. + Completions { + /// The shell to generate completions for + #[arg(long, short)] + shell: Shell, + }, /// 🚀 Deploys a smart function to jstz. Deploy { /// Function code. @@ -98,6 +106,7 @@ enum Command { async fn exec(command: Command) -> Result<()> { match command { Command::Docs => docs::exec(), + Command::Completions { shell } => completions::exec(shell), Command::Sandbox(sandbox_command) => sandbox::exec(sandbox_command).await, Command::Bridge(bridge_command) => bridge::exec(bridge_command), Command::Account(account_command) => account::exec(account_command).await,