diff --git a/features/roles.feature b/features/roles.feature index 53ac7046ab..c4df0c9075 100644 --- a/features/roles.feature +++ b/features/roles.feature @@ -81,3 +81,9 @@ Feature: Manage WordPress roles delete_posts delete_published_posts """ + + When I run `wp cap list reporter --format=count` + Then STDOUT should be: + """ + 10 + """ diff --git a/php/commands/cap.php b/php/commands/cap.php index f5f99d55a8..0663f248e4 100644 --- a/php/commands/cap.php +++ b/php/commands/cap.php @@ -19,6 +19,10 @@ */ class Capabilities_Command extends WP_CLI_Command { + private $fields = array( + 'name' + ); + /** * List capabilities for a given role. * @@ -27,6 +31,19 @@ class Capabilities_Command extends WP_CLI_Command { * * : Key for the role. * + * [--format=] + * : Render output in a particular format. + * --- + * default: list + * options: + * - list + * - table + * - csv + * - json + * - count + * - yaml + * --- + * * ## EXAMPLES * * # Display alphabetical list of Contributor capabilities @@ -39,11 +56,26 @@ class Capabilities_Command extends WP_CLI_Command { * * @subcommand list */ - public function list_( $args ) { + public function list_( $args, $assoc_args ) { $role_obj = self::get_role( $args[0] ); - foreach ( array_keys( $role_obj->capabilities ) as $cap ) - WP_CLI::line( $cap ); + if ( 'list' === $assoc_args['format'] ) { + foreach ( array_keys( $role_obj->capabilities ) as $cap ) { + WP_CLI::line( $cap ); + } + } + else { + $output_caps = array(); + foreach ( array_keys( $role_obj->capabilities ) as $cap ) { + $output_cap = new stdClass; + + $output_cap->name = $cap; + + $output_caps[] = $output_cap; + } + $formatter = new \WP_CLI\Formatter( $assoc_args, $this->fields ); + $formatter->display_items( $output_caps ); + } } /**