//! Implements `limb completions`. Emits shell completion scripts.

use std::io;

use anyhow::Result;
use clap::CommandFactory;
use clap_complete::generate;

use crate::cli::{Cli, CompletionsArgs};

/// Runs `limb completions`.
///
/// Writes the `clap_complete`-generated script for the requested shell
/// to stdout. Infallible in practice; the `Result` is for signature
/// symmetry with other `run` functions.
///
/// # Errors
///
/// The return type is `Result` for dispatch-symmetry; no error paths are
/// currently exercised.
#[allow(clippy::unnecessary_wraps)]
pub fn run(args: &CompletionsArgs) -> Result<()> {
    let mut cmd = Cli::command();
    let name = cmd.get_name().to_string();
    generate(
        clap_complete::Shell::from(args.shell),
        &mut cmd,
        name,
        &mut io::stdout(),
    );
    Ok(())
}
