Skip to main content

perl_dap_command_args/
lib.rs

1//! Platform-aware shell argument formatting used by `perl-dap`.
2
3/// Format command-line arguments for platform-specific shells.
4#[must_use]
5pub fn format_command_args(args: &[String]) -> Vec<String> {
6    args.iter()
7        .map(|arg| {
8            if arg.contains(' ') {
9                #[cfg(windows)]
10                {
11                    format!("\"{}\"", arg.replace('"', "\\\""))
12                }
13                #[cfg(not(windows))]
14                {
15                    if arg.contains('\'') {
16                        format!("\"{}\"", arg.replace('"', "\\\""))
17                    } else {
18                        format!("'{}'", arg)
19                    }
20                }
21            } else {
22                arg.clone()
23            }
24        })
25        .collect()
26}
27
28#[cfg(test)]
29mod tests {
30    use super::format_command_args;
31
32    #[test]
33    fn leaves_simple_args_unmodified() {
34        let args = vec!["plain".to_string(), "--flag".to_string()];
35        let formatted = format_command_args(&args);
36        assert_eq!(formatted, args);
37    }
38
39    #[test]
40    fn quotes_args_with_spaces() {
41        let args = vec!["file with spaces.txt".to_string()];
42        let formatted = format_command_args(&args);
43        assert_eq!(formatted.len(), 1);
44        assert!(formatted[0].contains("file with spaces.txt"));
45        assert_ne!(formatted[0], "file with spaces.txt");
46    }
47}