Format command list for Windows CMD shell execution. Properly quotes arguments that contain spaces or special characters. Args: cmd: Command as list of strings Returns: Command string suitable for shell=True execution
(cmd: list[str])
| 123 | |
| 124 | |
| 125 | def format_cmd_for_shell(cmd: list[str]) -> str: |
| 126 | """Format command list for Windows CMD shell execution. |
| 127 | |
| 128 | Properly quotes arguments that contain spaces or special characters. |
| 129 | |
| 130 | Args: |
| 131 | cmd: Command as list of strings |
| 132 | |
| 133 | Returns: |
| 134 | Command string suitable for shell=True execution |
| 135 | """ |
| 136 | quoted_parts: list[str] = [] |
| 137 | for part in cmd: |
| 138 | # Quote if contains spaces or special characters |
| 139 | if " " in part or any(c in part for c in "&|<>^"): |
| 140 | # Escape quotes inside the part |
| 141 | escaped = part.replace('"', '\\"') |
| 142 | quoted_parts.append(f'"{escaped}"') |
| 143 | else: |
| 144 | quoted_parts.append(part) |
| 145 | |
| 146 | return " ".join(quoted_parts) |
no test coverage detected