From 5aaaa1ab8dd6cf678514e38c8bf8f6234cf0b691 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Fri, 12 Apr 2013 10:06:16 -0700 Subject: [PATCH] Support for `--format` when using `wp post list` --- man-src/post-list.txt | 6 ++++++ man/post-list.1 | 10 +++++++++- php/commands/post.php | 26 +++++++++++++++----------- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/man-src/post-list.txt b/man-src/post-list.txt index 295f102c..ad678636 100644 --- a/man-src/post-list.txt +++ b/man-src/post-list.txt @@ -8,8 +8,14 @@ Return only the IDs of the found posts, separated by spaces. +* `--format`=: + + Output list as table, CSV or JSON. Defaults to table. + ## EXAMPLES wp post list wp post list --post_type=page --post_status=draft --ids + + wp post list --post_type=post --posts_per_page=5 --format=json diff --git a/man/post-list.1 b/man/post-list.1 index 299c62aa..85a82be0 100644 --- a/man/post-list.1 +++ b/man/post-list.1 @@ -7,7 +7,7 @@ \fBwp\-post\-list\fR \- Get a list of posts\. . .SH "SYNOPSIS" -wp post list [\-\-\fIfield\fR=\fIvalue\fR] [\-\-ids] +wp post list [\-\-\fIfield\fR=\fIvalue\fR] [\-\-ids] [\-\-format=\fIformat\fR] . .SH "OPTIONS" . @@ -23,6 +23,12 @@ One or more args to pass to WP_Query\. .IP Return only the IDs of the found posts, separated by spaces\. . +.TP +\fB\-\-format\fR=\fIformat\fR: +. +.IP +Output list as table, CSV or JSON\. Defaults to table\. +. .SH "EXAMPLES" . .nf @@ -30,6 +36,8 @@ Return only the IDs of the found posts, separated by spaces\. wp post list wp post list \-\-post_type=page \-\-post_status=draft \-\-ids + +wp post list \-\-post_type=post \-\-posts_per_page=5 \-\-format=json . .fi diff --git a/php/commands/post.php b/php/commands/post.php index 028afbce..645df962 100644 --- a/php/commands/post.php +++ b/php/commands/post.php @@ -108,13 +108,20 @@ class Post_Command extends \WP_CLI\CommandWithDBObject { * Get a list of posts. * * @subcommand list - * @synopsis [--=] [--ids] + * @synopsis [--=] [--ids] [--format=] */ public function _list( $_, $assoc_args ) { $query_args = array( 'posts_per_page' => -1 ); + if ( ! empty( $assoc_args['format'] ) ) { + $format = $assoc_args['format']; + unset( $assoc_args['format'] ); + } else { + $format = 'table'; + } + foreach ( $assoc_args as $key => $value ) { if ( true === $value ) continue; @@ -130,24 +137,21 @@ class Post_Command extends \WP_CLI\CommandWithDBObject { if ( isset( $assoc_args['ids'] ) ) { WP_CLI::out( implode( ' ', $query->posts ) ); } else { - $fields = array( 'ID', 'post_title', 'post_name', 'post_date' ); - - $table = new \cli\Table(); - $table->setHeaders( $fields ); + $fields = array( 'ID', 'post_title', 'post_name', 'post_date' ); + $output_posts = array(); foreach ( $query->posts as $post ) { - $line = array(); + $output_post = new stdClass; foreach ( $fields as $field ) { - $line[] = $post->$field; + $output_post->$field = $post->$field; } - - $table->addRow( $line ); + $output_posts[] = $output_post; } - - $table->display(); + WP_CLI\Utils\format_items( $format, $fields, $output_posts ); } + } /** -- 2.11.0