Split a compound command into individual command segments. Handles command chaining (&&, ||, ;) but not pipes (those are single commands). Args: command_string: The full shell command Returns: List of individual command segments
(command_string: str)
| 140 | |
| 141 | |
| 142 | def split_command_segments(command_string: str) -> list[str]: |
| 143 | """ |
| 144 | Split a compound command into individual command segments. |
| 145 | |
| 146 | Handles command chaining (&&, ||, ;) but not pipes (those are single commands). |
| 147 | |
| 148 | Args: |
| 149 | command_string: The full shell command |
| 150 | |
| 151 | Returns: |
| 152 | List of individual command segments |
| 153 | """ |
| 154 | import re |
| 155 | |
| 156 | # Split on && and || while preserving the ability to handle each segment |
| 157 | # This regex splits on && or || that aren't inside quotes |
| 158 | segments = re.split(r"\s*(?:&&|\|\|)\s*", command_string) |
| 159 | |
| 160 | # Further split on semicolons |
| 161 | result = [] |
| 162 | for segment in segments: |
| 163 | sub_segments = re.split(r'(?<!["\'])\s*;\s*(?!["\'])', segment) |
| 164 | for sub in sub_segments: |
| 165 | sub = sub.strip() |
| 166 | if sub: |
| 167 | result.append(sub) |
| 168 | |
| 169 | return result |
| 170 | |
| 171 | |
| 172 | def _extract_primary_command(segment: str) -> str | None: |
no outgoing calls
no test coverage detected