OSDN Git Service

introduce SynopsisParser class
authorscribu <mail@scribu.net>
Mon, 11 Feb 2013 18:53:55 +0000 (20:53 +0200)
committerscribu <mail@scribu.net>
Mon, 11 Feb 2013 19:23:56 +0000 (21:23 +0200)
php/WP_CLI/Dispatcher/Subcommand.php
php/WP_CLI/SynopsisParser.php [new file with mode: 0644]

index e725199..47ebb6f 100644 (file)
@@ -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<name>[a-z-_]+)';
-               $p_value = '(?P<value>[a-z-|]+)';
-
-               $param_types = array(
-                       array( 'positional', "<$p_value>",           1, 1 ),
-                       array( 'generic',    "--<field>=<value>",    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 (file)
index 0000000..bc594df
--- /dev/null
@@ -0,0 +1,66 @@
+<?php
+
+namespace WP_CLI;
+
+class SynopsisParser {
+
+       /**
+        * @param string
+        * @return array List of parameters
+        */
+       static function parse( $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<name>[a-z-_]+)';
+               $p_value = '(?P<value>[a-z-|]+)';
+
+               $param_types = array(
+                       array( 'positional', "<$p_value>",           1, 1 ),
+                       array( 'generic',    "--<field>=<value>",    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 );
+       }
+}
+