Skip to main content

render_clap_completion_from_command

Function render_clap_completion_from_command 

Source
pub fn render_clap_completion_from_command(
    shell: impl Into<Shell>,
    bin_name: &str,
    command: Command,
) -> Result<Vec<u8>>
Available on crate feature clap only.
Expand description

Renders a completion script from a prebuilt clap::Command.

Use this when the caller needs to construct the real command tree first and then apply lightweight adjustments before rendering, such as adding build-specific flags or hiding an internal subcommand from completions.

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.

§Errors

Returns crate::Error::UnsupportedShell for Shell::Other(_).

§Examples

use clap::{Arg, CommandFactory, Parser};
use shellcomp::render_clap_completion_from_command;

#[derive(Parser)]
struct Cli {
    #[arg(long)]
    verbose: bool,
}

let command = Cli::command().arg(Arg::new("profile").long("profile"));
let script = render_clap_completion_from_command(
    shellcomp::clap_complete::Shell::Fish,
    "demo",
    command,
)?;

assert!(!script.is_empty());