Skip to main content

omni_dev/cli/
completions.rs

1//! `omni-dev completions <shell>` — emit a shell completion script generated
2//! from the live clap command tree.
3
4use anyhow::Result;
5use clap::{CommandFactory, Parser};
6use clap_complete::{generate, Shell};
7
8use crate::cli::Cli;
9
10/// Shell-completion-script generator subcommand.
11#[derive(Parser)]
12pub struct CompletionsCommand {
13    /// Target shell (`bash`, `elvish`, `fish`, `powershell`, `zsh`).
14    #[arg(value_enum)]
15    pub shell: Shell,
16}
17
18impl CompletionsCommand {
19    /// Writes the completion script for `self.shell` to stdout.
20    pub fn execute(self) -> Result<()> {
21        let mut cmd = Cli::command();
22        generate(self.shell, &mut cmd, "omni-dev", &mut std::io::stdout());
23        Ok(())
24    }
25}
26
27#[cfg(test)]
28#[allow(clippy::unwrap_used, clippy::expect_used)]
29mod tests {
30    use super::*;
31    use crate::cli::Commands;
32
33    fn assert_parses_to(arg: &str, expected: Shell) {
34        let cli = Cli::try_parse_from(["omni-dev", "completions", arg]).unwrap();
35        assert!(matches!(
36            cli.command,
37            Commands::Completions(CompletionsCommand { shell }) if shell == expected
38        ));
39    }
40
41    #[test]
42    fn clap_parses_completions_bash() {
43        assert_parses_to("bash", Shell::Bash);
44    }
45
46    #[test]
47    fn clap_parses_completions_zsh() {
48        assert_parses_to("zsh", Shell::Zsh);
49    }
50
51    #[test]
52    fn clap_parses_completions_fish() {
53        assert_parses_to("fish", Shell::Fish);
54    }
55
56    #[test]
57    fn clap_parses_completions_powershell() {
58        assert_parses_to("powershell", Shell::PowerShell);
59    }
60
61    #[test]
62    fn clap_parses_completions_elvish() {
63        assert_parses_to("elvish", Shell::Elvish);
64    }
65
66    #[test]
67    fn clap_rejects_unknown_shell() {
68        let result = Cli::try_parse_from(["omni-dev", "completions", "banana"]);
69        assert!(result.is_err());
70    }
71}