Skip to content

Commit

Permalink
Add runtime_args to options array for runcommand
Browse files Browse the repository at this point in the history
Allow a caller to pass runtime arguments to WP_CLI::runcommand so that it is possible to store all 'default' options in one place.

ex if you want to avoid plugins and themes per default in a script that calls runcommand a lot:

```
$default_cli_opts  = array(
	'return'       => 'all',
	'exit_error'   => false,
	'runtime_args' => array( '--skip-themes', '--skip-plugins' ),
);

$output = WP_CLI::runcommand( 'option get siteurl', $default_cli_opts);
```
  • Loading branch information
mrsdizzie committed Jan 18, 2023
1 parent 61c86b9 commit 01797e8
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions php/class-wp-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -1269,13 +1269,15 @@ public static function get_config( $key = null ) {
* * Prevent halting script execution on error.
* * Capture and return STDOUT, or full details about command execution.
* * Parse JSON output if the command rendered it.
* * Include additional runtime arguments to be used with command.
*
* ```
* $options = array(
* 'return' => true, // Return 'STDOUT'; use 'all' for full object.
* 'parse' => 'json', // Parse captured STDOUT to JSON array.
* 'launch' => false, // Reuse the current process.
* 'exit_error' => true, // Halt script execution on error.
* 'runtime_args' => array('--skip-themes'), // Additional runtime arguments to be used with $command
* );
* $plugins = WP_CLI::runcommand( 'plugin list --format=json', $options );
* ```
Expand All @@ -1288,18 +1290,25 @@ public static function get_config( $key = null ) {
* @return mixed
*/
public static function runcommand( $command, $options = [] ) {
$defaults = [
'launch' => true, // Launch a new process, or reuse the existing.
'exit_error' => true, // Exit on error by default.
'return' => false, // Capture and return output, or render in realtime.
'parse' => false, // Parse returned output as a particular format.
$defaults = [
'launch' => true, // Launch a new process, or reuse the existing.
'exit_error' => true, // Exit on error by default.
'return' => false, // Capture and return output, or render in realtime.
'parse' => false, // Parse returned output as a particular format.
'runtime_args' => array(), //Include optional runtime arguments
];
$options = array_merge( $defaults, $options );
$launch = $options['launch'];
$exit_error = $options['exit_error'];
$return = $options['return'];
$parse = $options['parse'];
$retval = null;
$options = array_merge( $defaults, $options );
$launch = $options['launch'];
$exit_error = $options['exit_error'];
$return = $options['return'];
$parse = $options['parse'];
$runtime_args = $options['runtime_args'];

if ( ! empty( $runtime_args ) ) {
$command = $command . ' ' . implode( ' ', $runtime_args );
}

$retval = null;
if ( $launch ) {
Utils\check_proc_available( 'launch option' );

Expand Down

0 comments on commit 01797e8

Please sign in to comment.