Trying very hard not to miss calendar events
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Rework CLI using clap

Co-authored-by: Claude <noreply@anthropic.com>

+51 -26
+1
Cargo.toml
··· 12 12 gio-sys = "0.20" 13 13 libc = "0.2" 14 14 chrono = "0.4" 15 + clap = { version = "4.5.54", features = ["derive"] } 15 16 16 17 [build-dependencies] 17 18 pkg-config = "0.3"
+50 -26
src/main.rs
··· 1 1 mod constants; 2 2 mod eds; 3 3 4 - use std::env; 4 + use clap::{Parser, Subcommand}; 5 + 6 + #[derive(Parser)] 7 + #[command(name = "list_events")] 8 + #[command(about = "Evolution Data Server test program", long_about = None)] 9 + #[command(version)] 10 + struct Cli { 11 + #[command(subcommand)] 12 + command: Commands, 13 + } 14 + 15 + #[derive(Subcommand)] 16 + enum Commands { 17 + /// Show library version information 18 + Version, 19 + /// Calendar sources operations 20 + Sources { 21 + #[command(subcommand)] 22 + action: SourcesCommands, 23 + }, 24 + /// Calendar events operations 25 + Events { 26 + #[command(subcommand)] 27 + action: EventsCommands, 28 + }, 29 + } 30 + 31 + #[derive(Subcommand)] 32 + enum SourcesCommands { 33 + /// List available calendar sources 34 + List, 35 + } 36 + 37 + #[derive(Subcommand)] 38 + enum EventsCommands { 39 + /// List events from now to 60 days ahead 40 + List, 41 + } 5 42 6 43 fn print_version() { 7 44 let version = eds::GLibVersion::get(); ··· 11 48 " GLib version: {}.{}.{}", 12 49 version.major, version.minor, version.micro 13 50 ); 14 - } 15 - 16 - fn print_usage(program_name: &str) { 17 - println!("Usage: {} [COMMAND]", program_name); 18 - println!("\nCommands:"); 19 - println!(" --version Show version information"); 20 - println!(" sources list List available calendar sources"); 21 - println!(" events list List events from now to 60 days ahead"); 22 51 } 23 52 24 53 fn list_sources() -> i32 { ··· 134 163 } 135 164 136 165 fn main() { 137 - let args: Vec<String> = env::args().collect(); 166 + let cli = Cli::parse(); 138 167 139 - if args.len() < 2 { 140 - print_usage(&args[0]); 141 - std::process::exit(1); 142 - } 143 - 144 - let exit_code = if args[1] == "--version" { 145 - print_version(); 146 - 0 147 - } else if args.len() >= 3 && args[1] == "sources" && args[2] == "list" { 148 - list_sources() 149 - } else if args.len() >= 3 && args[1] == "events" && args[2] == "list" { 150 - list_events() 151 - } else { 152 - eprintln!("Unknown command\n"); 153 - print_usage(&args[0]); 154 - 1 168 + let exit_code = match cli.command { 169 + Commands::Version => { 170 + print_version(); 171 + 0 172 + } 173 + Commands::Sources { action } => match action { 174 + SourcesCommands::List => list_sources(), 175 + }, 176 + Commands::Events { action } => match action { 177 + EventsCommands::List => list_events(), 178 + }, 155 179 }; 156 180 157 181 std::process::exit(exit_code);