OSDN Git Service

Added the `--feature` option for `post get`.
authorJohn Lamp <j3lamp@gmail.com>
Sat, 13 Apr 2013 01:21:33 +0000 (21:21 -0400)
committerJohn Lamp <j3lamp@gmail.com>
Sat, 13 Apr 2013 01:21:33 +0000 (21:21 -0400)
features/post.feature
features/steps/basic_steps.php
man-src/post-get.txt
man/post-get.1
php/commands/post.php

index 8746090..74f6cd8 100644 (file)
@@ -42,3 +42,37 @@ Feature: Manage WordPress posts
       """
       Test content.
       """
+
+    When I run `wp post get --format=content {POST_ID}`
+    Then it should run without errors
+    And STDOUT should be:
+      """
+      Test content.
+      """
+
+    When I run `wp post get --format=table {POST_ID}`
+    Then it should run without errors
+    And STDOUT should be a table containing rows:
+      """
+      Field    Value
+      ID       {POST_ID}
+      post_title       Test post
+      post_content     Test content.
+      """
+
+    When I run `wp post get --format=csv {POST_ID}`
+    Then it should run without errors
+    And STDOUT should be a table containing rows:
+      """
+      Field,Value
+      ID,{POST_ID}
+      post_title,"Test post"
+      post_content,"Test content."
+      """
+
+    When I run `wp post get --format=json {POST_ID}`
+    Then it should run without errors
+    And STDOUT should be JSON containing:
+      """
+      {"ID":{POST_ID},"post_title":"Test post","post_content":"Test content."}
+      """
index 352bdb7..d6eb182 100644 (file)
@@ -178,6 +178,41 @@ $steps->Then( '/^(STDOUT|STDERR) should match \'([^\']+)\'$/',
        }
 );
 
