Add shell completions
- Add a `build.rs` (plus dependencies) to generate the shell completions clap knows.
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
//! build
|
||||
|
||||
use anyhow::Result;
|
||||
use anyhow;
|
||||
use clap::CommandFactory;
|
||||
use clap::ValueEnum;
|
||||
use clap_complete::Shell;
|
||||
|
||||
use std::env;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
// include the cli definition
|
||||
pub mod cli {
|
||||
include!("src/cli/cli_args.rs");
|
||||
}
|
||||
|
||||
const ASSET_OUT_ENV_NAME: &str = "ASSET_OUT_DIR";
|
||||
|
||||
/// Returns a path to a directory suitable as a target for generated
|
||||
/// files.
|
||||
fn out_dir(section: &str) -> Result<PathBuf> {
|
||||
println!("cargo:rerun-if-env-changed={}", ASSET_OUT_ENV_NAME);
|
||||
|
||||
let outdir: PathBuf = env::var_os(ASSET_OUT_ENV_NAME)
|
||||
.unwrap_or_else(|| env::var_os("OUT_DIR")
|
||||
.expect("OUT_DIR not set")
|
||||
).into();
|
||||
|
||||
if outdir.exists() && ! outdir.is_dir() {
|
||||
return Err(anyhow::anyhow!("{:?} is not a directory", outdir));
|
||||
}
|
||||
let path = outdir.join(section);
|
||||
fs::create_dir_all(&path)?;
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let mut husk = cli::CliArgs::command();
|
||||
|
||||
// shell completions
|
||||
let shell_completion_path = out_dir("shell_completions")?;
|
||||
for shell in Shell::value_variants() {
|
||||
clap_complete::generate_to(*shell, &mut husk, "husk", &shell_completion_path)?;
|
||||
}
|
||||
|
||||
println!("cargo:warning=shell completions written to {}", shell_completion_path.display());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user