From f8445949635d969cb14b943c57526de6e9cb8651 Mon Sep 17 00:00:00 2001 From: Alistair Date: Thu, 8 Feb 2024 14:27:18 +0000 Subject: [PATCH] feat(cli): add `completions` command to jstz --- Cargo.lock | 10 ++++++++++ crates/jstz_cli/Cargo.toml | 1 + crates/jstz_cli/src/completions.rs | 12 ++++++++++++ crates/jstz_cli/src/main.rs | 11 ++++++++++- 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 crates/jstz_cli/src/completions.rs diff --git a/Cargo.lock b/Cargo.lock index 4b341e198..a9089c4f1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -757,6 +757,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" @@ -1995,6 +2004,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 39ada9678..e9e30be84 100644 --- a/crates/jstz_cli/Cargo.toml +++ b/crates/jstz_cli/Cargo.toml @@ -52,6 +52,7 @@ crossterm = "0.27" ansi_term = "0.12.1" console = "0.15.8" futures = "0.3" +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 c585a1ea0..528a9a65a 100644 --- a/crates/jstz_cli/src/main.rs +++ b/crates/jstz_cli/src/main.rs @@ -2,6 +2,7 @@ use clap::Parser; mod account; mod bridge; +mod completions; mod config; mod deploy; mod error; @@ -14,6 +15,7 @@ mod sandbox; mod term; mod utils; +use clap_complete::Shell; use config::Config; use error::Result; use run::DEFAULT_GAS_LIMIT; @@ -21,7 +23,7 @@ use utils::AddressOrAlias; #[derive(Parser)] #[command(author, version)] -enum Command { +pub enum Command { /// Commands related to the jstz sandbox. #[command(subcommand)] Sandbox(sandbox::Command), @@ -31,6 +33,12 @@ enum Command { /// Commands related to the account management #[command(subcommand)] Account(account::Command), + /// Generates shell completions + Completions { + /// The shell to generate completions for + #[arg(long, short)] + shell: Shell, + }, /// Deploys a smart function Deploy { /// Function code. @@ -85,6 +93,7 @@ enum Command { async fn exec(command: Command) -> Result<()> { match command { + 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,