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
'file' => false,
'synopsis' => '',
'default' => null,
+ 'multiple' => false,
);
foreach ( $this->spec as &$option ) {
* @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;
}
}
}
$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;
$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' ) );
// 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'] ) ) {