Show Commands

One of the most common patterns with command line programs is the need to print properties of objects. cliff provides a base class for commands of this type so that they only need to prepare the data, and the user can choose from one of several output formatter plugins to see the data in their preferred format.


The base class API extends Command to allow take_action() to return data to be formatted using a user-selectable formatter. Subclasses should provide a take_action() implementation that returns a two member tuple containing a tuple with the names of the columns in the dataset and an iterable that contains the data values associated with those names. See the description of the file command in the demoapp for details.

Show Output Formatters

cliff is delivered with output formatters for show commands. ShowOne adds a command line switch to let the user specify the formatter they want, so you don’t have to do any extra work in your application.


The table formatter uses PrettyTable to produce output formatted for human consumption. This is the default formatter.

(.venv)$ cliffdemo file
|     Field     |    Value     |
| Name          |     |
| Size          | 5825         |
| UID           | 502          |
| GID           | 20           |
| Modified Time | 1335569964.0 |


The shell formatter produces output that can be parsed directly by a typical UNIX shell as variable assignments. This avoids extra parsing overhead in shell scripts.

(.venv)$ cliffdemo file -f shell

(.venv)$ eval "$(cliffdemo file -f shell --prefix example_"
(.venv)$ echo $example_size


The value formatter produces output that only contains the value of the field or fields.

(.venv)$ cliffdemo file -f value -c Size
(.venv)$ SIZE="$(cliffdemo file -f value -c Size"
(.venv)$ echo $SIZE


The yaml formatter uses PyYAML to produce a YAML mapping where the field name is the key.

(.venv)$ cliffdemo file -f yaml
Size: 1807
UID: 1000
GID: 1000
Modified Time: 1393531476.9587486


The json formatter produces a JSON object where the field name is the key.

(.venv)$ cliffdemo file -f json
  "Modified Time": 1438726433.8055942,
  "GID": 1000,
  "UID": 1000,
  "Name": "",
  "Size": 1028

Other Formatters

A formatter using tablib to produce HTML is available as part of cliff-tablib.

Creating Your Own Formatter

If the standard formatters do not meet your needs, you can bundle another formatter with your program by subclassing from cliff.formatters.base.ShowFormatter and registering the plugin in the namespace.