Pass in args to before_run_command hook by CodeProKid · Pull Request #5554 · wp-cli/wp-cli
This PR passes in some args to the before_run_command hook so callbacks have some more context to base their logic off of.
The reason for proposing this change is because of a particular use case I have where I'm implementing some logging to track CLI commands that are being run in our codebase. I currently have an implementation that looks something like:
WP_CLI::add_hook( 'before_run_command', function() { $runner = WP_CLI::get_runner(); $command = $runner->find_command_to_run( $runner->arguments )[0]; log( $command->get_name() ); } );
This works for commands invoked from the command line but doesn't work for commands invoked via WP_CLI::run_command with launch => false set. This is because run_command will call the Runner->run_command() method directly so the Runner instance doesn't get bootstrapped again with the proper context.
To get around this, I'm proposing we pass the params from the run_command method into the before_run_command hook.