+$steps->Then( '/^STDOUT should be a table containing rows:$/',
+       function ( $world, PyStringNode $expected ) {
+               $output     = $world->result->STDOUT;
+               $outputRows = explode( "\n", rtrim( $output, "\n" ) );
+
+               $expected     = $world->replace_variables( (string) $expected );
+               $expectedRows = explode( "\n", rtrim( $expected, "\n" ) );
+
+               // the first row is the header and must be present
+               if ( $expectedRows[0] != $outputRows[0] ) {
+                       throw new \Exception( $output );
+               }
+
+               unset($outputRows[0]);
+               unset($expectedRows[0]);
+               $matches = array_intersect( $expectedRows, $outputRows );
+               if ( count( $expectedRows ) != count( $matches ) ) {
+                       throw new \Exception( $output );
+               }
+       }
+);
+
+$steps->Then( '/^STDOUT should be JSON containing:$/',
+       function ( $world, PyStringNode $expected ) {
+               $output     = $world->result->STDOUT;
+               $outputJson = json_decode( $output );
+
+               $expected     = $world->replace_variables( (string) $expected );
+               $expectedJson = json_decode( $expected );
+
+               if ( !compareJson( $expectedJson, $outputJson ) ) {
+                       throw new \Exception( $output );
+               }
+});
+
 $steps->Then( '/^(STDOUT|STDERR) should be empty$/',
        function ( $world, $stream ) {
                if ( !empty( $world->result->$stream ) ) {
@@ -197,3 +232,28 @@ $steps->Then( '/^the (.+) file should exist$/',
                assertFileExists( $world->get_path( $path ) );
        }
 );
+
+
+function compareJson( $expected, $actual ) {
+       if ( gettype( $expected ) != gettype( $actual ) ) {
+               return false;
+       }
+
+       if ( is_object( $expected ) ) {
+               foreach ( get_object_vars( $expected ) as $name => $value ) {
+                       if ( !compareJson( $value, $actual->$name ) ) {
+                               return false;
+                       }
+               }
+       } else if ( is_array( $expected ) ) {
+               foreach ( $expected as $key => $value ) {
+                       if ( !compareJson( $value, $actual[$key] ) ) {
+                               return false;
+                       }
+               }
+       } else {
+               return $expected === $actual;
+       }
+
+       return true;
+}
index 38d10df..ff20d1d 100644 (file)
@@ -1,5 +1,18 @@
 ## OPTIONS
 
+* `[--format=<format>]`:
+
+       The format to use when printing the post, acceptable values:
+
+       content: outputs only the post's content (default)
+
+       table: output all fields of the post as a table, note that the
+       post_content field is output last to simplify parsing
+
+       csv: output all fields in Comma Separated Value format
+
+       json: output all fields in JavaScript Object Notation format
+
 * `<id>`:
 
        The ID of the post to get.
index 7666551..1a44a82 100644 (file)
@@ -7,11 +7,29 @@
 \fBwp\-post\-get\fR \- Get a post\'s content by ID\.
 .
 .SH "SYNOPSIS"
-wp post get \fIid\fR
+wp post get [\-\-format=\fIformat\fR] \fIid\fR
 .
 .SH "OPTIONS"
 .
 .TP
+\fB[\-\-format=<format>]\fR:
+.
+.IP
+The format to use when printing the post, acceptable values:
+.
+.IP
+content: outputs only the post\'s content (default)
+.
+.IP
+table: output all fields of the post as a table, note that the post_content field is output last to simplify parsing
+.
+.IP
+csv: output all fields in Comma Separated Value format
+.
+.IP
+json: output all fields in JavaScript Object Notation format
+.
+.TP
 \fB<id>\fR:
 .
 .IP
index e2bd934..b84d193 100644 (file)
@@ -65,14 +65,14 @@ class Post_Command extends \WP_CLI\CommandWithDBObject {
        public function edit( $args, $_ ) {
                $post_id = $args[0];
                if ( !$post_id || !$post = get_post( $post_id ) )
-                        \WP_CLI::error( "Failed opening post $post_id to edit." );
+                       \WP_CLI::error( "Failed opening post $post_id to edit." );
 
                $r = $this->_edit( $post->post_content, "WP-CLI post $post_id" );
 
                if ( $r === false )
-                        \WP_CLI::warning( 'No change made to post content.', 'Aborted' );
+                       \WP_CLI::warning( 'No change made to post content.', 'Aborted' );
                else
-                        parent::update( $args, array( 'post_content' => $r ) );
+                       parent::update( $args, array( 'post_content' => $r ) );
        }
 
        protected function _edit( $content, $title ) {
@@ -80,16 +80,53 @@ class Post_Command extends \WP_CLI\CommandWithDBObject {
        }
 
        /**
-        * Get a post's content by ID.
-        *
-        * @synopsis <id>
-        */
-       public function get( $args, $_ ) {
+        * Get a post's content by ID.
+        *
+        * @synopsis [--format=<format>] <id>
+        */
+       public function get( $args, $assoc_args ) {
+               $assoc_args = wp_parse_args( $assoc_args, array(
+                       'format' => 'content'
+               ) );
+               $format = $assoc_args['format'];
+
                $post_id = $args[0];
                if ( !$post_id || !$post = get_post( $post_id ) )
-                        \WP_CLI::error( "Failed opening post $post_id to get." );
+                       \WP_CLI::error( "Failed opening post $post_id to get." );
+
+               switch ( $assoc_args['format'] ) {
+
+               case 'content':
+                       echo($post->post_content);
+                       break;
 
-        echo($post->post_content);
+               case 'table':
+               case 'csv':
+                       $items = array();
+                       foreach ( get_object_vars( $post ) as $field => $value ) {
+                               if ( 'post_content' === $field )
+                                       continue;
+
+                               $item = new \stdClass;
+                               $item->Field = $field;
+                               $item->Value = $value;
+                               $items[] = $item;
+                       }
+                       $content = new \stdClass;
+                       $content->Field = 'post_content';
+                       $content->Value = $post->post_content;
+                       $items[] = $content;
+
+                       \WP_CLI\Utils\format_items( $format, array( 'Field', 'Value' ), $items );
+                       break;
+
+               case 'json':
+                       echo( json_encode( $post ) );
+                       break;
+
+               default:
+                       \WP_CLI::error( "Invalid value for format: " . $format );
+               }
        }
 
        /**