--field & --fields (WP-CLI)

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen

I used to be puzzled about the seemingly redundancy of the --field and --fields options. But no more: With --field, output is not formatted. With --fields, it is, and it seems that you can't straightforwardly get rid of it - All pretty cool!

Example

Let's show this with an example.

Context

We'll be playing with wp plugin get wp-rocket:

$ wp plugin get wp-rocket
+-------------+----------------------------------------+
| Field       | Value                                  |
+-------------+----------------------------------------+
| name        | wp-rocket                              |
| title       | WP Rocket                              |
| author      | WP Media                               |
| version     | 3.14.4.2                               |
| description | The best WordPress performance plugin. |
| status      | active                                 |
+-------------+----------------------------------------+

--field

Let's now use --field to only fetch the version number of the plugin. Note that the output isn't formatted as a table:

$ wp plugin get wp-rocket --field=version
3.15.4

--fields

With --fields, output is nicely formatted in a table:

$ wp plugin get wp-rocket --fields=name,version
+---------+-----------+
| Field   | Value     |
+---------+-----------+
| name    | wp-rocket |
| version | 3.15.4    |
+---------+-----------+

--fields with only 1 field

With --field, output is without formatting, as it is quite clear that this value will be further processed. However, if you do want a nice ASCII-table around your output of only one field, use --fields with only one argument:

$ wp plugin get wp-rocket --fields=name
+-------+-----------+
| Field | Value     |
+-------+-----------+
| name  | wp-rocket |
+-------+-----------+

--fields + --format=csv

Pretty cool, but I still had the feeling that there is some redundancy: Couldn't the flag --fiels simple be replaced by wp plugin get wp-rocket --fields=name --format=csv . Well, no - Eventhough the pretty ASCII borders are gone, the title fields aren't:

$ wp plugin get wp-rocket --fields=name --format=csv
Field,Value
name,wp-rocket

See also