add shell completion script generation

This commit is contained in:
qwjyh 2024-03-17 04:08:47 +09:00
parent f5fe3d6580
commit 8396b8c97d
5 changed files with 33 additions and 1 deletions

10
Cargo.lock generated
View file

@ -218,6 +218,15 @@ dependencies = [
"strsim", "strsim",
] ]
[[package]]
name = "clap_complete"
version = "4.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "885e4d7d5af40bfb99ae6f9433e292feac98d452dcb3ec3d25dfe7552b77da8c"
dependencies = [
"clap",
]
[[package]] [[package]]
name = "clap_derive" name = "clap_derive"
version = "4.5.0" version = "4.5.0"
@ -1438,6 +1447,7 @@ dependencies = [
"chrono", "chrono",
"clap", "clap",
"clap-verbosity-flag", "clap-verbosity-flag",
"clap_complete",
"dirs", "dirs",
"dunce", "dunce",
"env_logger", "env_logger",

View file

@ -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 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
clap = { version = "4.4.0", features = ["derive"] } clap = { version = "4.4.0", features = ["cargo", "derive"] }
sysinfo = { version = "0.29.8", features = ["serde"] } sysinfo = { version = "0.29.8", features = ["serde"] }
log = "0.4" log = "0.4"
clap-verbosity-flag = "2.0.1" clap-verbosity-flag = "2.0.1"
clap_complete = "4.5.1"
chrono = { version = "0.4.35", features = ["serde"] } chrono = { version = "0.4.35", features = ["serde"] }
env_logger = "0.10.0" env_logger = "0.10.0"
inquire = "0.6.2" inquire = "0.6.2"

View file

@ -55,6 +55,11 @@ pub(crate) enum Commands {
/// Check config files. /// Check config files.
Check {}, Check {},
/// Generate completion script.
Completion {
shell: clap_complete::Shell,
}
} }
#[derive(Args, Debug)] #[derive(Args, Debug)]

12
src/cmd_completion.rs Normal file
View file

@ -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(())
}

View file

@ -31,6 +31,7 @@ mod cmd_backup;
mod cmd_init; mod cmd_init;
mod cmd_storage; mod cmd_storage;
mod cmd_sync; mod cmd_sync;
mod cmd_completion;
mod devices; mod devices;
mod inquire_filepath_completer; mod inquire_filepath_completer;
mod storages; mod storages;
@ -122,6 +123,9 @@ fn main() -> Result<()> {
} => cmd_backup::cmd_backup_done(name, exit_status, log, repo, &config_dir)?, } => 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)?)?; full_status(&Repository::open(&config_dir)?)?;
Ok(()) Ok(())