OSDN Git Service

support passing --require multiple times
authorscribu <mail@scribu.net>
Wed, 12 Jun 2013 03:39:01 +0000 (06:39 +0300)
committerscribu <mail@scribu.net>
Wed, 12 Jun 2013 04:44:33 +0000 (07:44 +0300)
features/flags.feature
php/WP_CLI/Configurator.php
php/WP_CLI/Runner.php
php/config-spec.php

index 95a201a..e99f959 100644 (file)
@@ -61,23 +61,53 @@ Feature: Global flags
   Scenario: Using --require
     Given a WP install
     And a custom-cmd.php file:
-    """
-    <?php
-    class Test_Command extends WP_CLI_Command {
+      """
+      <?php
+      class Test_Command extends WP_CLI_Command {
 
-      function req( $args, $assoc_args ) {
-        WP_CLI::line( $args[0] );
+        function req( $args, $assoc_args ) {
+          WP_CLI::line( $args[0] );
+        }
       }
-    }
 
-    WP_CLI::add_command( 'test', 'Test_Command' );
-    """
+      WP_CLI::add_command( 'test', 'Test_Command' );
+      """
+
+    And a foo.php file:
+      """
+      <?php echo basename(__FILE__) . "\n";
+      """
+
+    And a bar.php file:
+      """
+      <?php echo basename(__FILE__) . "\n";
+      """
+
+    And a wp-cli.yml file:
+      """
+      require:
+        - foo.php
+        - bar.php
+      """
+
+    And a wp-cli2.yml file:
+      """
+      require: custom-cmd.php
+      """
 
     When I run `wp --require=custom-cmd.php test req 'This is a custom command.'`
     Then STDOUT should be:
-    """
-    This is a custom command.
-    """
+      """
+      foo.php
+      bar.php
+      This is a custom command.
+      """
+
+    When I run `wp --config=wp-cli2.yml test req 'This is a custom command.'`
+    Then STDOUT should contain:
+      """
+      This is a custom command.
+      """
 
   Scenario: Enabling/disabling color
     Given a WP install
index 47e919f..fba6f75 100644 (file)
@@ -14,6 +14,7 @@ class Configurator {
                        'file' => false,
                        'synopsis' => '',
                        'default' => null,
+                       'multiple' => false,
                );
 
                foreach ( $this->spec as &$option ) {
@@ -37,28 +38,34 @@ class Configurator {
         * @return array
         */
        function parse_args( $arguments ) {
-               $regular_args = array();
-               $assoc_args = array();
+               $regular_args = $mixed_args = array();
 
                foreach ( $arguments as $arg ) {
                        if ( preg_match( '|^--([^=]+)$|', $arg, $matches ) ) {
-                               $assoc_args[ $matches[1] ] = true;
+                               $mixed_args[] = array( $matches[1], true );
                        } elseif ( preg_match( '|^--([^=]+)=(.+)|', $arg, $matches ) ) {
-                               $assoc_args[ $matches[1] ] = $matches[2];
+                               $mixed_args[] = array( $matches[1], $matches[2] );
                        } else {
                                $regular_args[] = $arg;
                        }
                }
 
-               $runtime_config = array();
+               $assoc_args = $runtime_config = array();
 
-               foreach ( $this->spec as $key => $details ) {
-                       if ( true === $details['runtime'] ) {
-                               self::handle_boolean_param( $assoc_args, $runtime_config, $key );
-                       } elseif ( false !== $details['runtime'] ) {
-                               if ( isset( $assoc_args[ $key ] ) ) {
-                                       $runtime_config[ $key ] = $assoc_args[ $key ];
-                                       unset( $assoc_args[ $key ] );
+               foreach ( $mixed_args as $tmp ) {
+                       list( $key, $value ) = $tmp;
+
+                       $enabled = isset( $this->spec[ $key ] ) ? $this->spec[ $key ]['runtime'] : false;
+
+                       if ( false === $enabled ) {
+                               $assoc_args[ $key ] = $value;
+                       } elseif ( true === $enabled ) {
+                               self::handle_boolean_param( $mixed_args, $runtime_config, $key );
+                       } else {
+                               if ( $this->spec[ $key ]['multiple'] ) {
+                                       $runtime_config[ $key ][] = $value;
+                               } else {
+                                       $runtime_config[ $key ] = $value;
                                }
                        }
                }
@@ -95,16 +102,16 @@ class Configurator {
                $sanitized_config = array();
 
                foreach ( $this->spec as $key => $details ) {
-                       if ( $details['file'] && isset( $config[ $key ] ) )
-                               $sanitized_config[ $key ] = $config[ $key ];
-                       else
-                               $sanitized_config[ $key ] = $details['default'];
-               }
+                       if ( $details['file'] && isset( $config[ $key ] ) ) {
+                               $value = $config[ $key ];
+                               if ( $details['multiple'] && !is_array( $value ) ) {
+                                       $value = array( $value );
+                               }
+                       } else {
+                                $value = $details['default'];
+                       }
 
-               // When invoking from a subdirectory in the project,
-               // make sure a config-relative 'path' is made absolute
-               if ( ! empty( $sanitized_config['path'] ) && ! \WP_CLI\Utils\is_path_absolute( $sanitized_config['path'] ) ) {
-                       $sanitized_config['path'] = dirname( $path ) . DIRECTORY_SEPARATOR . $sanitized_config['path'];
+                       $sanitized_config[ $key ] = $value;
                }
 
                return $sanitized_config;
index c33edc7..13def08 100644 (file)
@@ -236,7 +236,21 @@ class Runner {
 
                $local_config = \WP_CLI::$configurator->load_config( $this->config_path );
 
-               $this->config = array_merge( $local_config, $runtime_config );
+               // When invoking from a subdirectory in the project,
+               // make sure a config-relative 'path' is made absolute
+               if ( !empty( $local_config['path'] ) && !\WP_CLI\Utils\is_path_absolute( $local_config['path'] ) ) {
+                       $local_config['path'] = dirname( $this->config_path ) . DIRECTORY_SEPARATOR . $local_config['path'];
+               }
+
+               $this->config = $local_config;
+
+               foreach ( $runtime_config as $key => $value ) {
+                       if ( isset( $this->config[ $key ] ) && is_array( $this->config[ $key ] ) ) {
+                               $this->config[ $key ] = array_merge( $this->config[ $key ], $value );
+                       } else {
+                               $this->config[ $key ] = $value;
+                       }
+               }
 
                if ( !isset( $this->config['path'] ) ) {
                        $this->config['path'] = dirname( Utils\find_file_upward( 'wp-load.php' ) );
@@ -307,8 +321,11 @@ class Runner {
                // Handle --user parameter
                self::set_user( $this->config );
 
-               if ( isset( $this->config['require'] ) )
-                       require $this->config['require'];
+               if ( isset( $this->config['require'] ) ) {
+                       foreach ( $this->config['require'] as $path ) {
+                               require $path;
+                       }
+               }
 
                // Handle --completions parameter
                if ( isset( $this->assoc_args['completions'] ) ) {
index 41332d9..a711db4 100644 (file)
@@ -33,6 +33,8 @@ return array(
                'runtime' => '=<path>',
                'file' => '<path>',
                'desc' => 'Load given PHP file before running the command',
+               'multiple' => true,
+               'default' => array(),
        ),
 
        'disabled_commands' => array(