From 5503d3d0068484b7428e223011733667eb4e347f Mon Sep 17 00:00:00 2001 From: Terry Howe Date: Fri, 14 Mar 2014 10:15:29 -0600 Subject: Add value only output formattter It may be useful to have a value only output formatter to perform operationgs like: TOKEN=$(openstack token create -c id -f value) Rather than: eval $(openstack token create -c id -f shell) TOKEN="${id}" Change-Id: I7bda3cc1a1d154a05e8e31eed564dae1e82066d6 Closes-Bug: #1292578 --- cliff/formatters/value.py | 15 +++++++++++++++ cliff/tests/test_formatters_value.py | 15 +++++++++++++++ setup.cfg | 1 + 3 files changed, 31 insertions(+) create mode 100644 cliff/formatters/value.py create mode 100644 cliff/tests/test_formatters_value.py diff --git a/cliff/formatters/value.py b/cliff/formatters/value.py new file mode 100644 index 0000000..9910319 --- /dev/null +++ b/cliff/formatters/value.py @@ -0,0 +1,15 @@ +"""Output formatters values only +""" + +from .base import SingleFormatter + + +class ValueFormatter(SingleFormatter): + + def add_argument_group(self, parser): + pass + + def emit_one(self, column_names, data, stdout, parsed_args): + for value in data: + stdout.write('%s\n' % str(value)) + return diff --git a/cliff/tests/test_formatters_value.py b/cliff/tests/test_formatters_value.py new file mode 100644 index 0000000..25fb23d --- /dev/null +++ b/cliff/tests/test_formatters_value.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python +from six import StringIO + +from cliff.formatters import value + + +def test_value_formatter(): + sf = value.ValueFormatter() + c = ('a', 'b', 'c', 'd') + d = ('A', 'B', 'C', '"no escape me"') + expected = 'A\nB\nC\n"no escape me"\n' + output = StringIO() + sf.emit_one(c, d, output, None) + actual = output.getvalue() + assert expected == actual diff --git a/setup.cfg b/setup.cfg index e019b4f..b7f1529 100644 --- a/setup.cfg +++ b/setup.cfg @@ -33,6 +33,7 @@ cliff.formatter.list = cliff.formatter.show = table = cliff.formatters.table:TableFormatter shell = cliff.formatters.shell:ShellFormatter + value = cliff.formatters.value:ValueFormatter cliff.formatter.completion = bash = cliff.complete:CompleteBash -- cgit v1.2.1