From 4a4f3805bdff96b63ecc769948fd31699768bdda Mon Sep 17 00:00:00 2001 From: scribu Date: Mon, 11 Feb 2013 20:53:55 +0200 Subject: [PATCH] introduce SynopsisParser class --- php/WP_CLI/Dispatcher/Subcommand.php | 56 +----------------------------- php/WP_CLI/SynopsisParser.php | 66 ++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 55 deletions(-) create mode 100644 php/WP_CLI/SynopsisParser.php diff --git a/php/WP_CLI/Dispatcher/Subcommand.php b/php/WP_CLI/Dispatcher/Subcommand.php index e725199d..47ebb6f2 100644 --- a/php/WP_CLI/Dispatcher/Subcommand.php +++ b/php/WP_CLI/Dispatcher/Subcommand.php @@ -51,7 +51,7 @@ class Subcommand implements Command, AtomicCommand, Documentable { if ( !$synopsis ) return; - $accepted_params = $this->parse_synopsis( $synopsis ); + $accepted_params = \WP_CLI\SynopsisParser::parse( $synopsis ); $this->check_positional( $args, $accepted_params ); @@ -119,59 +119,5 @@ class Subcommand implements Command, AtomicCommand, Documentable { } } - protected function parse_synopsis( $synopsis ) { - list( $patterns, $params ) = self::get_patterns(); - - $tokens = preg_split( '/[\s\t]+/', $synopsis ); - - foreach ( $tokens as $token ) { - foreach ( $patterns as $regex => $desc ) { - if ( preg_match( $regex, $token, $matches ) ) { - $type = $desc['type']; - $params[$type][] = array_merge( $matches, $desc ); - break; - } - } - } - - return $params; - } - - private static function get_patterns() { - $p_name = '(?P[a-z-_]+)'; - $p_value = '(?P[a-z-|]+)'; - - $param_types = array( - array( 'positional', "<$p_value>", 1, 1 ), - array( 'generic', "--=", 1, 1 ), - array( 'assoc', "--$p_name=<$p_value>", 1, 1 ), - array( 'flag', "--$p_name", 1, 0 ), - ); - - $patterns = array(); - $params = array(); - - foreach ( $param_types as $pt ) { - list( $type, $pattern, $optional, $mandatory ) = $pt; - - if ( $mandatory ) { - $patterns[ "/^$pattern$/" ] = array( - 'type' => $type, - 'optional' => false - ); - } - - if ( $optional ) { - $patterns[ "/^\[$pattern\]$/" ] = array( - 'type' => $type, - 'optional' => true - ); - } - - $params[ $type ] = array(); - } - - return array( $patterns, $params ); - } } diff --git a/php/WP_CLI/SynopsisParser.php b/php/WP_CLI/SynopsisParser.php new file mode 100644 index 00000000..bc594df3 --- /dev/null +++ b/php/WP_CLI/SynopsisParser.php @@ -0,0 +1,66 @@ + $desc ) { + if ( preg_match( $regex, $token, $matches ) ) { + $type = $desc['type']; + $params[$type][] = array_merge( $matches, $desc ); + break; + } + } + } + + return $params; + } + + private static function get_patterns() { + $p_name = '(?P[a-z-_]+)'; + $p_value = '(?P[a-z-|]+)'; + + $param_types = array( + array( 'positional', "<$p_value>", 1, 1 ), + array( 'generic', "--=", 1, 1 ), + array( 'assoc', "--$p_name=<$p_value>", 1, 1 ), + array( 'flag', "--$p_name", 1, 0 ), + ); + + $patterns = array(); + $params = array(); + + foreach ( $param_types as $pt ) { + list( $type, $pattern, $optional, $mandatory ) = $pt; + + if ( $mandatory ) { + $patterns[ "/^$pattern$/" ] = array( + 'type' => $type, + 'optional' => false + ); + } + + if ( $optional ) { + $patterns[ "/^\[$pattern\]$/" ] = array( + 'type' => $type, + 'optional' => true + ); + } + + $params[ $type ] = array(); + } + + return array( $patterns, $params ); + } +} + -- 2.11.0