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
13 changes: 13 additions & 0 deletions features/cli.feature
@@ -1,5 +1,18 @@
Feature: `wp cli` tasks

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"
Copy link
Member

Choose a reason for hiding this comment

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

Why do you need to generate a new Phar here?

Copy link
Member Author

Choose a reason for hiding this comment

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

You're correct, there wasn't a need to generate a phar. I replaced it with wp @danielbachhuber


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`
Copy link
Member

Choose a reason for hiding this comment

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

Use a short package identifier here:
When I run {PHAR_PATH} package install wp-cli/scaffold-package-command`

Copy link
Member Author

Choose a reason for hiding this comment

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

@schlessera, Why didn't git clone work?

Copy link
Member

Choose a reason for hiding this comment

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

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

I see 👀

When I run `{PHAR_PATH} cli has-command scaffold package`
Then the return code should be 0


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

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 );
}
}