From 8396b8c97d3c8783294d5a98b0baa2bd0ed25907 Mon Sep 17 00:00:00 2001 From: qwjyh Date: Sun, 17 Mar 2024 04:08:47 +0900 Subject: [PATCH] add shell completion script generation --- Cargo.lock | 10 ++++++++++ Cargo.toml | 3 ++- src/cmd_args.rs | 5 +++++ src/cmd_completion.rs | 12 ++++++++++++ src/main.rs | 4 ++++ 5 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/cmd_completion.rs diff --git a/Cargo.lock b/Cargo.lock index bae6737..16170d6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -218,6 +218,15 @@ dependencies = [ "strsim", ] +[[package]] +name = "clap_complete" +version = "4.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "885e4d7d5af40bfb99ae6f9433e292feac98d452dcb3ec3d25dfe7552b77da8c" +dependencies = [ + "clap", +] + [[package]] name = "clap_derive" version = "4.5.0" @@ -1438,6 +1447,7 @@ dependencies = [ "chrono", "clap", "clap-verbosity-flag", + "clap_complete", "dirs", "dunce", "env_logger", diff --git a/Cargo.toml b/Cargo.toml index bc4ec94..98cbdb4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,10 +11,11 @@ license = "MIT OR Apache-1.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -clap = { version = "4.4.0", features = ["derive"] } +clap = { version = "4.4.0", features = ["cargo", "derive"] } sysinfo = { version = "0.29.8", features = ["serde"] } log = "0.4" clap-verbosity-flag = "2.0.1" +clap_complete = "4.5.1" chrono = { version = "0.4.35", features = ["serde"] } env_logger = "0.10.0" inquire = "0.6.2" diff --git a/src/cmd_args.rs b/src/cmd_args.rs index 3a4796e..0fe2eda 100644 --- a/src/cmd_args.rs +++ b/src/cmd_args.rs @@ -55,6 +55,11 @@ pub(crate) enum Commands { /// Check config files. Check {}, + + /// Generate completion script. + Completion { + shell: clap_complete::Shell, + } } #[derive(Args, Debug)] diff --git a/src/cmd_completion.rs b/src/cmd_completion.rs new file mode 100644 index 0000000..a21f1b1 --- /dev/null +++ b/src/cmd_completion.rs @@ -0,0 +1,12 @@ +use crate::cmd_args::Cli; +use std::io; + +use anyhow::Result; +use clap::CommandFactory; +use clap_complete::Shell; + +pub(crate) fn cmd_completion(shell: Shell) -> Result<()> { + let mut cmd = Cli::command(); + clap_complete::generate(shell, &mut cmd, "xdbm", &mut io::stdout()); + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index 2367aff..d14a081 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,6 +31,7 @@ mod cmd_backup; mod cmd_init; mod cmd_storage; mod cmd_sync; +mod cmd_completion; mod devices; mod inquire_filepath_completer; mod storages; @@ -122,6 +123,9 @@ fn main() -> Result<()> { } => cmd_backup::cmd_backup_done(name, exit_status, log, repo, &config_dir)?, } } + Commands::Completion { shell } => { + cmd_completion::cmd_completion(shell)? + } } full_status(&Repository::open(&config_dir)?)?; Ok(())