Repository: carbon-language/carbon-lang
File: scripts/lldbinit.py (Python)

Code before:
    # Look for <type><id> as a single argument.
    if m := re.fullmatch("([a-z_]+)(\\d+)", args[1]):
        if m[1] in id_types:
            if len(args) != 2:
                print_usage()
                return
            id_type = m[1]
            id = f"SemIR::{id_types[id_type]}({m[2]})"

    # Look for <type> <id> as two arguments.
    if not id_type:
        if args[1] in id_types:
            if len(args) != 3:
                print_usage()
                return
            id_type = args[1]
            id = f"SemIR::{id_types[id_type]}({args[2]})"

    if not id_type:
        # Use `--` to escape a variable name like `inst22`.
        if args[1] == "--":
            id = " ".join(args[2:])
        else:
            id = " ".join(args[1:])

    cmd = f"p Dump({context}, {id})"
    out = RunCommand(cmd, print_command=False)
    if m := re.match(dump_re, out):
        print(m[1])
    else:
        # Unexpected output, show the command that was run.
        print(f"(lldb) {cmd}")
        print(out)


def __lldb_init_module(debugger: Any, internal_dict: Any) -> None:
    RunCommand("command script add -f lldbinit.cmd_dump dump")


Review comment (refactor):
Just looking one more time at the code structure, had you considered putting the final print in a helper function, like `def print_id`, similar to `print_usage`? Mainly I was wondering because of the `if not id_type` flow, which I assume you're doing so that the `print_usage` calls return to where you want. But then I was thinking about nesting, and not sure what you'd think of something like:

```
    print_id(f"SemIR::...")
    return
```

which by the return, would mean you wouldn't need as much nesting.

Anyways, I think this code is plenty good as-is, so I'm going to approve and leave this to you to choose.

Code after:
            # Use the `dump_re` match to print just the interesting part of the
            # dump output.
            print(m[1])
        else:
            # Unexpected output, show the command that was run.
            print(f"(lldb) {cmd}")
            print(out)

    # Try to find a type + id from the input args. If not, the id will be passed
    # through directly to C++, as it can be a variable name.
    id_type = None

    # Look for <type><id> as a single argument.
    if m := re.fullmatch("([a-z_]+)(\\d+)", args[1]):
        if m[1] in id_types:
            if len(args) != 2:
                print_usage()
                return
            id_type = m[1]
            print_dump(context, f"{id_types[id_type]}({m[2]})")

    # Look for <type> <id> as two arguments.
    if not id_type:
        if args[1] in id_types:
            if len(args) != 3:
                print_usage()
                return
            id_type = args[1]
            print_dump(context, f"{id_types[id_type]}({args[2]})")

    if not id_type:
        # Use `--` to escape a variable name like `inst22`.
        if args[1] == "--":
            expr = " ".join(args[2:])
        else:
            expr = " ".join(args[1:])
        print_dump(context, expr)


def __lldb_init_module(debugger: Any, internal_dict: Any) -> None:
    RunCommand("command script add -f lldbinit.cmd_dump dump")