33// file, You can obtain one at https://mozilla.org/MPL/2.0/.
44
55use andromeda_core:: RuntimeFile ;
6- use clap:: { Parser as ClapParser , Subcommand } ;
6+ use clap:: { CommandFactory , Parser as ClapParser , Subcommand } ;
7+ use clap_complete:: { Shell , generate} ;
78use libsui:: find_section;
9+ use std:: io;
810use std:: path:: PathBuf ;
911
1012mod compile;
@@ -82,6 +84,12 @@ enum Command {
8284 #[ arg( required = false ) ]
8385 paths : Vec < PathBuf > ,
8486 } ,
87+ /// Generate shell completion scripts
88+ Completions {
89+ /// The shell to generate completions for
90+ #[ arg( value_enum) ]
91+ shell : Option < Shell > ,
92+ } ,
8593}
8694
8795fn main ( ) {
@@ -169,9 +177,12 @@ fn run_main() -> Result<()> {
169177 }
170178 Ok ( ( ) )
171179 }
180+ Command :: Completions { shell } => {
181+ generate_completions ( shell) ;
182+ Ok ( ( ) )
183+ }
172184 }
173185 } ) ;
174-
175186 match rt. block_on ( nova_thread) {
176187 Ok ( result) => result,
177188 Err ( e) => Err ( AndromedaError :: config_error (
@@ -181,3 +192,46 @@ fn run_main() -> Result<()> {
181192 ) ) ,
182193 }
183194}
195+
196+ fn generate_completions ( shell : Option < Shell > ) {
197+ let mut cmd = Cli :: command ( ) ;
198+ let bin_name = "andromeda" ;
199+
200+ match shell {
201+ Some ( shell) => {
202+ generate ( shell, & mut cmd, bin_name, & mut io:: stdout ( ) ) ;
203+ }
204+ None => {
205+ // If no shell is specified, try to detect it from environment
206+ // This mimics Deno's behavior
207+ if let Some ( detected_shell) = detect_shell ( ) {
208+ generate ( detected_shell, & mut cmd, bin_name, & mut io:: stdout ( ) ) ;
209+ } else {
210+ eprintln ! (
211+ "Error: Could not detect shell. Please specify one of: bash, zsh, fish, powershell"
212+ ) ;
213+ std:: process:: exit ( 1 ) ;
214+ }
215+ }
216+ }
217+ }
218+
219+ fn detect_shell ( ) -> Option < Shell > {
220+ // Try to detect shell from environment variables
221+ if let Ok ( shell) = std:: env:: var ( "SHELL" ) {
222+ if shell. contains ( "bash" ) {
223+ return Some ( Shell :: Bash ) ;
224+ } else if shell. contains ( "zsh" ) {
225+ return Some ( Shell :: Zsh ) ;
226+ } else if shell. contains ( "fish" ) {
227+ return Some ( Shell :: Fish ) ;
228+ }
229+ }
230+
231+ // On Windows, check for PowerShell
232+ if cfg ! ( windows) && std:: env:: var ( "PSModulePath" ) . is_ok ( ) {
233+ return Some ( Shell :: PowerShell ) ;
234+ }
235+
236+ None
237+ }
0 commit comments