WP_CLI

SynopsisParser::classify_token()private staticWP-CLI 1.0

Classify argument attributes based on its syntax.

Method of the class: SynopsisParser{}

No Hooks.

Return

Array.

Usage

$result = SynopsisParser::classify_token( $token );
$token(string) (required)
-

SynopsisParser::classify_token() code WP-CLI 2.8.0-alpha

private static function classify_token( $token ) {
	$param = [];

	list( $param['optional'], $token )  = self::is_optional( $token );
	list( $param['repeating'], $token ) = self::is_repeating( $token );

	$p_name  = '([a-z-_0-9]+)';
	$p_value = '([a-zA-Z-_|,0-9]+)';

	if ( '--<field>=<value>' === $token ) {
		$param['type'] = 'generic';
	} elseif ( preg_match( "/^<($p_value)>$/", $token, $matches ) ) {
		$param['type'] = 'positional';
		$param['name'] = $matches[1];
	} elseif ( preg_match( "/^--(?:\\[no-\\])?$p_name/", $token, $matches ) ) {
		$param['name'] = $matches[1];

		$value = substr( $token, strlen( $matches[0] ) );

		// substr returns false <= PHP 5.6, and '' PHP 7+
		if ( false === $value || '' === $value ) {
			$param['type'] = 'flag';
		} else {
			$param['type'] = 'assoc';

			list( $param['value']['optional'], $value ) = self::is_optional( $value );

			if ( preg_match( "/^=<$p_value>$/", $value, $matches ) ) {
				$param['value']['name'] = $matches[1];
			} else {
				$param = [
					'type' => 'unknown',
				];
			}
		}
	} else {
		$param['type'] = 'unknown';
	}

	return $param;
}