From c3fd41e4de76c44d4ecdd5bd4fdc907536a750fb Mon Sep 17 00:00:00 2001 From: Greyforge Admin Date: Wed, 20 May 2026 00:48:56 -0400 Subject: [PATCH] Accept pwsh for shell completions --- src/cortex-cli/src/cli/args.rs | 26 ++++++++++++++++++++- src/cortex-cli/tests/completion_pwsh.rs | 30 +++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 src/cortex-cli/tests/completion_pwsh.rs diff --git a/src/cortex-cli/src/cli/args.rs b/src/cortex-cli/src/cli/args.rs index 641d63a4a..8357580ef 100644 --- a/src/cortex-cli/src/cli/args.rs +++ b/src/cortex-cli/src/cli/args.rs @@ -551,7 +551,7 @@ pub struct LogoutCommand { #[derive(Args)] pub struct CompletionCommand { /// Shell to generate completions for. - #[arg(value_enum)] + #[arg(value_parser = parse_completion_shell)] pub shell: Option, /// Install completions to your shell configuration file. @@ -559,6 +559,19 @@ pub struct CompletionCommand { pub install: bool, } +fn parse_completion_shell(value: &str) -> Result { + match value.to_ascii_lowercase().as_str() { + "bash" => Ok(clap_complete::Shell::Bash), + "elvish" => Ok(clap_complete::Shell::Elvish), + "fish" => Ok(clap_complete::Shell::Fish), + "powershell" | "pwsh" => Ok(clap_complete::Shell::PowerShell), + "zsh" => Ok(clap_complete::Shell::Zsh), + _ => Err(format!( + "invalid shell '{value}'; expected one of bash, elvish, fish, powershell, pwsh, zsh" + )), + } +} + /// Init command - initialize AGENTS.md. #[derive(Args)] pub struct InitCommand { @@ -1754,6 +1767,17 @@ mod tests { } } + #[test] + fn test_completion_command_pwsh_alias() { + let cli = Cli::try_parse_from(["cortex", "completion", "pwsh"]) + .expect("should parse completion pwsh"); + if let Some(Commands::Completion(completion)) = cli.command { + assert_eq!(completion.shell, Some(clap_complete::Shell::PowerShell)); + } else { + panic!("Expected Completion command"); + } + } + #[test] fn test_completion_command_install() { let cli = Cli::try_parse_from(["cortex", "completion", "--install"]) diff --git a/src/cortex-cli/tests/completion_pwsh.rs b/src/cortex-cli/tests/completion_pwsh.rs new file mode 100644 index 000000000..f1df3a142 --- /dev/null +++ b/src/cortex-cli/tests/completion_pwsh.rs @@ -0,0 +1,30 @@ +use std::fs; +use std::process::Command; + +use tempfile::tempdir; + +#[test] +fn completion_pwsh_is_accepted_as_powershell_alias() { + let home = tempdir().unwrap(); + let output = Command::new(env!("CARGO_BIN_EXE_Cortex")) + .args(["completion", "pwsh", "--install"]) + .env("HOME", home.path()) + .output() + .unwrap(); + + assert!( + output.status.success(), + "completion pwsh failed:\n{}{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + + let profile = home + .path() + .join(".config/powershell/Microsoft.PowerShell_profile.ps1"); + let profile = fs::read_to_string(profile).unwrap(); + assert!( + profile.contains("cortex completion powershell"), + "profile:\n{profile}" + ); +}