OSDN Git Service

Improvements to `wp term` output
authorDaniel Bachhuber <d@danielbachhuber.com>
Sun, 21 Apr 2013 04:27:00 +0000 (21:27 -0700)
committerDaniel Bachhuber <d@danielbachhuber.com>
Sun, 21 Apr 2013 04:27:00 +0000 (21:27 -0700)
* Add `--porcelain` argument to `wp term create`
* `wp term create` should include term ID in success message
* `wp term delete` includes the term ID of the deleted term
* Updated tests / doc

See https://github.com/wp-cli/wp-cli/issues/399#issuecomment-16685216

features/term.feature
man-src/term-create.txt
man/term-create.1
php/commands/term.php

index e8be165..47a4219 100644 (file)
@@ -3,12 +3,9 @@ Feature: Manage WordPress terms
   Scenario: Creating/listing a term
     Given a WP install
 
-    When I run `wp term create 'Test term' post_tag --slug=test --description='This is a test term'`
+    When I run `wp term create 'Test term' post_tag --slug=test --description='This is a test term' --porcelain`
     Then it should run without errors
-    And STDOUT should be:
-      """
-      Success: Term created.
-      """
+    And STDOUT should match '%d'
 
     When I run the previous command again
     Then STDERR should not be empty
@@ -19,3 +16,21 @@ Feature: Manage WordPress terms
       """
       [{"name":"Test term","slug":"test","description":"This is a test term","parent":"0","count":"0"}]
       """
+
+  Scenario: Creating/deleting a term
+    Given a WP install
+
+    When I run `wp term create 'Test delete term' post_tag --slug=test-delete --description='This is a test term to be deleted' --porcelain`
+    Then it should run without errors
+    And STDOUT should match '%d'
+    And save STDOUT as {TERM_ID}
+
+    When I run `wp term delete {TERM_ID} post_tag`
+    Then it should run without errors
+    And STDOUT should contain:
+      """
+      Deleted post_tag {TERM_ID}.
+      """
+
+    When I run the previous command again
+    Then STDERR should not be empty
index e34cd38..46b85b9 100644 (file)
 
        A parent for the new term.
 
+* `--porcelain`:
+
+       Output just the new term id.
+
 ## EXAMPLES
 
        wp term create Apple category --description="A type of fruit"
index a5cf4ad..366ee1a 100644 (file)
@@ -7,7 +7,7 @@
 \fBwp\-term\-create\fR \- Create a term\.
 .
 .SH "SYNOPSIS"
-wp term create \fIterm\fR \fItaxonomy\fR [\-\-slug=\fIslug\fR] [\-\-description=\fIdescription\fR] [\-\-parent=\fIterm\-id\fR]
+wp term create \fIterm\fR \fItaxonomy\fR [\-\-slug=\fIslug\fR] [\-\-description=\fIdescription\fR] [\-\-parent=\fIterm\-id\fR] [\-\-porcelain]
 .
 .SH "OPTIONS"
 .
@@ -41,6 +41,12 @@ A description for the new term\.
 .IP
 A parent for the new term\.
 .
+.TP
+\fB\-\-porcelain\fR:
+.
+.IP
+Output just the new term id\.
+.
 .SH "EXAMPLES"
 .
 .nf
index c55b585..12e56bd 100644 (file)
@@ -43,7 +43,7 @@ class Term_Command extends WP_CLI_Command {
        /**
         * Create a term.
         *
-        * @synopsis <term> <taxonomy> [--slug=<slug>] [--description=<description>] [--parent=<term-id>]
+        * @synopsis <term> <taxonomy> [--slug=<slug>] [--description=<description>] [--parent=<term-id>] [--porcelain]
         */
        public function create( $args, $assoc_args ) {
 
@@ -56,12 +56,22 @@ class Term_Command extends WP_CLI_Command {
                );
                $assoc_args = wp_parse_args( $assoc_args, $defaults );
 
+               if ( isset( $assoc_args['porcelain'] ) ) {
+                       $porcelain = true;
+                       unset( $assoc_args['porcelain'] );
+               } else
+                       $porcelain = false;
+
                $ret = wp_insert_term( $term, $taxonomy, $assoc_args );
 
                if ( is_wp_error( $ret ) )
                        WP_CLI::error( $ret->get_error_message() );
-               else
-                       WP_CLI::success( "Term created." );
+               else {
+                       if ( $porcelain )
+                               WP_CLI::line( $ret['term_id'] );
+                       else
+                               WP_CLI::success( sprintf( "Created %s %d.", $taxonomy, $ret['term_id'] ) );
+               }
        }
 
        /**
@@ -108,9 +118,9 @@ class Term_Command extends WP_CLI_Command {
                if ( is_wp_error( $ret ) )
                        WP_CLI::error( $ret->get_error_message() );
                else if ( $ret )
-                       WP_CLI::success( "Term deleted." );
+                       WP_CLI::success( sprintf( "Deleted %s %d.", $taxonomy, $term_id ) );
                else
-                       WP_CLI::error( "Error deleting term." );
+                       WP_CLI::error( "Term doesn't exist." );
        }
 
 }