diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-02-23 01:02:54 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-02-23 01:57:21 +0900 |
| commit | 6676b4bb36159e161b6bb0edbe5b3762f43832f4 (patch) | |
| tree | 8e0467d83aab227114e0838b0023507428556b49 /crates/mozart/src/commands | |
| parent | d707cc1ed26efa62c74243254a95fae17192a840 (diff) | |
| download | php-mozart-6676b4bb36159e161b6bb0edbe5b3762f43832f4.tar.gz php-mozart-6676b4bb36159e161b6bb0edbe5b3762f43832f4.tar.zst php-mozart-6676b4bb36159e161b6bb0edbe5b3762f43832f4.zip | |
fix(completion): auto-detect shell from $SHELL when argument omitted
Composer auto-detects the shell from the $SHELL environment variable
when the shell argument is not provided. Mozart previously required
the argument and errored without it.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Diffstat (limited to 'crates/mozart/src/commands')
| -rw-r--r-- | crates/mozart/src/commands/completion.rs | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/crates/mozart/src/commands/completion.rs b/crates/mozart/src/commands/completion.rs index d095ce6..7cae278 100644 --- a/crates/mozart/src/commands/completion.rs +++ b/crates/mozart/src/commands/completion.rs @@ -4,9 +4,9 @@ use clap_complete::aot::Shell; #[derive(Args)] pub struct CompletionArgs { - /// The shell to generate completions for + /// The shell to generate completions for (auto-detected from $SHELL if omitted) #[arg(value_enum)] - pub shell: Shell, + pub shell: Option<Shell>, } pub async fn execute( @@ -14,7 +14,31 @@ pub async fn execute( _cli: &super::Cli, _console: &mozart_core::console::Console, ) -> anyhow::Result<()> { + let shell = match args.shell { + Some(s) => s, + None => detect_shell()?, + }; let mut cmd = super::Cli::command(); - clap_complete::aot::generate(args.shell, &mut cmd, "mozart", &mut std::io::stdout()); + clap_complete::aot::generate(shell, &mut cmd, "mozart", &mut std::io::stdout()); Ok(()) } + +fn detect_shell() -> anyhow::Result<Shell> { + let shell_env = std::env::var("SHELL") + .map_err(|_| anyhow::anyhow!("Could not auto-detect shell. Please specify one of: bash, elvish, fish, powershell, zsh"))?; + let basename = std::path::Path::new(&shell_env) + .file_name() + .and_then(|s| s.to_str()) + .unwrap_or(""); + match basename { + "bash" => Ok(Shell::Bash), + "zsh" => Ok(Shell::Zsh), + "fish" => Ok(Shell::Fish), + "elvish" => Ok(Shell::Elvish), + "pwsh" | "powershell" => Ok(Shell::PowerShell), + _ => anyhow::bail!( + "Unrecognized shell '{}'. Please specify one of: bash, elvish, fish, powershell, zsh", + basename + ), + } +} |
