Skip to content
Open
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
46 changes: 46 additions & 0 deletions features/shell.feature
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,49 @@ Feature: WordPress REPL
Error: The 'shutdown' hook has not fired yet
"""
And the return code should be 1

Scenario: Quiet mode suppresses return value output
Given a WP install
And a session file:
"""
$a = "hello";
"""

When I run `wp shell --basic --quiet < session`
Then STDOUT should be empty

Scenario: Quiet mode still shows echo output
Given a WP install
And a session file:
"""
echo "test output";
"""

When I run `wp shell --basic --quiet < session`
Then STDOUT should contain:
"""
test output
"""

Scenario: Quiet mode with expression
Given a WP install
And a session file:
"""
get_bloginfo('name');
"""

When I run `wp shell --basic --quiet < session`
Then STDOUT should be empty

Scenario: Normal mode shows return value
Given a WP install
And a session file:
"""
$a = "hello";
"""

When I run `wp shell --basic < session`
Then STDOUT should contain:
"""
string(5) "hello"
"""
11 changes: 10 additions & 1 deletion src/Shell_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class Shell_Command extends WP_CLI_Command {
*
* # Start a shell, ensuring the 'init' hook has already fired.
* $ wp shell --hook=init
*
* # Start a shell in quiet mode, suppressing return value output.
* $ wp shell --quiet
* wp> $a = "hello";
* wp>
*/
public function __invoke( $_, $assoc_args ) {
$hook = Utils\get_flag_value( $assoc_args, 'hook', '' );
Expand Down Expand Up @@ -71,6 +76,7 @@ public function __invoke( $_, $assoc_args ) {
*/
private function start_shell( $assoc_args ) {
$class = WP_CLI\Shell\REPL::class;
$quiet = (bool) WP_CLI::get_config( 'quiet' );

$implementations = array(
\Psy\Shell::class,
Expand All @@ -94,11 +100,14 @@ private function start_shell( $assoc_args ) {
if ( \Psy\Shell::class === $class ) {
$shell = new Psy\Shell();
$shell->run();
} elseif ( \Boris\Boris::class === $class ) {
$boris = new \Boris\Boris( 'wp> ' );
$boris->start();
} else {
/**
* @var class-string<WP_CLI\Shell\REPL> $class
*/
$repl = new $class( 'wp> ' );
$repl = new $class( 'wp> ', $quiet );
$repl->start();
}
}
Expand Down
17 changes: 14 additions & 3 deletions src/WP_CLI/Shell/REPL.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ class REPL {

private $history_file;

public function __construct( $prompt ) {
/** @var bool Whether to suppress automatic output. */
private $quiet;

/**
* @param string $prompt Prompt to display.
* @param bool $quiet Whether to suppress automatic output.
*/
public function __construct( $prompt, $quiet = false ) {
$this->prompt = $prompt;
$this->quiet = $quiet;

$this->set_history_file();
}
Expand Down Expand Up @@ -49,8 +57,11 @@ public function start() {
if ( 0 < strlen( $__repl_output ) ) {
echo rtrim( $__repl_output, "\n" ) . "\n";
}
echo '=> ';
var_dump( $__repl_eval_result );
ob_start();
if ( ! $this->quiet ) {
echo '=> ';
var_dump( $__repl_eval_result );
}
fwrite( STDOUT, (string) ob_get_clean() );
}
}
Expand Down