Skip to content

Commit 3385cc2

Browse files
authored
Merge pull request #6059 from karthick-murugan/wpcli/command-suggestion
Improve command suggestions for taxonomies and post types
2 parents c0513d0 + 9931116 commit 3385cc2

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

features/runner.feature

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,23 @@ Feature: Runner WP-CLI
7979
Did you mean 'meta'?
8080
"""
8181
And the return code should be 1
82+
83+
Scenario: Suggest 'wp term <command>' when an invalid taxonomy command is run
84+
Given a WP install
85+
86+
When I try `wp category list`
87+
Then STDERR should contain:
88+
"""
89+
Did you mean 'wp term <command>'?
90+
"""
91+
And the return code should be 1
92+
93+
Scenario: Suggest 'wp post <command>' when an invalid post type command is run
94+
Given a WP install
95+
96+
When I try `wp page create`
97+
Then STDERR should contain:
98+
"""
99+
Did you mean 'wp post --post_type=page <command>'?
100+
"""
101+
And the return code should be 1

php/WP_CLI/Runner.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,16 @@ public function find_command_to_run( $args ) {
396396

397397
$suggestion = $this->get_subcommand_suggestion( $full_name, $command );
398398

399+
// If the functions are available, it means WordPress is available
400+
// and has already been loaded.
401+
if ( function_exists( '\taxonomy_exists' ) ) {
402+
if ( \taxonomy_exists( $cmd_path[0] ) ) {
403+
$suggestion = 'wp term <command>';
404+
} elseif ( \post_type_exists( $cmd_path[0] ) ) {
405+
$suggestion = "wp post --post_type={$cmd_path[0]} <command>";
406+
}
407+
}
408+
399409
return sprintf(
400410
"'%s' is not a registered wp command. See 'wp help' for available commands.%s",
401411
$full_name,

0 commit comments

Comments
 (0)