WP_CLI
Runner::find_command_to_run
Given positional arguments, find the command to execute.
Method of the class: Runner{}
No Hooks.
Returns
Array|String. Command, args, and path on success; error message on failure
Usage
$Runner = new Runner(); $Runner->find_command_to_run( $args );
- $args(array) (required)
- .
Runner::find_command_to_run() Runner::find command to run code WP-CLI 2.13.0-alpha
public function find_command_to_run( $args ) {
$command = WP_CLI::get_root_command();
WP_CLI::do_hook( 'find_command_to_run_pre' );
$cmd_path = [];
while ( ! empty( $args ) && $command->can_have_subcommands() ) {
$cmd_path[] = $args[0];
$full_name = implode( ' ', $cmd_path );
$subcommand = $command->find_subcommand( $args );
if ( ! $subcommand ) {
if ( count( $cmd_path ) > 1 ) {
$child = array_pop( $cmd_path );
$parent_name = implode( ' ', $cmd_path );
$suggestion = $this->get_subcommand_suggestion( $child, $command );
if ( 'network' === $parent_name && 'option' === $child ) {
$suggestion = 'meta';
}
return sprintf(
"'%s' is not a registered subcommand of '%s'. See 'wp help %s' for available subcommands.%s",
$child,
$parent_name,
$parent_name,
! empty( $suggestion ) ? PHP_EOL . "Did you mean '{$suggestion}'?" : ''
);
}
$suggestion = $this->get_subcommand_suggestion( $full_name, $command );
// If the functions are available, it means WordPress is available
// and has already been loaded.
if ( function_exists( '\taxonomy_exists' ) ) {
if ( \taxonomy_exists( $cmd_path[0] ) ) {
$suggestion = 'wp term <command>';
} elseif ( \post_type_exists( $cmd_path[0] ) ) {
$suggestion = "wp post --post_type={$cmd_path[0]} <command>";
}
}
return sprintf(
"'%s' is not a registered wp command. See 'wp help' for available commands.%s",
$full_name,
! empty( $suggestion ) ? PHP_EOL . "Did you mean '{$suggestion}'?" : ''
);
}
if ( $this->is_command_disabled( $subcommand ) ) {
return sprintf(
"The '%s' command has been disabled from the config file.",
$full_name
);
}
$command = $subcommand;
}
return [ $command, $args, $cmd_path ];
}