Skip to main content

cardanowall_cli/commands/
completion.rs

1//! `cardanowall completion <shell>` — print a shell completion script to stdout.
2//!
3//! Generated from the same clap command tree the binary uses, so the completions
4//! never drift from the real flags. Install instructions live in the crate README.
5
6use clap::{Args, CommandFactory, ValueEnum};
7use clap_complete::{generate, Shell};
8
9use crate::cli::Cli;
10use crate::util::CliError;
11
12/// Arguments for `cardanowall completion`.
13#[derive(Debug, Args)]
14pub struct CompletionArgs {
15    /// the shell to generate a completion script for.
16    #[arg(value_enum)]
17    pub shell: CompletionShell,
18}
19
20/// The shells we emit completions for (a thin mirror of [`clap_complete::Shell`]
21/// so the value-enum surface is part of our own help text).
22#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
23pub enum CompletionShell {
24    /// Bash.
25    Bash,
26    /// Zsh.
27    Zsh,
28    /// Fish.
29    Fish,
30    /// PowerShell.
31    Powershell,
32}
33
34impl From<CompletionShell> for Shell {
35    fn from(value: CompletionShell) -> Self {
36        match value {
37            CompletionShell::Bash => Shell::Bash,
38            CompletionShell::Zsh => Shell::Zsh,
39            CompletionShell::Fish => Shell::Fish,
40            CompletionShell::Powershell => Shell::PowerShell,
41        }
42    }
43}
44
45/// Run the `completion` command: write the script to stdout.
46///
47/// # Errors
48///
49/// Infallible in practice; returns `Ok(())`.
50pub fn run(args: CompletionArgs) -> Result<(), CliError> {
51    let mut cmd = Cli::command();
52    let bin_name = cmd.get_name().to_string();
53    generate(
54        Shell::from(args.shell),
55        &mut cmd,
56        bin_name,
57        &mut std::io::stdout(),
58    );
59    Ok(())
60}