1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//! Shell completion generation for Ralph CLI.
//!
//! Provides the `ralph completions` subcommand to generate shell completion
//! scripts for bash, zsh, fish, and PowerShell.
use clap::{CommandFactory, Parser, ValueEnum};
use clap_complete::{Shell, generate};
use std::io;
/// Arguments for the completions subcommand.
#[derive(Parser, Debug)]
pub struct CompletionsArgs {
/// The shell to generate completions for
#[arg(value_enum)]
pub shell: ShellArg,
}
/// Shell options for completion generation.
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum ShellArg {
/// Bash shell completions
Bash,
/// Zsh shell completions
Zsh,
/// Fish shell completions
Fish,
/// PowerShell completions
PowerShell,
}
impl From<ShellArg> for Shell {
fn from(arg: ShellArg) -> Self {
match arg {
ShellArg::Bash => Shell::Bash,
ShellArg::Zsh => Shell::Zsh,
ShellArg::Fish => Shell::Fish,
ShellArg::PowerShell => Shell::PowerShell,
}
}
}
/// Generate shell completions for the given shell.
///
/// This function uses clap_complete to generate a completion script
/// for the specified shell, printing it to stdout.
pub fn generate_completions(args: &CompletionsArgs) {
let shell: Shell = args.shell.into();
let mut cmd = crate::Cli::command();
generate(shell, &mut cmd, "ralph", &mut io::stdout());
}