Fallback command extraction when shlex fails. Extracts the first word that looks like a command, handling cases like complex docker exec commands with nested quotes. Args: segment: The command segment to parse Returns: The primary command name, or None if extr
(segment: str)
| 170 | |
| 171 | |
| 172 | def _extract_primary_command(segment: str) -> str | None: |
| 173 | """ |
| 174 | Fallback command extraction when shlex fails. |
| 175 | |
| 176 | Extracts the first word that looks like a command, handling cases |
| 177 | like complex docker exec commands with nested quotes. |
| 178 | |
| 179 | Args: |
| 180 | segment: The command segment to parse |
| 181 | |
| 182 | Returns: |
| 183 | The primary command name, or None if extraction fails |
| 184 | """ |
| 185 | # Remove leading whitespace |
| 186 | segment = segment.lstrip() |
| 187 | |
| 188 | if not segment: |
| 189 | return None |
| 190 | |
| 191 | # Skip env var assignments at start (VAR=value cmd) |
| 192 | words = segment.split() |
| 193 | while words and "=" in words[0] and not words[0].startswith("="): |
| 194 | words = words[1:] |
| 195 | |
| 196 | if not words: |
| 197 | return None |
| 198 | |
| 199 | # Extract first token (the command) |
| 200 | first_word = words[0] |
| 201 | |
| 202 | # Match valid command characters (alphanumeric, dots, underscores, hyphens, slashes) |
| 203 | match = re.match(r"^([a-zA-Z0-9_./-]+)", first_word) |
| 204 | if match: |
| 205 | cmd = match.group(1) |
| 206 | return os.path.basename(cmd) |
| 207 | |
| 208 | return None |
| 209 | |
| 210 | |
| 211 | def extract_commands(command_string: str) -> list[str]: |