Introduce wp cli has-command to detect whether a command is registered#4349
Introduce wp cli has-command to detect whether a command is registered#4349schlessera merged 11 commits intowp-cli:masterfrom
wp cli has-command to detect whether a command is registered#4349Conversation
php/commands/src/CLI_Command.php
Outdated
| * | ||
| * @subcommand has-command | ||
| * | ||
| * @when before_wp_load |
There was a problem hiding this comment.
Having before_wp_load is an issue as it won't detect commands added in plugins/themes. Actually, this is an issue for cmd-dump as well as the whole CLI_Command class is marked as before_wp_load. So I think the before_wp_load needs to be removed from the CLI_Command class and added to the individual commands, except for this command and cmd-dump, where after_wp_load should be used.
There was a problem hiding this comment.
Actually, this is an issue for
cmd-dumpas well
Historically, cmd-dump has only been used for exporting bundled command definition to documentation, so it hasn't actually been an issue.
There was a problem hiding this comment.
Oh, ok - I'll open an issue re individual function whens not being honoured if there's a class when as it probably needs discussion.
php/commands/src/CLI_Command.php
Outdated
| /** | ||
| * Get dump as array. | ||
| */ | ||
| $json_decoded = self::command_to_array( WP_CLI::get_root_command() ); |
php/commands/src/CLI_Command.php
Outdated
| /** | ||
| * If command is input as a string, then explode it into array. | ||
| */ | ||
| $command = ( 1 === count( $_ ) ) ? explode( ' ', $_[0] ) : $_; |
There was a problem hiding this comment.
It'd be simpler just to do $args = explode( ' ', implode( ' ', $_ ) ); which allows any old arg format.
php/commands/src/CLI_Command.php
Outdated
| /** | ||
| * If command doesn't start with WP, then halt script execution and exit with status 1. | ||
| */ | ||
| if ( 'wp' !== $command[0] ) { |
There was a problem hiding this comment.
It'd be nicer to make prefixing with wp optional by doing if ( 'wp' === $args[0] ) { array_shift( $args ); }
php/commands/src/CLI_Command.php
Outdated
| WP_CLI::halt( 1 ); | ||
| } | ||
|
|
||
| /** |
There was a problem hiding this comment.
Re-using Runner::find_command_to_run() would make sense here:
WP_CLI::halt( is_array( WP_CLI::get_runner()->find_command_to_run( $args ) ) ? 0 : 1 );
making the recursive_search() function below and the command_to_array() call above unnecessary.
There was a problem hiding this comment.
This is just such a beautiful way to do this, I did not know about this!
php/commands/src/CLI_Command.php
Outdated
| */ | ||
| public function has_command( $_, $assoc_args ) { | ||
|
|
||
| /** |
There was a problem hiding this comment.
In general in-function comments shouldn't be docblocks and if they're one liners should use // I think.
|
Looks good so far. A general note: As regards to testing, you could go through the following steps:
|
danielbachhuber
left a comment
There was a problem hiding this comment.
👍 Looking good, some minor nits.
php/commands/src/CLI_Command.php
Outdated
| * @when after_wp_load | ||
| * | ||
| * @param array $_ Array of positional arguments. | ||
| * @param array $assoc_args Array of associative arguments. |
There was a problem hiding this comment.
We should remove this argument definition, as it's not a pattern we have elsewhere.
| * <command_name>... | ||
| * : The command | ||
| * | ||
| * ## EXAMPLES |
There was a problem hiding this comment.
Needs another space after this heading.
php/commands/src/CLI_Command.php
Outdated
| * | ||
| * ## EXAMPLES | ||
| * wp cli has-command "wp site delete" | ||
| * wp cli has-command wp site delete |
There was a problem hiding this comment.
Each of these examples should have some explanation:
# The "site delete" command is registered
$ wp cli has-command "site delete"
$ echo $?
0
# The "foo bar" command is not registered
$ wp cli has-command "foo bar"
$ echo $?
1
php/commands/src/CLI_Command.php
Outdated
| // If command is input as a string, then explode it into array. | ||
| $command = explode( ' ', implode( ' ', $_ ) ); | ||
|
|
||
| if ( 'wp' === $command[0] ) { |
There was a problem hiding this comment.
I don't think we should accept "wp" to this command.
|
Made changes as per review |
features/cli.feature
Outdated
| When I try `{PHAR_PATH} cli has-command scaffold package` | ||
| Then the return code should be 1 | ||
|
|
||
| When I run `{PHAR_PATH} package install git@github.com:wp-cli/scaffold-package-command.git` |
There was a problem hiding this comment.
Use a short package identifier here:
When I run {PHAR_PATH} package install wp-cli/scaffold-package-command`
There was a problem hiding this comment.
Probably just because Travis hit the git rate limiting.
With the short package identifier, in this case, you fetch the ZIP through the old package index, instead of the GH versioned source.
php/commands/src/CLI_Command.php
Outdated
| * | ||
| * ## EXAMPLES | ||
| * | ||
| * The "site delete" command is registered. |
There was a problem hiding this comment.
For the examples, you should follow the notation convention, as @danielbachhuber had provided:
Comments start with #, and the command starts with the "basic" prompt $. Lines without either are output.
Here's one example:
/**
* ## EXAMPLES
*
* # The "site delete" command is registered.
* $ wp cli has-command "site delete"
* $ echo $?
* 0
*/
There was a problem hiding this comment.
Here's a link to the documentation for this: https://make.wordpress.org/cli/handbook/documentation-standards/#command-annotation
features/cli.feature
Outdated
| Scenario: Ability to detect a WP-CLI registered command | ||
| Given an empty directory | ||
| And save the {SRC_DIR}/VERSION file as {TRUE_VERSION} | ||
| And a new Phar with version "1.3.0" |
There was a problem hiding this comment.
Why do you need to generate a new Phar here?
There was a problem hiding this comment.
You're correct, there wasn't a need to generate a phar. I replaced it with wp @danielbachhuber
wp cli has-command to detect whether a command is registered
I've created a recursive function that traverses the array of the dump output by
self::command_to_array( WP_CLI::get_root_command() )See #4283