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",
]
[[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",

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
[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"

View file

@ -55,6 +55,11 @@ pub(crate) enum Commands {
/// Check config files.
Check {},
/// Generate completion script.
Completion {
shell: clap_complete::Shell,
}
}
#[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_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(())