WP_CLI
Completions::get_command
Get the specific WP-CLI command that is being referenced.
Method of the class: Completions{}
No Hooks.
Returns
Array|Mixed. Array with command and arguments, or error result if command detection failed.
Usage
// private - for code of main (parent) class only $result = $this->get_command( $words );
- $words(array) (required)
- Individual input line words.
Completions::get_command() Completions::get command code WP-CLI 2.13.0-alpha
private function get_command( $words ) {
$positional_args = [];
$assoc_args = [];
# Avoid having to polyfill array_key_last().
end( $words );
$last_arg_i = key( $words );
foreach ( $words as $i => $arg ) {
if ( preg_match( '|^--([^=]+)(=?)|', $arg, $matches ) ) {
if ( $i === $last_arg_i && '' === $matches[2] ) {
continue;
}
$assoc_args[ $matches[1] ] = true;
} else {
$positional_args[] = $arg;
}
}
$r = WP_CLI::get_runner()->find_command_to_run( $positional_args );
if ( ! is_array( $r ) && array_pop( $positional_args ) === $this->cur_word ) {
$r = WP_CLI::get_runner()->find_command_to_run( $positional_args );
}
if ( ! is_array( $r ) ) {
return $r;
}
list( $command, $args ) = $r;
return [ $command, $args, $assoc_args ];
}