OSDN Git Service

Allow formatting for search results.
authorjtsternberg <me@jtsternberg.com>
Mon, 5 Aug 2013 15:49:46 +0000 (11:49 -0400)
committerjtsternberg <me@jtsternberg.com>
Mon, 5 Aug 2013 15:49:46 +0000 (11:49 -0400)
php/WP_CLI/CommandWithUpgrade.php
php/commands/plugin.php

index 7b0b9c1..fb0867c 100644 (file)
@@ -312,11 +312,12 @@ abstract class CommandWithUpgrade extends \WP_CLI_Command {
        /**
         * Search wordpress.org plugin repo
         *
-        * @param  object $api       data from WP plugin/theme API
-        * @param  array  $fields    Data fields to display in table.
-        * @param  string $data_type Plugin or Theme api endpoint
+        * @param  object $api        Data from WP plugin/theme API
+        * @param  array  $fields     Data fields to display in table.
+        * @param  array  $assoc_args Data passed in from command.
+        * @param  string $data_type  Plugin or Theme api endpoint
         */
-       public function _search( $api, $fields, $data_type = 'plugin' ) {
+       public function _search( $api, $fields, $assoc_args, $data_type = 'plugin' ) {
 
                // Sanitize to 1 of 2 types
                $data_type = 'plugin' === $data_type ? 'plugin' : 'theme';
@@ -339,7 +340,9 @@ abstract class CommandWithUpgrade extends \WP_CLI_Command {
 
                $set = set_site_transient( 'wpcli-$data_type-search-data', $data, 60*60 );
 
-               \WP_CLI\Utils\format_items( 'table', $data, array_merge( array( 'key' ), $fields ) );
+               $format = isset( $assoc_args['format'] ) ? $assoc_args['format'] : 'table';
+
+               \WP_CLI\Utils\format_items( $format, $data, array_merge( array( 'key' ), $fields ) );
 
        }
 
index b7e269a..018e6ab 100644 (file)
@@ -54,6 +54,9 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
         * --per-page
         * : Optional number of results to display. Defaults to 10.
         *
+        * --format
+        * : Output list as table, CSV or JSON. Defaults to table.
+        *
         * --fields
         * : Ask for specific fields from the API. Defaults to name,slug,author_profile,rating. acceptable values:
         *
@@ -74,11 +77,11 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
         *
         * ## EXAMPLES
         *
-        *     wp plugin search dsgnwrks --per-page=20
+        *     wp plugin search dsgnwrks --per-page=20 --format=json
         *
         *     wp plugin search dsgnwrks --fields=name,version,slug,rating,num_ratings
         *
-        * @synopsis <plugin> [--per-page=<per-page>] [--fields=<fields>]
+        * @synopsis <plugin> [--per-page=<per-page>] [--fields=<fields>] [--format=<format>]
         */
        public function search( $args, $assoc_args = array() ) {
                $term = $args[0];
@@ -90,8 +93,7 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
                        'search' => $term,
                ) );
 
-               parent::_search( $api, $fields, 'plugin' );
-
+               parent::_search( $api, $fields, $assoc_args, 'plugin' );
        }
 
        protected function status_single( $args ) {