| 527 | } |
| 528 | |
| 529 | fn parse_args(arg: &str) -> Result<Vec<String>> { |
| 530 | // Try to parse as JSON array first |
| 531 | if arg.trim_start().starts_with('[') { |
| 532 | match serde_json::from_str::<Vec<String>>(arg) { |
| 533 | Ok(args) => return Ok(args), |
| 534 | Err(_) => { |
| 535 | bail!( |
| 536 | "Failed to parse arguments as JSON array. Expected format: '[\"arg1\", \"arg2\", \"arg,with,commas\"]'" |
| 537 | ); |
| 538 | }, |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | // Check if the string contains escaped commas |
| 543 | let has_escaped_commas = arg.contains("\\,"); |
| 544 | |
| 545 | if has_escaped_commas { |
| 546 | // Parse with escape support |
| 547 | let mut args = Vec::new(); |
| 548 | let mut current_arg = String::new(); |
| 549 | let mut chars = arg.chars().peekable(); |
| 550 | |
| 551 | while let Some(ch) = chars.next() { |
| 552 | match ch { |
| 553 | '\\' => { |
| 554 | // Handle escape sequences |
| 555 | if let Some(&next_ch) = chars.peek() { |
| 556 | if next_ch == ',' || next_ch == '\\' { |
| 557 | current_arg.push(chars.next().unwrap()); |
| 558 | } else { |
| 559 | current_arg.push(ch); |
| 560 | } |
| 561 | } else { |
| 562 | current_arg.push(ch); |
| 563 | } |
| 564 | }, |
| 565 | ',' => { |
| 566 | // Split on unescaped comma |
| 567 | args.push(current_arg.trim().to_string()); |
| 568 | current_arg.clear(); |
| 569 | }, |
| 570 | _ => { |
| 571 | current_arg.push(ch); |
| 572 | }, |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | // Add the last argument |
| 577 | if !current_arg.is_empty() || !args.is_empty() { |
| 578 | args.push(current_arg.trim().to_string()); |
| 579 | } |
| 580 | |
| 581 | Ok(args) |
| 582 | } else { |
| 583 | // Default behavior: split on commas (backward compatibility) |
| 584 | Ok(arg.split(',').map(|s| s.trim().to_string()).collect()) |
| 585 | } |
| 586 | } |