Generate shell auto-completion files at build time

This commit is contained in:
Marcin Kulik
2024-04-29 21:52:47 +02:00
parent 3e73959c06
commit 2c178c82a6
3 changed files with 20 additions and 2 deletions

10
Cargo.lock generated
View File

@@ -88,6 +88,7 @@ dependencies = [
"avt",
"axum",
"clap",
"clap_complete",
"clap_mangen",
"config",
"futures-util",
@@ -318,6 +319,15 @@ dependencies = [
"strsim",
]
[[package]]
name = "clap_complete"
version = "4.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd79504325bf38b10165b02e89b4347300f855f273c4cb30c4a3209e6583275e"
dependencies = [
"clap",
]
[[package]]
name = "clap_derive"
version = "4.4.7"

View File

@@ -46,6 +46,7 @@ tokio-util = "0.7.10"
[build-dependencies]
clap = { version = "4.4.7", features = ["derive"] }
clap_complete = "4.5.2"
clap_mangen = "0.2.20"
url = "2.5.0"

View File

@@ -1,4 +1,6 @@
use clap::CommandFactory;
use clap::ValueEnum;
use clap_complete::{generate_to, Shell};
use clap_mangen::Man;
use std::env;
use std::fs::File;
@@ -11,9 +13,14 @@ mod cli {
fn main() -> std::io::Result<()> {
let out_dir = PathBuf::from(env::var_os("OUT_DIR").ok_or(io::ErrorKind::NotFound)?);
let cmd = cli::Cli::command();
let man = Man::new(cmd);
let mut cmd = cli::Cli::command();
let man = Man::new(cmd.clone());
man.render(&mut File::create(out_dir.join("asciinema.1"))?)?;
for shell in Shell::value_variants() {
generate_to(*shell, &mut cmd, "asciinema", &out_dir).unwrap();
}
Ok(())
}