WP_CLI\Dispatcher

Subcommand::invoke()publicWP-CLI 1.0

Invoke the subcommand with the supplied arguments. Given a --prompt argument, interactively request input from the end user.

Method of the class: Subcommand{}

No Hooks.

Return

null. Nothing.

Usage

$Subcommand = new Subcommand();
$Subcommand->invoke( $args, $assoc_args, $extra_args );
$args(array) (required)
-
$assoc_args(array) (required)
-
$extra_args (required)
-

Subcommand::invoke() code WP-CLI 2.8.0-alpha

public function invoke( $args, $assoc_args, $extra_args ) {
	static $prompted_once = false;

	if ( 'help' !== $this->name ) {
		if ( \WP_CLI::get_config( 'prompt' ) && ! $prompted_once ) {
			list( $_args, $assoc_args ) = $this->prompt_args( $args, $assoc_args );
			$args                       = array_merge( $args, $_args );
			$prompted_once              = true;
		}
	}

	$extra_positionals = [];
	foreach ( $extra_args as $k => $v ) {
		if ( is_numeric( $k ) ) {
			if ( ! isset( $args[ $k ] ) ) {
				$extra_positionals[ $k ] = $v;
			}
			unset( $extra_args[ $k ] );
		}
	}
	$args += $extra_positionals;

	list( $to_unset, $args, $assoc_args, $extra_args ) = $this->validate_args( $args, $assoc_args, $extra_args );

	foreach ( $to_unset as $key ) {
		unset( $assoc_args[ $key ] );
	}

	$path   = get_path( $this->get_parent() );
	$parent = implode( ' ', array_slice( $path, 1 ) );
	$cmd    = $this->name;
	if ( $parent ) {
		WP_CLI::do_hook( "before_invoke:{$parent}", $parent );
		$cmd = $parent . ' ' . $cmd;
	}
	WP_CLI::do_hook( "before_invoke:{$cmd}", $cmd );

	// Check if `--prompt` arg passed or not.
	if ( $prompted_once ) {
		// Unset empty args.
		$actual_args = $assoc_args;
		foreach ( $actual_args as $key ) {
			if ( empty( $actual_args[ $key ] ) ) {
				unset( $actual_args[ $key ] );
			}
		}

		WP_CLI::log(
			sprintf(
				'wp %s %s',
				$cmd,
				ltrim( Utils\assoc_args_to_str( $actual_args ), ' ' )
			)
		);
	}

	call_user_func( $this->when_invoked, $args, array_merge( $extra_args, $assoc_args ) );

	if ( $parent ) {
		WP_CLI::do_hook( "after_invoke:{$parent}", $parent );
	}
	WP_CLI::do_hook( "after_invoke:{$cmd}", $cmd );
}