Read a --patterns-from command from string and return a CmdTuple object.
(cmd_line_str, fallback=ShellPattern)
| 345 | |
| 346 | |
| 347 | def parse_inclexcl_command(cmd_line_str, fallback=ShellPattern): |
| 348 | """Read a --patterns-from command from string and return a CmdTuple object.""" |
| 349 | |
| 350 | cmd_prefix_map = { |
| 351 | "-": IECommand.Exclude, |
| 352 | "!": IECommand.ExcludeNoRecurse, |
| 353 | "+": IECommand.Include, |
| 354 | "R": IECommand.RootPath, |
| 355 | "r": IECommand.RootPath, |
| 356 | "P": IECommand.PatternStyle, |
| 357 | "p": IECommand.PatternStyle, |
| 358 | } |
| 359 | if not cmd_line_str: |
| 360 | raise ArgumentTypeError("A pattern/command must not be empty.") |
| 361 | |
| 362 | cmd = cmd_prefix_map.get(cmd_line_str[0]) |
| 363 | if cmd is None: |
| 364 | raise ArgumentTypeError("A pattern/command must start with any of: %s" % ", ".join(cmd_prefix_map)) |
| 365 | |
| 366 | # remaining text on command-line following the command character |
| 367 | remainder_str = cmd_line_str[1:].lstrip() |
| 368 | if not remainder_str: |
| 369 | raise ArgumentTypeError("A pattern/command must have a value part.") |
| 370 | |
| 371 | if cmd is IECommand.RootPath: |
| 372 | # TODO: validate string? |
| 373 | val = remainder_str |
| 374 | elif cmd is IECommand.PatternStyle: |
| 375 | # then remainder_str is something like 're' or 'sh' |
| 376 | try: |
| 377 | val = get_pattern_class(remainder_str) |
| 378 | except ValueError: |
| 379 | raise ArgumentTypeError(f"Invalid pattern style: {remainder_str}") |
| 380 | else: |
| 381 | # determine recurse_dir based on command type |
| 382 | recurse_dir = command_recurses_dir(cmd) |
| 383 | val = parse_pattern(remainder_str, fallback, recurse_dir) |
| 384 | |
| 385 | return CmdTuple(val, cmd) |
| 386 | |
| 387 | |
| 388 | def get_regex_from_pattern(pattern: str) -> str: |
no test coverage detected