pub fn remove_arguments(arguments_to_remove: &[&'static str], arguments: &mut Vec<String>) {
for argument in arguments_to_remove.iter() {
let (remove_two, clean_argument) = if let Some(stripped) = argument.strip_suffix('=') {
(true, format!("--{}", stripped))
} else {
(false, format!("--{}", argument))
};
if let Some(index) = arguments
.iter()
.position(|x| x.starts_with(&format!("--{}", argument)))
{
arguments.remove(index);
}
if remove_two {
if let Some(index) = arguments.iter().position(|x| x == &clean_argument) {
arguments.remove(index);
arguments.remove(index);
}
}
}
}
#[test]
fn remove_arguments_test() {
let arguments_to_remove = [
"chip=",
"chip-description-path=",
"list-chips",
"disable-progressbars",
"protocol=",
"probe-index=",
"gdb",
"no-download",
"reset-halt",
"gdb-connection-string=",
"nrf-recover",
];
let mut arguments = vec![
"--chip-description-path=kek".to_string(),
"--chip-description-path".to_string(),
"kek".to_string(),
"--chip=kek".to_string(),
"--chip".to_string(),
"kek".to_string(),
"--list-chips".to_string(),
"--disable-progressbars".to_string(),
"--protocol=kek".to_string(),
"--protocol".to_string(),
"kek".to_string(),
"--probe-index=kek".to_string(),
"--probe-index".to_string(),
"kek".to_string(),
"--gdb".to_string(),
"--no-download".to_string(),
"--reset-halt".to_string(),
"--gdb-connection-string=kek".to_string(),
"--gdb-connection-string".to_string(),
"kek".to_string(),
"--nrf-recover".to_string(),
];
remove_arguments(&arguments_to_remove, &mut arguments);
assert!(arguments.is_empty());
}