|
1 | 1 | use anyhow::Result; |
2 | | -use clap::{Parser, Subcommand}; |
| 2 | +use clap::{CommandFactory, Parser, Subcommand}; |
| 3 | +use clap_complete::{Shell, generate}; |
3 | 4 | use repos::commands::validators; |
4 | 5 | use repos::{commands::*, config::Config, constants, plugins}; |
5 | | -use std::{env, path::PathBuf}; |
| 6 | +use std::{env, io, path::PathBuf}; |
6 | 7 |
|
7 | 8 | #[derive(Parser)] |
8 | 9 | #[command(name = "repos")] |
@@ -193,6 +194,13 @@ enum Commands { |
193 | 194 | supplement: bool, |
194 | 195 | }, |
195 | 196 |
|
| 197 | + /// Generate shell completions |
| 198 | + Completions { |
| 199 | + /// Shell to generate completions for |
| 200 | + #[arg(value_enum)] |
| 201 | + shell: Shell, |
| 202 | + }, |
| 203 | + |
196 | 204 | /// External plugin command |
197 | 205 | #[command(external_subcommand)] |
198 | 206 | External(Vec<String>), |
@@ -221,6 +229,11 @@ async fn main() -> Result<()> { |
221 | 229 |
|
222 | 230 | // Handle commands |
223 | 231 | match cli.command { |
| 232 | + Some(Commands::Completions { shell }) => { |
| 233 | + let mut cmd = Cli::command(); |
| 234 | + generate(shell, &mut cmd, "repos", &mut io::stdout()); |
| 235 | + return Ok(()); |
| 236 | + } |
224 | 237 | Some(Commands::External(args)) => { |
225 | 238 | if args.is_empty() { |
226 | 239 | anyhow::bail!("External command provided but no arguments given"); |
@@ -502,6 +515,10 @@ async fn execute_builtin_command(command: Commands) -> Result<()> { |
502 | 515 | .execute(&context) |
503 | 516 | .await?; |
504 | 517 | } |
| 518 | + Commands::Completions { .. } => { |
| 519 | + // Handled in main(), this should not be reached |
| 520 | + unreachable!("Completions command should be handled in main()") |
| 521 | + } |
505 | 522 | } |
506 | 523 |
|
507 | 524 | Ok(()) |
|
0 commit comments