Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce wp cli has-command to detect whether a command is registered #4349

Merged
merged 11 commits into from
Sep 26, 2017
11 changes: 11 additions & 0 deletions features/cli.feature
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
Feature: `wp cli` tasks

Scenario: Ability to detect a WP-CLI registered command
Given an empty directory

When I run `wp package install wp-cli/scaffold-package-command`
When I run `wp cli has-command scaffold package`
Then the return code should be 0

When I run `wp package uninstall wp-cli/scaffold-package-command`
When I try `wp cli has-command scaffold package`
Then the return code should be 1

Scenario: Ability to set a custom version when building
Given an empty directory
And save the {SRC_DIR}/VERSION file as {TRUE_VERSION}
Expand Down
34 changes: 34 additions & 0 deletions php/commands/src/CLI_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -575,4 +575,38 @@ private function get_update_type_str( $assoc_args ) {
return $update_type;
}

/**
* Detects if a command exists
*
* This commands checks if a command is registered with WP-CLI.
* If the command is found then it returns with exit status 0.
* If the command doesn't exist, then it will exit with status 1.
*
* ## OPTIONS
* <command_name>...
* : The command
*
* ## EXAMPLES
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs another space after this heading.

*
* The "site delete" command is registered.
Copy link
Member

@schlessera schlessera Sep 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
 */

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

* $ wp cli has-command "site delete"
* $ echo $?
* 0
*
* The "foo bar" command is not registered.
* $ wp cli has-command "foo bar"
* $ echo $?
* 1
*
* @subcommand has-command
*
* @when after_wp_load
*/
public function has_command( $_, $assoc_args ) {

// If command is input as a string, then explode it into array.
$command = explode( ' ', implode( ' ', $_ ) );

WP_CLI::halt( is_array( WP_CLI::get_runner()->find_command_to_run( $command ) ) ? 0 : 1 );
}
}