WP_CLI\Dispatcher

Subcommand::prompt_args()privateWP-CLI 1.0

Interactively prompt the user for input based on defined synopsis and passed arguments.

Метод класса: Subcommand{}

Хуков нет.

Возвращает

Массив.

Использование

// private - только в коде основоного (родительского) класса
$result = $this->prompt_args( $args, $assoc_args );
$args(массив) (обязательный)
-
$assoc_args(массив) (обязательный)
-

Код Subcommand::prompt_args() WP-CLI 2.8.0-alpha

private function prompt_args( $args, $assoc_args ) {

	$synopsis = $this->get_synopsis();

	if ( ! $synopsis ) {
		return [ $args, $assoc_args ];
	}

	// To skip the already provided positional arguments, we need to count
	// how many we had already received.
	$arg_index = 0;

	$spec = array_filter(
		SynopsisParser::parse( $synopsis ),
		function( $spec_arg ) use ( $args, $assoc_args, &$arg_index ) {
			switch ( $spec_arg['type'] ) {
				case 'positional':
					// Only prompt for the positional arguments that are not
					// yet provided, based purely on number.
					return $arg_index++ >= count( $args );
				case 'generic':
					// Always prompt for generic arguments.
					return true;
				case 'assoc':
				case 'flag':
				default:
					// Prompt for the specific flags that were not provided
					// yet, based on name.
					return ! isset( $assoc_args[ $spec_arg['name'] ] );
			}
		}
	);

	$spec = array_values( $spec );

	$prompt_args = WP_CLI::get_config( 'prompt' );
	if ( true !== $prompt_args ) {
		$prompt_args = explode( ',', $prompt_args );
	}

	// 'positional' arguments are positional (aka zero-indexed)
	// so $args needs to be reset before prompting for new arguments
	$args = [];

	foreach ( $spec as $key => $spec_arg ) {

		// When prompting for specific arguments (e.g. --prompt=user_pass),
		// ignore all arguments that don't match.
		if ( is_array( $prompt_args ) ) {
			if ( 'assoc' !== $spec_arg['type'] ) {
				continue;
			}
			if ( ! in_array( $spec_arg['name'], $prompt_args, true ) ) {
				continue;
			}
		}

		$current_prompt = ( $key + 1 ) . '/' . count( $spec ) . ' ';
		$default        = $spec_arg['optional'] ? '' : false;

		// 'generic' permits arbitrary key=value (e.g. [--<field>=<value>] )
		if ( 'generic' === $spec_arg['type'] ) {

			list( $key_token, $value_token ) = explode( '=', $spec_arg['token'] );

			$repeat = false;
			do {
				if ( ! $repeat ) {
					$key_prompt = $current_prompt . $key_token;
				} else {
					$key_prompt = str_repeat( ' ', strlen( $current_prompt ) ) . $key_token;
				}

				$key = $this->prompt( $key_prompt, $default );
				if ( false === $key ) {
					return [ $args, $assoc_args ];
				}

				if ( $key ) {
					$key_prompt_count = strlen( $key_prompt ) - strlen( $value_token ) - 1;
					$value_prompt     = str_repeat( ' ', $key_prompt_count ) . '=' . $value_token;

					$value = $this->prompt( $value_prompt, $default );
					if ( false === $value ) {
						return [ $args, $assoc_args ];
					}

					$assoc_args[ $key ] = $value;

					$repeat = true;
				} else {
					$repeat = false;
				}
			} while ( $repeat );

		} else {
			$prompt = $current_prompt . $spec_arg['token'];
			if ( 'flag' === $spec_arg['type'] ) {
				$prompt .= ' (Y/n)';
			}

			$response = $this->prompt( $prompt, $default );
			if ( false === $response ) {
				return [ $args, $assoc_args ];
			}

			if ( $response ) {
				switch ( $spec_arg['type'] ) {
					case 'positional':
						if ( $spec_arg['repeating'] ) {
							$response = explode( ' ', $response );
						} else {
							$response = [ $response ];
						}
						$args = array_merge( $args, $response );
						break;
					case 'assoc':
						$assoc_args[ $spec_arg['name'] ] = $response;
						break;
					case 'flag':
						if ( 'Y' === strtoupper( $response ) ) {
							$assoc_args[ $spec_arg['name'] ] = true;
						}
						break;
				}
			}
		}
	}

	return [ $args, $assoc_args ];
}