diff options
author | TerryHowe <terrylhowe@gmail.com> | 2015-05-13 13:07:16 -0600 |
---|---|---|
committer | TerryHowe <terrylhowe@gmail.com> | 2015-05-26 10:18:21 -0600 |
commit | 24120da1df54b1438fa173430fd9b0270995b00f (patch) | |
tree | fafd859e101e2e7f5030811d84ac04c2f45e8e8d /cliff | |
parent | 4abf524be5be719ba0b2e6846bb67828ee7653a5 (diff) | |
download | cliff-24120da1df54b1438fa173430fd9b0270995b00f.tar.gz |
Add value format for list command
The value format for the show command is very handy for shell
scripting and a value format for the list command would be just
as handy. It will allow command substitution and piping like:
os container list --format value | grep tmp | while read NAME
do
os container delete $NAME
done
Change-Id: If9784c27d689073e9145b2cb8077ac604025a4c3
Diffstat (limited to 'cliff')
-rw-r--r-- | cliff/formatters/value.py | 10 | ||||
-rw-r--r-- | cliff/tests/test_formatters_value.py | 13 |
2 files changed, 22 insertions, 1 deletions
diff --git a/cliff/formatters/value.py b/cliff/formatters/value.py index 9910319..6cc6744 100644 --- a/cliff/formatters/value.py +++ b/cliff/formatters/value.py @@ -1,14 +1,22 @@ """Output formatters values only """ +import six + +from .base import ListFormatter from .base import SingleFormatter -class ValueFormatter(SingleFormatter): +class ValueFormatter(ListFormatter, SingleFormatter): def add_argument_group(self, parser): pass + def emit_list(self, column_names, data, stdout, parsed_args): + for row in data: + stdout.write(' '.join(map(six.text_type, row)) + u'\n') + return + def emit_one(self, column_names, data, stdout, parsed_args): for value in data: stdout.write('%s\n' % str(value)) diff --git a/cliff/tests/test_formatters_value.py b/cliff/tests/test_formatters_value.py index 25fb23d..a19a4d2 100644 --- a/cliff/tests/test_formatters_value.py +++ b/cliff/tests/test_formatters_value.py @@ -13,3 +13,16 @@ def test_value_formatter(): sf.emit_one(c, d, output, None) actual = output.getvalue() assert expected == actual + + +def test_value_list_formatter(): + sf = value.ValueFormatter() + c = ('a', 'b', 'c') + d1 = ('A', 'B', 'C') + d2 = ('D', 'E', 'F') + data = [d1, d2] + expected = 'A B C\nD E F\n' + output = StringIO() + sf.emit_list(c, data, output, None) + actual = output.getvalue() + assert expected == actual |