pub fn render_clap_completion<T: CommandFactory>(
shell: impl Into<Shell>,
bin_name: &str,
) -> Result<Vec<u8>>Available on crate feature
clap only.Expand description
Renders a completion script from a clap::CommandFactory implementation.
This helper is intentionally optional so the core crate does not require clap.
It only renders script bytes; installation and activation are still handled by install.
The shell argument accepts either crate::Shell or crate::clap_complete::Shell.
If you need to tweak or prune the command tree before rendering, use
render_clap_completion_from_command instead.
§Errors
Returns crate::Error::UnsupportedShell for Shell::Other(_).
§Examples
use clap::Parser;
use shellcomp::{InstallRequest, install, render_clap_completion};
#[derive(Parser)]
struct Cli {
#[arg(long)]
verbose: bool,
}
let generator_shell = shellcomp::clap_complete::Shell::Bash;
let script = render_clap_completion::<Cli>(generator_shell, "demo")?;
let report = install(InstallRequest {
shell: generator_shell.into(),
program_name: "demo",
script: &script,
path_override: None,
})?;
assert!(!script.is_empty());
assert_eq!(report.shell, shellcomp::Shell::Bash);Examples found in repository?
examples/clap_integration.rs (line 14)
12fn main() -> Result<(), Box<dyn std::error::Error>> {
13 let generator_shell = shellcomp::clap_complete::Shell::Bash;
14 let script = render_clap_completion::<Cli>(generator_shell, "example-cli")?;
15 let demo_path = env::temp_dir().join(format!("example-cli-{}.bash", std::process::id()));
16 let report = install(InstallRequest {
17 shell: generator_shell.into(),
18 program_name: "example-cli",
19 script: &script,
20 path_override: Some(demo_path.clone()),
21 })?;
22
23 println!("Rendered completion from clap and installed it to a temporary path.");
24 println!("Path: {}", demo_path.display());
25 println!("{report:#?}");
26 Ok(())
27}