Skip to content

Commit 38c509e

Browse files
authored
Merge pull request #6133 from wp-cli/copilot/show-default-values-prompt
2 parents fe7027d + f8f240e commit 38c509e

File tree

2 files changed

+149
-2
lines changed

2 files changed

+149
-2
lines changed

features/prompt.feature

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,135 @@ Feature: Prompt user for input
292292
wp test-sensitive --username='admin' --password='[REDACTED]' --api-key='[REDACTED]'
293293
"""
294294
295+
Scenario: Flag prompt should accept Y for yes
296+
Given an empty directory
297+
And a cmd.php file:
298+
"""
299+
<?php
300+
/**
301+
* Test that flag prompt accepts Y.
302+
*
303+
* ## OPTIONS
304+
*
305+
* [--flag]
306+
* : An optional flag
307+
*
308+
* @when before_wp_load
309+
*/
310+
WP_CLI::add_command( 'test-flag-y', function( $_, $assoc_args ){
311+
$flag_value = isset( $assoc_args['flag'] ) ? 'true' : 'false';
312+
WP_CLI::line( 'flag: ' . $flag_value );
313+
});
314+
"""
315+
And a yes-response file:
316+
"""
317+
Y
318+
"""
319+
And a wp-cli.yml file:
320+
"""
321+
require:
322+
- cmd.php
323+
"""
324+
325+
When I run `wp test-flag-y --prompt < yes-response`
326+
Then STDOUT should contain:
327+
"""
328+
wp test-flag-y --flag
329+
"""
330+
And STDOUT should contain:
331+
"""
332+
flag: true
333+
"""
334+
335+
Scenario: Assoc param with default should apply default on empty input
336+
Given an empty directory
337+
And a cmd.php file:
338+
"""
339+
<?php
340+
/**
341+
* Test that default value is applied for assoc params.
342+
*
343+
* ## OPTIONS
344+
*
345+
* [--format=<format>]
346+
* : Output format
347+
* ---
348+
* default: table
349+
* options:
350+
* - table
351+
* - csv
352+
* - json
353+
* ---
354+
*
355+
* @when before_wp_load
356+
*/
357+
WP_CLI::add_command( 'test-assoc-default', function( $_, $assoc_args ){
358+
WP_CLI::line( 'format: ' . $assoc_args['format'] );
359+
});
360+
"""
361+
And a empty-response file:
362+
"""
363+
364+
"""
365+
And a wp-cli.yml file:
366+
"""
367+
require:
368+
- cmd.php
369+
"""
370+
371+
When I run `wp test-assoc-default --prompt < empty-response`
372+
Then STDOUT should contain:
373+
"""
374+
wp test-assoc-default --format='table'
375+
"""
376+
And STDOUT should contain:
377+
"""
378+
format: table
379+
"""
380+
381+
Scenario: Positional arg with default should apply default on empty input
382+
Given an empty directory
383+
And a cmd.php file:
384+
"""
385+
<?php
386+
/**
387+
* Test that default value is applied for positional args.
388+
*
389+
* ## OPTIONS
390+
*
391+
* [<name>]
392+
* : The name
393+
* ---
394+
* default: World
395+
* ---
396+
*
397+
* @when before_wp_load
398+
*/
399+
WP_CLI::add_command( 'test-positional-default', function( $args, $_ ){
400+
$name = isset( $args[0] ) ? $args[0] : 'Nobody';
401+
WP_CLI::line( 'Hello ' . $name );
402+
});
403+
"""
404+
And a empty-response file:
405+
"""
406+
407+
"""
408+
And a wp-cli.yml file:
409+
"""
410+
require:
411+
- cmd.php
412+
"""
413+
414+
When I run `wp test-positional-default --prompt < empty-response`
415+
Then STDOUT should contain:
416+
"""
417+
wp test-positional-default 'World'
418+
"""
419+
And STDOUT should contain:
420+
"""
421+
Hello World
422+
"""
423+
295424
Scenario: Prompt should display argument descriptions
296425
Given an empty directory
297426
And a cmd.php file:

php/WP_CLI/Dispatcher/Subcommand.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ private function prompt_args( $args, $assoc_args ) {
183183
return [ $args, $assoc_args ];
184184
}
185185

186+
// Create a docparser to get default values and descriptions
187+
$docparser = $this->get_docparser();
188+
186189
// To skip the already provided positional arguments, we need to count
187190
// how many we had already received.
188191
$arg_index = 0;
@@ -276,7 +279,8 @@ function ( $spec_arg ) use ( $args, $assoc_args, &$arg_index ) {
276279
} while ( $repeat );
277280

278281
} else {
279-
$prompt = $current_prompt . $spec_arg['token'];
282+
$prompt = $current_prompt . $spec_arg['token'];
283+
$default_val = null;
280284

281285
// Add description if available
282286
$longdesc = $this->get_longdesc();
@@ -286,16 +290,30 @@ function ( $spec_arg ) use ( $args, $assoc_args, &$arg_index ) {
286290
$prompt .= ' (' . $description . ')';
287291
}
288292

293+
// Get default value for the argument (not for flags)
289294
if ( 'flag' === $spec_arg['type'] ) {
290295
$prompt .= ' (Y/n)';
296+
} elseif ( 'positional' === $spec_arg['type'] || 'assoc' === $spec_arg['type'] ) {
297+
$spec_args = ( 'positional' === $spec_arg['type'] )
298+
? $docparser->get_arg_args( $spec_arg['name'] )
299+
: $docparser->get_param_args( $spec_arg['name'] );
300+
if ( null !== $spec_args && isset( $spec_args['default'] ) ) {
301+
$default_val = $spec_args['default'];
302+
$prompt .= ' [' . $default_val . ']';
303+
}
291304
}
292305

293306
$response = $this->prompt( $prompt );
294307
if ( false === $response ) {
295308
return [ $args, $assoc_args ];
296309
}
297310

298-
if ( $response ) {
311+
// If response is empty and there's a default (not a flag), use the default
312+
if ( '' === $response && null !== $default_val ) {
313+
$response = $default_val;
314+
}
315+
316+
if ( '' !== $response ) {
299317
switch ( $spec_arg['type'] ) {
300318
case 'positional':
301319
if ( $spec_arg['repeating'] ) {

0 commit comments

Comments
 (0)