cqlsh_rs/
shell_completions.rs

1//! Shell completion generation for cqlsh-rs CLI arguments.
2//!
3//! Generates completion scripts for bash, zsh, fish, elvish, and PowerShell
4//! using clap_complete. Usage:
5//!
6//! ```sh
7//! # Generate bash completions
8//! cqlsh-rs --completions bash > /etc/bash_completion.d/cqlsh-rs
9//!
10//! # Generate zsh completions
11//! cqlsh-rs --completions zsh > ~/.zfunc/_cqlsh-rs
12//!
13//! # Generate fish completions
14//! cqlsh-rs --completions fish > ~/.config/fish/completions/cqlsh-rs.fish
15//! ```
16
17use clap::CommandFactory;
18use clap_complete::{generate as gen_complete, Shell};
19
20use crate::cli::CliArgs;
21
22/// Generate shell completion script for the given shell and write to stdout.
23pub fn generate(shell: Shell) {
24    let mut cmd = CliArgs::command();
25    gen_complete(shell, &mut cmd, "cqlsh-rs", &mut std::io::stdout());
26}
27
28#[cfg(test)]
29mod tests {
30    use super::*;
31
32    #[test]
33    fn generate_bash_completions_does_not_panic() {
34        let mut cmd = CliArgs::command();
35        let mut buf = Vec::new();
36        gen_complete(Shell::Bash, &mut cmd, "cqlsh-rs", &mut buf);
37        let output = String::from_utf8(buf).unwrap();
38        assert!(output.contains("cqlsh-rs"));
39    }
40
41    #[test]
42    fn generate_zsh_completions_does_not_panic() {
43        let mut cmd = CliArgs::command();
44        let mut buf = Vec::new();
45        gen_complete(Shell::Zsh, &mut cmd, "cqlsh-rs", &mut buf);
46        let output = String::from_utf8(buf).unwrap();
47        assert!(output.contains("cqlsh-rs"));
48    }
49
50    #[test]
51    fn generate_fish_completions_does_not_panic() {
52        let mut cmd = CliArgs::command();
53        let mut buf = Vec::new();
54        gen_complete(Shell::Fish, &mut cmd, "cqlsh-rs", &mut buf);
55        let output = String::from_utf8(buf).unwrap();
56        assert!(output.contains("cqlsh-rs"));
57    }
58
59    #[test]
60    fn generate_powershell_completions_does_not_panic() {
61        let mut cmd = CliArgs::command();
62        let mut buf = Vec::new();
63        gen_complete(Shell::PowerShell, &mut cmd, "cqlsh-rs", &mut buf);
64        let output = String::from_utf8(buf).unwrap();
65        assert!(output.contains("cqlsh-rs"));
66    }
67
68    #[test]
69    fn generate_elvish_completions_does_not_panic() {
70        let mut cmd = CliArgs::command();
71        let mut buf = Vec::new();
72        gen_complete(Shell::Elvish, &mut cmd, "cqlsh-rs", &mut buf);
73        let output = String::from_utf8(buf).unwrap();
74        assert!(output.contains("cqlsh-rs"));
75    }
76}