rocketmq_admin_core/
rocketmq_cli.rs1use clap::CommandFactory;
16use clap::Parser;
17use clap_complete::generate;
18use clap_complete::shells::Bash;
19use clap_complete::shells::Fish;
20use clap_complete::shells::Zsh;
21
22use crate::commands::CommandExecute;
23use crate::commands::Commands;
24
25#[derive(Parser)]
26#[command(name = "rocketmq-admin-cli-rust")]
27#[command(about = "Rocketmq Rust admin commands", long_about = None, author="mxsm")]
28pub struct RocketMQCli {
29 #[arg(
30 long = "generate-completion",
31 value_name = "SHELL",
32 help = "Generate shell completion script (bash, zsh, fish)"
33 )]
34 completion: Option<String>,
35
36 #[command(subcommand)]
37 commands: Option<Commands>,
38}
39
40impl RocketMQCli {
41 pub async fn handle(&self) {
42 if let Some(shell) = &self.completion {
44 let mut cmd = RocketMQCli::command();
45 let bin_name = "rocketmq-admin-cli-rust";
46
47 match shell.to_lowercase().as_str() {
48 "bash" => {
49 generate(Bash, &mut cmd, bin_name, &mut std::io::stdout());
50 }
51 "zsh" => {
52 generate(Zsh, &mut cmd, bin_name, &mut std::io::stdout());
53 }
54 "fish" => {
55 generate(Fish, &mut cmd, bin_name, &mut std::io::stdout());
56 }
57 _ => {
58 eprintln!("Unsupported shell: {}", shell);
59 eprintln!("Supported shells: bash, zsh, fish");
60 std::process::exit(1);
61 }
62 }
63 return;
64 }
65
66 if let Some(ref commands) = self.commands {
68 if let Err(e) = commands.execute(None).await {
69 eprintln!("Error: {e}");
70 }
71 } else {
72 eprintln!("No command specified. Use --help for usage information.");
73 }
74 }
75}