How to use middleware for core shell commands

I’m not sure about the exact solution to your problem because I don’t know the differences between a regular request startup process and a command line startup.

However, here is a diagram of the regular request startup. CakePHP 4.x Request/Resonse Sequence Overview

Step 8 is where you can get involved in both Cake and Plugin bootstrap processes. Step 11 is where you can operate on both Cake and Plugin Middleware initialization.

So, Application.php might be your only tool to effect things so early. After those two calls, things are mostly underway.