src/Codeception/Module/WPCLI.php
public function cliToArray($userCommand = 'post list --format=ids', callable  $splitCallback = null)
    {
        $this->initPaths();

        $command = $this->buildCommand($userCommand);

        $this->debugSection('command', $command);
        $output = $this->executor->execAndOutput($command, $status);
        $this->debugSection('output', $output);

        $this->evaluateStatus($output, $status);

        if (empty($output)) {
            return [];
        }

        $hasSplitCallback = null !== $splitCallback;
        $originalOutput   = $output;
        if (!is_array($output) || (is_array($output) && $hasSplitCallback)) {
            if (is_array($output)) {
                $output = implode(PHP_EOL, $output);
            }
            if (!$hasSplitCallback) {
                if (!preg_match('/[\\n]+/', $output)) {
                    $output = preg_split('/\\s+/', $output);
                } else {
                    $output = preg_split('/\\s*\\n+\\s*/', $output);
                }
            } else {
                $output = $splitCallback($output, $userCommand, $this);
            }
        }

        if (!is_array($output) && $hasSplitCallback) {
            throw new ModuleException(
                __CLASS__,
                "Split callback must return an array, it returned: \n" . print_r(
                    $output,
                    true
                ) . "\nfor original output:\n" . print_r(
                    $originalOutput,
                    true
                )
            );
        }

        return empty($output) ? [] : array_map('trim', $output);
    }
public function cliToArray($userCommand = 'post list --format=ids', callable  $splitCallback = null)
    {
        $this->initPaths();

        $command = $this->buildCommand($userCommand);

        $this->debugSection('command', $command);
        $output = $this->executor->execAndOutput($command, $status);
        $this->debugSection('output', $output);

        $this->evaluateStatus($output, $status);

        if (empty($output)) {
            return [];
        }

        $hasSplitCallback = null !== $splitCallback;
        $originalOutput   = $output;
        if (!is_array($output) || (is_array($output) && $hasSplitCallback)) {
            if (is_array($output)) {
                $output = implode(PHP_EOL, $output);
            }
            if (!$hasSplitCallback) {
                if (!preg_match('/[\\n]+/', $output)) {
                    $output = preg_split('/\\s+/', $output);
                } else {
                    $output = preg_split('/\\s*\\n+\\s*/', $output);
                }
            } else {
                $output = $splitCallback($output, $userCommand, $this);
            }
        }

        if (!is_array($output) && $hasSplitCallback) {
            throw new ModuleException(
                __CLASS__,
                "Split callback must return an array, it returned: \n" . print_r(
                    $output,
                    true
                ) . "\nfor original output:\n" . print_r(
                    $originalOutput,
                    true
                )
            );
        }

        return empty($output) ? [] : array_map('trim', $output);
    }
public function cliToArray ( $ userCommand = 'post list --format=ids' , callable $ splitCallback = null ) { $ this -> initPaths ( ) ; $ command = $ this -> buildCommand ( $ userCommand ) ; $ this -> debugSection ( 'command' , $ command ) ; $ output = $ this -> executor -> execAndOutput ( $ command , $ status ) ; $ this -> debugSection ( 'output' , $ output ) ; $ this -> evaluateStatus ( $ output , $ status ) ; if ( empty ( $ output ) ) { return [ ] ; } $ hasSplitCallback = null !== $ splitCallback ; $ originalOutput = $ output ; if ( ! is_array ( $ output ) || ( is_array ( $ output ) && $ hasSplitCallback ) ) { if ( is_array ( $ output ) ) { $ output = implode ( PHP_EOL , $ output ) ; } if ( ! $ hasSplitCallback ) { if ( ! preg_match ( '/[\\n]+/' , $ output ) ) { $ output = preg_split ( '/\\s+/' , $ output ) ; } else { $ output = preg_split ( '/\\s*\\n+\\s*/' , $ output ) ; } } else { $ output = $ splitCallback ( $ output , $ userCommand , $ this ) ; } } if ( ! is_array ( $ output ) && $ hasSplitCallback ) { throw new ModuleException ( __CLASS__ , "Split callback must return an array, it returned: \n" . print_r ( $ output , true ) . "\nfor original output:\n" . print_r ( $ originalOutput , true ) ) ; } return empty ( $ output ) ? [ ] : array_map ( 'trim' , $ output ) ; }
Returns the output of a wp-cli command as an array optionally allowing a callback to process the output.

@example
```php
// Return a list of inactive themes, like ['twentyfourteen', 'twentyfifteen'].
$inactiveThemes = $I->cliToArray('theme list --status=inactive --field=name');
// Get the list of installed plugins and only keep the ones starting with "foo".
$fooPlugins = $I->cliToArray('plugin list --field=name', function($output){
return array_filter(explode(PHP_EOL, $output), function($name){
return strpos(trim($name), 'foo') === 0;
});
});
```

@param string $userCommand The string of command and parameters as it would be passed to wp-cli minus `wp`.
@param callable $splitCallback An optional callback function in charge of splitting the results array.

@return array An array containing the output of wp-cli split into single elements.

@throws \Codeception\Exception\ModuleException If the $splitCallback function does not return an array.
Returns the output of a wp - cli command as an array optionally allowing a callback to process the output .