Skip to main content

usage/complete/
mod.rs

1use crate::error::UsageErr;
2use crate::Spec;
3
4mod bash;
5mod fish;
6mod powershell;
7mod zsh;
8
9/// Options for generating shell completion scripts.
10pub struct CompleteOptions {
11    /// Path to the `usage` binary (e.g., "usage" or "/usr/local/bin/usage").
12    pub usage_bin: String,
13    /// Target shell: "bash", "fish", "zsh", or "powershell".
14    pub shell: String,
15    /// Name of the CLI binary to generate completions for.
16    pub bin: String,
17    /// Optional cache key (e.g., version) to avoid regenerating the spec file.
18    pub cache_key: Option<String>,
19    /// The usage spec to embed directly in the completion script.
20    pub spec: Option<Spec>,
21    /// Command to run to generate the usage spec dynamically.
22    pub usage_cmd: Option<String>,
23    /// Whether to include the bash-completion library sourcing (bash only).
24    pub include_bash_completion_lib: bool,
25    /// Source file path for the `@generated` comment.
26    pub source_file: Option<String>,
27}
28
29/// Generates a shell completion script for the specified shell.
30///
31/// # Arguments
32/// * `options` - Configuration options including target shell and spec source
33///
34/// # Returns
35/// The generated completion script as a string, or an error if the shell is unsupported.
36///
37/// # Supported Shells
38/// - `bash` - Bash completion using `complete` builtin
39/// - `fish` - Fish shell completions
40/// - `zsh` - Zsh completion using `compdef`
41/// - `powershell` - PowerShell completion using `Register-ArgumentCompleter`
42pub fn complete(options: &CompleteOptions) -> Result<String, UsageErr> {
43    match options.shell.as_str() {
44        "bash" => Ok(bash::complete_bash(options)),
45        "fish" => Ok(fish::complete_fish(options)),
46        "powershell" => Ok(powershell::complete_powershell(options)),
47        "zsh" => Ok(zsh::complete_zsh(options)),
48        _ => Err(UsageErr::UnsupportedShell(options.shell.clone())),
49    }
50}