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
33 changes: 33 additions & 0 deletions php/commands/src/CLI_Command.php
Expand Up @@ -575,4 +575,37 @@ 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.

* wp cli has-command "wp site delete"
* wp cli has-command wp site delete
Copy link
Member

Choose a reason for hiding this comment

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

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

*
* @subcommand has-command
*
* @when after_wp_load
*
* @param array $_ Array of positional arguments.
* @param array $assoc_args Array of associative arguments.
Copy link
Member

Choose a reason for hiding this comment

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

We should remove this argument definition, as it's not a pattern we have elsewhere.

*/
public function has_command( $_, $assoc_args ) {

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

if ( 'wp' === $command[0] ) {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should accept "wp" to this command.

array_shift( $command );
}

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