use std::{fs::File, io, path::Path}; use clap::{CommandFactory, ValueEnum}; use clap_complete::Shell; use clap_mangen::Man; #[path = "src/config.rs"] mod config; fn main() { // OUT_DIR is where any additional build artifacts are written; Set by Cargo. let Some(out_dir) = std::env::var_os("OUT_DIR") else { eprintln!("OUT_DIR environment variable not defined."); std::process::exit(1); }; std::fs::create_dir_all(&out_dir).unwrap(); let cmd = config::Config::command(); let bin_name = cmd.get_bin_name().unwrap_or_else(|| cmd.get_name()); if let Err(err) = generate_man_page(bin_name, &out_dir) { eprintln!("failed to generate man page: {}", err); } if let Err(err) = generate_completions(bin_name, &out_dir) { eprintln!("failed to generate shell completion: {}", err); } println!("cargo:rerun-if-changed=build.rs"); } fn generate_man_page>(bin_name: &str, out_dir: P) -> Result<(), io::Error> { let out_dir = out_dir.as_ref().join("man"); std::fs::create_dir_all(&out_dir)?; let cmd = config::Config::command(); let mut man_page = out_dir.join(bin_name); man_page.set_extension("1"); let mut file = File::create(&man_page)?; Man::new(cmd).render(&mut file) } fn generate_completions>(bin_name: &str, out_dir: P) -> Result<(), io::Error> { let out_dir = out_dir.as_ref().join("completions"); std::fs::create_dir_all(&out_dir)?; let mut cmd = config::Config::command(); Shell::value_variants().iter().try_for_each(|&shell| { clap_complete::generate_to(shell, &mut cmd, bin_name, out_dir.as_path())?; Ok(()) }) }