3

I am missing some feature to prompt the user for input. In Laravel we have $this->ask('What is your age') (https://laravel.com/docs/8.x/artisan#prompting-for-input)

I haven't been able to find any information on this online

1

I didn't find any wp-cli utility either, but readline php function works just fine.

PHPStorm warns me that ext-readline must be enabled in order to use it, but it seems like something that is always enabled in cli environment

0

There's the global parameter

--prompt[=<assoc>]

Prompt the user to enter values for all command arguments, or a subset specified as comma-separated values.

Example - Prompting two arguments

We can prompt post title and content of a given command with:

$ wp post create --prompt=post_title,post_content

4/30 [--post_content=<post_content>]: test
6/30 [--post_title=<post_title>]: test
wp post create --post_content='test' --post_title='test'
Success: Created post 14464.

where the resulting command is:

wp post create --post_content='test' --post_title='test'

Example - Prompting all arguments

We can prompt all arguments of a given command with:

$ wp post create --prompt

1/30 [--post_author=<post_author>]:
2/30 [--post_date=<post_date>]:
3/30 [--post_date_gmt=<post_date_gmt>]:
4/30 [--post_content=<post_content>]: test
5/30 [--post_content_filtered=<post_content_filtered>]:
6/30 [--post_title=<post_title>]: test
7/30 [--post_excerpt=<post_excerpt>]:
8/30 [--post_status=<post_status>]:
9/30 [--post_type=<post_type>]:
10/30 [--comment_status=<comment_status>]:
11/30 [--ping_status=<ping_status>]:
12/30 [--post_password=<post_password>]:
13/30 [--post_name=<post_name>]:
14/30 [--from-post=<post_id>]:
15/30 [--to_ping=<to_ping>]:
16/30 [--pinged=<pinged>]:
17/30 [--post_modified=<post_modified>]:
18/30 [--post_modified_gmt=<post_modified_gmt>]:
19/30 [--post_parent=<post_parent>]:
20/30 [--menu_order=<menu_order>]:
21/30 [--post_mime_type=<post_mime_type>]:
22/30 [--guid=<guid>]:
23/30 [--post_category=<post_category>]:
24/30 [--tags_input=<tags_input>]:
25/30 [--tax_input=<tax_input>]:
26/30 [--meta_input=<meta_input>]:
27/30 [<file>]:
28/30 [--<field>:
29/30 [--edit] (Y/n):
30/30 [--porcelain] (Y/n):
wp post create --post_content='test' --post_title='test'
Success: Created post 14461.

where the resulting command is:

wp post create --post_content='test' --post_title='test'

WP_CLI::confirm

Looking into class-wp-cli.php we also have the y/n confirmation:

 * # `wp db drop` asks for confirmation before dropping the database.
 *
 * WP_CLI::confirm( "Are you sure you want to drop the database?", $assoc_args );

that uses:

$answer = strtolower( trim( fgets( STDIN ) ) );

to get the user input answer.

We could use the construction of the confirm method as a base for more general user inputs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.