Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions features/runcommand.feature
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,40 @@ Feature: Run a WP-CLI command
| func |
| proc_open |
| proc_close |

Scenario: Check that command_args provided to runcommand are used in command
Given a WP installation
And a custom-cmd.php file:
"""
<?php
class Custom_Command extends WP_CLI_Command {

/**
* Custom command to test passing command_args via runcommand options
*
* @when after_wp_load
*/
public function echo_test( $args ) {
$cli_opts = array( 'command_args' => array( '--exec="echo \'test\' . PHP_EOL;"' ) );
WP_CLI::runcommand( 'option get home', $cli_opts);
}
public function bad_path( $args ) {
$cli_opts = array( 'command_args' => array('--path=/bad/path' ) );
WP_CLI::runcommand( 'option get home', $cli_opts);
}
}
WP_CLI::add_command( 'custom-command', 'Custom_Command' );
"""

When I run `wp --require=custom-cmd.php custom-command echo_test`
Then STDOUT should be:
"""
test
https://example.com
"""

When I try `wp --require=custom-cmd.php custom-command bad_path`
Then STDERR should contain:
"""
The used path is: /bad/path/
"""
39 changes: 24 additions & 15 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 arguments that are passed to the 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.
* '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.
* 'command_args' => [ '--skip-themes' ], // Additional arguments to be passed to the $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.
'command_args' => [], // Include optional command 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'];
$command_args = $options['command_args'];

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

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

Expand Down