From 71f261f6d8ad80cffa11074dfcae178a017e8f22 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn Date: Tue, 28 Jul 2026 15:21:41 -0500 Subject: [PATCH] feat: extend parser to support combining flag options --- src/args.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/src/args.rs b/src/args.rs index a600876..a3deb4e 100644 --- a/src/args.rs +++ b/src/args.rs @@ -47,12 +47,26 @@ impl Cli { match arg.as_str() { FLAG_HELP | FLAG_HELP_LONG => return CliAction::PrintHelp, FLAG_VERSION | FLAG_VERSION_LONG => return CliAction::PrintVersion, - FLAG_NOTIFY | FLAG_NOTIFY_LONG => enable_notifications = true, FLAG_SOUND | FLAG_SOUND_LONG => enable_sound = true, FLAG_VISUALIZER | FLAG_VISUALIZER_LONG => enable_visualizer = true, "--" => {} + s if s.starts_with("--") => match s { + FLAG_NOTIFY_LONG => enable_notifications = true, + FLAG_VISUALIZER_LONG => enable_visualizer = true, + FLAG_SOUND_LONG => enable_sound = true, + _ => return CliAction::Error(format!("Unknown option: '{}'", s)), + }, s if s.starts_with('-') => { - return CliAction::Error(format!("Unknown option: '{}'", s)); + for ch in s.chars().skip(1) { + match ch { + 'h' => return CliAction::PrintHelp, + 'v' => return CliAction::PrintVersion, + 'n' => enable_notifications = true, + 's' => enable_sound = true, + 'z' => enable_visualizer = true, + _ => return CliAction::Error(format!("Unknown option: '-{}'", ch)), + } + } } s => positional.push(s.to_string()), } @@ -213,4 +227,33 @@ mod tests { }) ); } + + #[test] + fn test_combined_short_flags() { + let args = vec![ + "termato".to_string(), + "-nsz".to_string(), + "25".to_string(), + "5".to_string(), + ]; + assert_eq!( + Cli::parse_args(args), + CliAction::Run(CliConfig { + work_mins: 25, + break_mins: 5, + enable_notifications: true, + enable_sound: true, + enable_visualizer: true, + }) + ); + } + + #[test] + fn test_combined_short_flags_with_invalid_char() { + let args = vec!["termato".to_string(), "-nxz".to_string()]; + assert_eq!( + Cli::parse_args(args), + CliAction::Error("Unknown option: '-x'".to_string()) + ); + } }