use clap::CommandFactory;
use clap_complete::Shell;
pub fn emit<C: CommandFactory>(shell: Shell, bin_name: &str, out: &mut impl std::io::Write) {
let mut cmd = C::command();
clap_complete::generate(shell, &mut cmd, bin_name, out);
}
#[cfg(test)]
mod tests {
use super::*;
use clap::Parser;
#[derive(Debug, Parser)]
#[command(name = "merlion-test")]
#[allow(dead_code)]
struct DummyCli {
#[arg(long)]
flag: bool,
}
#[test]
fn emits_bash_completion_containing_bin_name() {
let mut out: Vec<u8> = Vec::new();
emit::<DummyCli>(Shell::Bash, "merlion", &mut out);
let text = String::from_utf8(out).expect("bash completion is utf8");
assert!(
text.contains("merlion"),
"bash completion should mention bin name `merlion`, got:\n{text}"
);
}
#[test]
fn emits_zsh_completion_containing_bin_name() {
let mut out: Vec<u8> = Vec::new();
emit::<DummyCli>(Shell::Zsh, "merlion", &mut out);
let text = String::from_utf8(out).expect("zsh completion is utf8");
assert!(
text.contains("merlion"),
"zsh completion should mention bin name `merlion`, got:\n{text}"
);
}
}