Skip to content

Commit c3fd41e

Browse files
author
Greyforge Admin
committed
Accept pwsh for shell completions
1 parent 7954d02 commit c3fd41e

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

src/cortex-cli/src/cli/args.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,14 +551,27 @@ pub struct LogoutCommand {
551551
#[derive(Args)]
552552
pub struct CompletionCommand {
553553
/// Shell to generate completions for.
554-
#[arg(value_enum)]
554+
#[arg(value_parser = parse_completion_shell)]
555555
pub shell: Option<clap_complete::Shell>,
556556

557557
/// Install completions to your shell configuration file.
558558
#[arg(long = "install")]
559559
pub install: bool,
560560
}
561561

562+
fn parse_completion_shell(value: &str) -> Result<clap_complete::Shell, String> {
563+
match value.to_ascii_lowercase().as_str() {
564+
"bash" => Ok(clap_complete::Shell::Bash),
565+
"elvish" => Ok(clap_complete::Shell::Elvish),
566+
"fish" => Ok(clap_complete::Shell::Fish),
567+
"powershell" | "pwsh" => Ok(clap_complete::Shell::PowerShell),
568+
"zsh" => Ok(clap_complete::Shell::Zsh),
569+
_ => Err(format!(
570+
"invalid shell '{value}'; expected one of bash, elvish, fish, powershell, pwsh, zsh"
571+
)),
572+
}
573+
}
574+
562575
/// Init command - initialize AGENTS.md.
563576
#[derive(Args)]
564577
pub struct InitCommand {
@@ -1754,6 +1767,17 @@ mod tests {
17541767
}
17551768
}
17561769

1770+
#[test]
1771+
fn test_completion_command_pwsh_alias() {
1772+
let cli = Cli::try_parse_from(["cortex", "completion", "pwsh"])
1773+
.expect("should parse completion pwsh");
1774+
if let Some(Commands::Completion(completion)) = cli.command {
1775+
assert_eq!(completion.shell, Some(clap_complete::Shell::PowerShell));
1776+
} else {
1777+
panic!("Expected Completion command");
1778+
}
1779+
}
1780+
17571781
#[test]
17581782
fn test_completion_command_install() {
17591783
let cli = Cli::try_parse_from(["cortex", "completion", "--install"])
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use std::fs;
2+
use std::process::Command;
3+
4+
use tempfile::tempdir;
5+
6+
#[test]
7+
fn completion_pwsh_is_accepted_as_powershell_alias() {
8+
let home = tempdir().unwrap();
9+
let output = Command::new(env!("CARGO_BIN_EXE_Cortex"))
10+
.args(["completion", "pwsh", "--install"])
11+
.env("HOME", home.path())
12+
.output()
13+
.unwrap();
14+
15+
assert!(
16+
output.status.success(),
17+
"completion pwsh failed:\n{}{}",
18+
String::from_utf8_lossy(&output.stdout),
19+
String::from_utf8_lossy(&output.stderr)
20+
);
21+
22+
let profile = home
23+
.path()
24+
.join(".config/powershell/Microsoft.PowerShell_profile.ps1");
25+
let profile = fs::read_to_string(profile).unwrap();
26+
assert!(
27+
profile.contains("cortex completion powershell"),
28+
"profile:\n{profile}"
29+
);
30+
}

0 commit comments

Comments
 (0)