Skip to content

Commit d4378ce

Browse files
committed
Merge branch 'main' into copilot/catch-exceptions-restart-session
2 parents 1a039c7 + 349cd9e commit d4378ce

3 files changed

Lines changed: 70 additions & 4 deletions

File tree

‎features/shell.feature‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,49 @@ Feature: WordPress REPL
258258
Error: The 'shutdown' hook has not fired yet
259259
"""
260260
And the return code should be 1
261+
262+
Scenario: Quiet mode suppresses return value output
263+
Given a WP install
264+
And a session file:
265+
"""
266+
$a = "hello";
267+
"""
268+
269+
When I run `wp shell --basic --quiet < session`
270+
Then STDOUT should be empty
271+
272+
Scenario: Quiet mode still shows echo output
273+
Given a WP install
274+
And a session file:
275+
"""
276+
echo "test output";
277+
"""
278+
279+
When I run `wp shell --basic --quiet < session`
280+
Then STDOUT should contain:
281+
"""
282+
test output
283+
"""
284+
285+
Scenario: Quiet mode with expression
286+
Given a WP install
287+
And a session file:
288+
"""
289+
get_bloginfo('name');
290+
"""
291+
292+
When I run `wp shell --basic --quiet < session`
293+
Then STDOUT should be empty
294+
295+
Scenario: Normal mode shows return value
296+
Given a WP install
297+
And a session file:
298+
"""
299+
$a = "hello";
300+
"""
301+
302+
When I run `wp shell --basic < session`
303+
Then STDOUT should contain:
304+
"""
305+
string(5) "hello"
306+
"""

‎src/Shell_Command.php‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ class Shell_Command extends WP_CLI_Command {
5858
* # Start a shell, ensuring the 'init' hook has already fired.
5959
* $ wp shell --hook=init
6060
*
61+
* # Start a shell in quiet mode, suppressing return value output.
62+
* $ wp shell --quiet
63+
* wp> $a = "hello";
64+
* wp>
65+
*
6166
* @param string[] $_ Positional arguments. Unused.
6267
* @param array{basic?: bool, watch?: string} $assoc_args Associative arguments.
6368
*/
@@ -109,6 +114,7 @@ private function start_shell( $assoc_args ) {
109114
}
110115

111116
$class = WP_CLI\Shell\REPL::class;
117+
$quiet = (bool) WP_CLI::get_config( 'quiet' );
112118

113119
$implementations = array(
114120
\Psy\Shell::class,
@@ -132,6 +138,9 @@ private function start_shell( $assoc_args ) {
132138
if ( \Psy\Shell::class === $class ) {
133139
$shell = new Psy\Shell();
134140
$shell->run();
141+
} elseif ( \Boris\Boris::class === $class ) {
142+
$boris = new \Boris\Boris( 'wp> ' );
143+
$boris->start();
135144
} else {
136145
/**
137146
* @var class-string<WP_CLI\Shell\REPL> $class
@@ -141,7 +150,7 @@ private function start_shell( $assoc_args ) {
141150
}
142151

143152
do {
144-
$repl = new $class( 'wp> ' );
153+
$repl = new $class( 'wp> ', $quiet );
145154
if ( $watch_path ) {
146155
$repl->set_watch_path( $watch_path );
147156
}

‎src/WP_CLI/Shell/REPL.php‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,22 @@ class REPL {
1010

1111
private $history_file;
1212

13+
/** @var bool Whether to suppress automatic output. */
14+
private $quiet;
15+
1316
private $watch_path;
1417

1518
private $watch_mtime;
1619

1720
const EXIT_CODE_RESTART = 10;
1821

19-
public function __construct( $prompt ) {
22+
/**
23+
* @param string $prompt Prompt to display.
24+
* @param bool $quiet Whether to suppress automatic output.
25+
*/
26+
public function __construct( $prompt, $quiet = false ) {
2027
$this->prompt = $prompt;
28+
$this->quiet = $quiet;
2129

2230
$this->set_history_file();
2331
}
@@ -96,8 +104,11 @@ public function start() {
96104
if ( $__repl_eval_had_error ) {
97105
continue;
98106
}
99-
echo '=> ';
100-
var_dump( $__repl_eval_result );
107+
ob_start();
108+
if ( ! $this->quiet ) {
109+
echo '=> ';
110+
var_dump( $__repl_eval_result );
111+
}
101112
fwrite( STDOUT, (string) ob_get_clean() );
102113
}
103114
}

0 commit comments

Comments
 (0)