Do not modify command line arguments that come after --

This commit is contained in:
Caelan Sayler
2024-05-23 23:09:33 +01:00
parent a971237db4
commit b1cc241ed9

View File

@@ -64,11 +64,33 @@ fn root_command() -> Command {
fn parse_command_line_matches(input_args: Vec<String>) -> ArgMatches { fn parse_command_line_matches(input_args: Vec<String>) -> ArgMatches {
// Split the arguments manually to handle the legacy `--flag=value` syntax // Split the arguments manually to handle the legacy `--flag=value` syntax
// Also, replace `--processStartAndWait` with `--processStart --wait` // Also, replace `--processStartAndWait` with `--processStart --wait`
let args: Vec<String> = input_args let mut args = Vec::new();
.into_iter() let mut preserve = false;
.flat_map(|arg| if arg.contains('=') { arg.splitn(2, '=').map(String::from).collect::<Vec<_>>() } else { vec![arg] }) for arg in input_args {
.flat_map(|arg| if arg.eq_ignore_ascii_case("--processStartAndWait") { vec!["--processStart".to_string(), "--wait".to_string()] } else { vec![arg] }) if preserve {
.collect(); args.push(arg);
} else if arg == "--" {
args.push("--".to_string());
preserve = true;
} else if arg.eq_ignore_ascii_case("--processStartAndWait") {
args.push("--processStart".to_string());
args.push("--wait".to_string());
} else if arg.starts_with("--processStartAndWait=") {
let mut split_arg = arg.splitn(2, '=');
split_arg.next(); // Skip the `--processStartAndWait` part
args.push("--processStart".to_string());
args.push("--wait".to_string());
if let Some(rest) = split_arg.next() {
args.push(rest.to_string());
}
} else if arg.contains('=') {
let mut split_arg = arg.splitn(2, '=');
args.push(split_arg.next().unwrap().to_string());
args.push(split_arg.next().unwrap().to_string());
} else {
args.push(arg);
}
}
root_command().get_matches_from(&args) root_command().get_matches_from(&args)
} }
@@ -217,13 +239,33 @@ fn test_start_command_supports_legacy_commands() {
fn try_parse_command_line_matches(input_args: Vec<String>) -> Result<ArgMatches> { fn try_parse_command_line_matches(input_args: Vec<String>) -> Result<ArgMatches> {
// Split the arguments manually to handle the legacy `--flag=value` syntax // Split the arguments manually to handle the legacy `--flag=value` syntax
// Also, replace `--processStartAndWait` with `--processStart --wait` // Also, replace `--processStartAndWait` with `--processStart --wait`
let args: Vec<String> = input_args let mut args = Vec::new();
.into_iter() let mut preserve = false;
.flat_map(|arg| if arg.contains('=') { arg.splitn(2, '=').map(String::from).collect::<Vec<_>>() } else { vec![arg] }) for arg in input_args {
.flat_map( if preserve {
|arg| if arg.eq_ignore_ascii_case("--processStartAndWait") { vec!["--processStart".to_string(), "--wait".to_string()] } else { vec![arg] }, args.push(arg);
) } else if arg == "--" {
.collect(); args.push("--".to_string());
preserve = true;
} else if arg.eq_ignore_ascii_case("--processStartAndWait") {
args.push("--processStart".to_string());
args.push("--wait".to_string());
} else if arg.starts_with("--processStartAndWait=") {
let mut split_arg = arg.splitn(2, '=');
split_arg.next(); // Skip the `--processStartAndWait` part
args.push("--processStart".to_string());
args.push("--wait".to_string());
if let Some(rest) = split_arg.next() {
args.push(rest.to_string());
}
} else if arg.contains('=') {
let mut split_arg = arg.splitn(2, '=');
args.push(split_arg.next().unwrap().to_string());
args.push(split_arg.next().unwrap().to_string());
} else {
args.push(arg);
}
}
root_command().try_get_matches_from(&args).map_err(|e| anyhow!("{}", e)) root_command().try_get_matches_from(&args).map_err(|e| anyhow!("{}", e))
} }
@@ -251,6 +293,14 @@ fn test_start_command_supports_legacy_commands() {
assert_eq!(legacy_args, None); assert_eq!(legacy_args, None);
assert_eq!(exe_args, None); assert_eq!(exe_args, None);
let command = vec!["Update.exe", "--processStartAndWait=hello.exe", "--", "Foo=Bar"];
let matches = try_parse_command_line_matches(command.iter().map(|s| s.to_string()).collect()).unwrap();
let (wait_for_parent, exe_name, legacy_args, exe_args) = get_start_args(matches.subcommand_matches("start").unwrap());
assert_eq!(wait_for_parent, true);
assert_eq!(exe_name, Some(&"hello.exe".to_string()));
assert_eq!(legacy_args, None);
assert_eq!(exe_args, Some(vec![&"Foo=Bar".to_string()]));
let command = vec!["Update.exe", "--processStartAndWait", "hello.exe", "-a", "myarg"]; let command = vec!["Update.exe", "--processStartAndWait", "hello.exe", "-a", "myarg"];
let matches = try_parse_command_line_matches(command.iter().map(|s| s.to_string()).collect()).unwrap(); let matches = try_parse_command_line_matches(command.iter().map(|s| s.to_string()).collect()).unwrap();
let (wait_for_parent, exe_name, legacy_args, exe_args) = get_start_args(matches.subcommand_matches("start").unwrap()); let (wait_for_parent, exe_name, legacy_args, exe_args) = get_start_args(matches.subcommand_matches("start").unwrap());