feat: extend parser to support combining flag options

This commit is contained in:
Stevan Freeborn
2026-07-28 15:21:41 -05:00
parent 71eb940afc
commit 71f261f6d8
+45 -2
View File
@@ -47,12 +47,26 @@ impl Cli {
match arg.as_str() { match arg.as_str() {
FLAG_HELP | FLAG_HELP_LONG => return CliAction::PrintHelp, FLAG_HELP | FLAG_HELP_LONG => return CliAction::PrintHelp,
FLAG_VERSION | FLAG_VERSION_LONG => return CliAction::PrintVersion, 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_SOUND | FLAG_SOUND_LONG => enable_sound = true,
FLAG_VISUALIZER | FLAG_VISUALIZER_LONG => enable_visualizer = 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('-') => { 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()), 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())
);
}
} }